在rbind中添加特定列

在rbind中添加特定列,r,R,我有这个数据框 test <- data.frame(col1 = c("test", "test2")) df <- data.frame() df <- rbind(df, data.frame(stock = test$col1)) test有无数可能的答案。以下是一些方法: 库(dplyr) df%>% mutate(name=“google”)#添加一列“name”和值“google” name您应该使用cbind添加列,并阅读有关向量回收的内容。为什么创建数

我有这个数据框

test <- data.frame(col1 = c("test", "test2"))

df <- data.frame()
df <- rbind(df, data.frame(stock = test$col1))

test有无数可能的答案。以下是一些方法:

库(dplyr)
df%>%
mutate(name=“google”)#添加一列“name”和值“google”

name您应该使用cbind添加列,并阅读有关向量回收的内容。为什么创建数据帧只是为了将完整的数据帧作为“行”添加到空数据帧?为什么不简单地在df测试中添加“name”呢?您能解释一下您试图解决的问题吗?你从哪里得到“谷歌”?为什么要绑定空数据帧?如果
df
不是空的怎么办?输出是什么?“谷歌”和“Facebook”?可能你正在寻找
rbind(df,transform(test,name='google'))
df <- rbind(df, data.frame(stock = test$col1, name = c("google", "google"))