按列r合并多个csv
我有多个csv文件,这些文件包含一些相同的列以及不同的列。 比如说,按列r合并多个csv,r,R,我有多个csv文件,这些文件包含一些相同的列以及不同的列。 比如说, #1st.csv col1,col2 1,2 #2nd.csv col1,col3,col4 1,2,3 #3rd.csv col1,col2,col3,col5 1,2,3,4 我尝试根据相同的列组合这些文件,但对于那些不同的列,我只需要 包括所有列,但用NA填充单元格(对于没有该列的数据) 因此,我希望看到: col1,col2,col3,col4,col5 1,2,NA,NA,NA #thi
#1st.csv
col1,col2
1,2
#2nd.csv
col1,col3,col4
1,2,3
#3rd.csv
col1,col2,col3,col5
1,2,3,4
我尝试根据相同的列组合这些文件,但对于那些不同的列,我只需要
包括所有列,但用NA填充单元格(对于没有该列的数据)
因此,我希望看到:
col1,col2,col3,col4,col5
1,2,NA,NA,NA #this is 1st.csv
1,NA,2,3,NA #this is 2nd.csv
1,2,3,NA,4 #this is 3rd.csv
这是我给出的r代码,但它返回一条错误消息
> Combine_data <- smartbind(1st,2nd,3rd)
Error in `[<-.data.frame`(`*tmp*`, , value = list(ID = c(1001, 1001, :
replacement element 1 has 143460 rows, need 143462
>合并数据您应该能够通过dplyr
df1 <- read.csv(text = "col1, col2
1,2", header = TRUE)
df2 <- read.csv(text = "col1, col3, col4
1,2,3", header = TRUE)
df3 <- read.csv(text = "col1, col2, col3, col5
1,2,3,4", header = TRUE)
library(dplyr)
res <- bind_rows(df1, df2, df3)
> res
col1 col2 col3 col4 col5
1 1 2 NA NA NA
2 1 NA 2 3 NA
3 1 2 3 NA 4
df1您应该能够通过dplyr
df1 <- read.csv(text = "col1, col2
1,2", header = TRUE)
df2 <- read.csv(text = "col1, col3, col4
1,2,3", header = TRUE)
df3 <- read.csv(text = "col1, col2, col3, col5
1,2,3,4", header = TRUE)
library(dplyr)
res <- bind_rows(df1, df2, df3)
> res
col1 col2 col3 col4 col5
1 1 2 NA NA NA
2 1 NA 2 3 NA
3 1 2 3 NA 4
df1 try合并数据try合并数据