R-将一列数据添加到数据帧中的现有列中
我试图找出如何将更多数据一次一列地附加到现有数据帧中。例如:我有这个数据帧:R-将一列数据添加到数据帧中的现有列中,r,dataframe,R,Dataframe,我试图找出如何将更多数据一次一列地附加到现有数据帧中。例如:我有这个数据帧: df <- data.frame("x" = c(1, 2, 3, 4, 5), "y" = c(0.255, 0.236, 0.587, 0.369, 0.789)) 追加数据时,需要确保列名相同 就你而言: df2 <- data.frame(x2, y2) #creating a dataframe names(df2) <- names(df) #changing the col
df <- data.frame("x" = c(1, 2, 3, 4, 5), "y" = c(0.255, 0.236, 0.587, 0.369, 0.789))
追加数据时,需要确保列名相同 就你而言:
df2 <- data.frame(x2, y2) #creating a dataframe
names(df2) <- names(df) #changing the column header names as this is a requirement for append
df <- rbind(df, df2) #appending
df2这应该可以,除非我遗漏了什么:
df <- data.frame("x" = c(1, 2, 3, 4, 5), "y" = c(0.255, 0.236, 0.587, 0.369, 0.789))
df如果适用,请提供副本,而不是发布答案。或者这一个或这一个或这一个或这一个,上述代码可以同时馈送多个df/tibble,而无需使用do.call循环。如下所示,数据帧之间的名称需要保持不变,以便对齐。
df2 <- data.frame(x2, y2) #creating a dataframe
names(df2) <- names(df) #changing the column header names as this is a requirement for append
df <- rbind(df, df2) #appending
df <- data.frame("x" = c(1, 2, 3, 4, 5), "y" = c(0.255, 0.236, 0.587, 0.369, 0.789))
x <- c(6, 7, 8, 9, 10)
y <- c(0.236, 0.963, 0.356, 0.489, 0.333)
df2 <- bind_cols(list(x = x, y = y))
df3 <- bind_rows(list(df, df2))