简体   繁体   English

在R中组合多个数据帧

[英]Combining multiple dataframe in R

I have 5 dataframe ( df1 , df2 , df3 , df4 , df5 ). 我有5个数据帧( df1df2df3df4df5 )。 All have same columns and column names (NIR database). 所有列和列名称都相同(NIR数据库)。 I would like to frist combine df1 and df2 into df12 and then df3 , df4 and df5 into df345 and finally combine df12 and df345 into df . 我想df12 df1df2合并为df12 ,然后将df3df4df5合并为df345 ,最后将df12df345合并为df (It has to be this two stages). (必须是这两个阶段)。

df12 <- do.call(rbind, list(df1,df2))
df345 <- do.call(rbind, list(df3,df4,df5))
df <- do.call(rbind, list(df12,df345))
newdf <- data.frame(oiltype="olive",nir=df[2:276]);

With this I got one of the column names become nir.nir.V4 while I need it to be nir.V4 . 有了这个,我得到了其中一个列名称为nir.nir.V4而我需要它为nir.V4

I think this is due to the use of list. 我认为这是由于使用列表。 I would like to know if there's any alternative to combine multiple dataframes without having to face this trouble. 我想知道是否有其他选择可以组合多个数据帧而不必面对这种麻烦。 Appreciate any suggestion. 感谢任何建议。

I have 5 data.frame df1, df2, df3, df4, df5 having same columns and column names. 我有5个data.frame df1,df2,df3,df4,df5,它们具有相同的列和列名称。

> df1
      Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
> df2
      Sepal.Length Sepal.Width Petal.Length Petal.Width Species
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa
> df3
      Sepal.Length Sepal.Width Petal.Length Petal.Width Species
7          4.6         3.4          1.4         0.3  setosa
8          5.0         3.4          1.5         0.2  setosa
9          4.4         2.9          1.4         0.2  setosa
> df4
      Sepal.Length Sepal.Width Petal.Length Petal.Width Species
10          4.9         3.1          1.5         0.1  setosa
11          5.4         3.7          1.5         0.2  setosa
12          4.8         3.4          1.6         0.2  setosa
> df5
      Sepal.Length Sepal.Width Petal.Length Petal.Width Species
13          4.8           3          1.4         0.1  setosa
14          4.3           3          1.1         0.1  setosa
15          5.8           4          1.2         0.2  setosa

For combining df1 and df2 用于组合df1和df2

> df12 <- rbind(df1,df2)
> df12
     Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

For combining df3, df4, df5 do the same as above 要合并df3,df4,df5,请执行与上述相同的操作

> df345 <- rbind(df3,df4,df5)
> df345
    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
7           4.6         3.4          1.4         0.3  setosa
8           5.0         3.4          1.5         0.2  setosa
9           4.4         2.9          1.4         0.2  setosa
10          4.9         3.1          1.5         0.1  setosa
11          5.4         3.7          1.5         0.2  setosa
12          4.8         3.4          1.6         0.2  setosa
13          4.8         3.0          1.4         0.1  setosa
14          4.3         3.0          1.1         0.1  setosa
15          5.8         4.0          1.2         0.2  setosa

And at last combining newly formed data frame can be done in similar way 最后,可以类似的方式组合新形成的数据帧

> df <- rbind(df12,df345)
> df
     Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1           5.1         3.5          1.4         0.2  setosa
2           4.9         3.0          1.4         0.2  setosa
3           4.7         3.2          1.3         0.2  setosa
4           4.6         3.1          1.5         0.2  setosa
5           5.0         3.6          1.4         0.2  setosa
6           5.4         3.9          1.7         0.4  setosa
7           4.6         3.4          1.4         0.3  setosa
8           5.0         3.4          1.5         0.2  setosa
9           4.4         2.9          1.4         0.2  setosa
10          4.9         3.1          1.5         0.1  setosa
11          5.4         3.7          1.5         0.2  setosa
12          4.8         3.4          1.6         0.2  setosa
13          4.8         3.0          1.4         0.1  setosa
14          4.3         3.0          1.1         0.1  setosa
15          5.8         4.0          1.2         0.2  setosa

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM