如何一次性向data.frame添加多个列?

时间:2021-02-10 22:55:03

I have following dataframe and vector:

我有以下数据帧和向量:

ddf = data.frame(a=rep(1,10), b=rep(2,10))
xx = c("c", "d", "e", "f")

How can I new empty columns which are named with items in xx ?

如何使用xx中的项目命名的新空列?

I tried following but it does not work:

我试过以下但它不起作用:

ddf = cbind(ddf, data.frame(xx))
Error in data.frame(..., check.names = FALSE) : 
  arguments imply differing number of rows: 10, 4

Following also does not work:

以下也不起作用:

for(i in 1:length(xx)){
    ddf$(xx[i]) = ""  
}

Error: unexpected '(' in:
"for(i in 1:length(xx)){
ddf$("
 }
Error: unexpected '}' in "}"

2 个解决方案

#1


23  

This will get you there:

这会让你到达那里:

ddf[xx] <- NA

#   a b  c  d  e  f
#1  1 2 NA NA NA NA
#2  1 2 NA NA NA NA
#3  1 2 NA NA NA NA
#...

You can't directly use something like ddf$xx because this will try to assign to a column called xx rather than interpreting xx. You need to use [ and [<- functions, using the square brackets when you are dealing with a character string - like ddf["columnname"].

你不能直接使用类似ddf $ xx的东西,因为这会尝试分配给一个名为xx的列而不是解释xx。你需要使用[和[< - 函数,在处理字符串时使用方括号 - 比如ddf [“columnname”]。

The reason why it selects columns is because data.frames are lists essentially:

它选择列的原因是因为data.frames本质上是列表:

is.list(ddf)
#[1] TRUE

as.list(ddf)
#$a
# [1] 1 1 1 1 1 1 1 1 1 1
# 
#$b
# [1] 2 2 2 2 2 2 2 2 2 2

...with each column corresponding to a list entry. So if you don't use a comma to specify a row, like ddf["name",] or a column like ddf[,"name"], you get the column by default.

...每列对应一个列表条目。因此,如果您不使用逗号指定行,例如ddf [“name”,]或ddf [,“name”]之类的列,则默认情况下会获得该列。

#2


4  

This seems to succeed:

这似乎成功了:

> cbind(ddf, setNames( lapply(xx, function(x) x=NA), xx) )
   a b  c  d  e  f
1  1 2 NA NA NA NA
2  1 2 NA NA NA NA
3  1 2 NA NA NA NA
4  1 2 NA NA NA NA
5  1 2 NA NA NA NA
6  1 2 NA NA NA NA
7  1 2 NA NA NA NA
8  1 2 NA NA NA NA
9  1 2 NA NA NA NA
10 1 2 NA NA NA NA

#1


23  

This will get you there:

这会让你到达那里:

ddf[xx] <- NA

#   a b  c  d  e  f
#1  1 2 NA NA NA NA
#2  1 2 NA NA NA NA
#3  1 2 NA NA NA NA
#...

You can't directly use something like ddf$xx because this will try to assign to a column called xx rather than interpreting xx. You need to use [ and [<- functions, using the square brackets when you are dealing with a character string - like ddf["columnname"].

你不能直接使用类似ddf $ xx的东西,因为这会尝试分配给一个名为xx的列而不是解释xx。你需要使用[和[< - 函数,在处理字符串时使用方括号 - 比如ddf [“columnname”]。

The reason why it selects columns is because data.frames are lists essentially:

它选择列的原因是因为data.frames本质上是列表:

is.list(ddf)
#[1] TRUE

as.list(ddf)
#$a
# [1] 1 1 1 1 1 1 1 1 1 1
# 
#$b
# [1] 2 2 2 2 2 2 2 2 2 2

...with each column corresponding to a list entry. So if you don't use a comma to specify a row, like ddf["name",] or a column like ddf[,"name"], you get the column by default.

...每列对应一个列表条目。因此,如果您不使用逗号指定行,例如ddf [“name”,]或ddf [,“name”]之类的列,则默认情况下会获得该列。

#2


4  

This seems to succeed:

这似乎成功了:

> cbind(ddf, setNames( lapply(xx, function(x) x=NA), xx) )
   a b  c  d  e  f
1  1 2 NA NA NA NA
2  1 2 NA NA NA NA
3  1 2 NA NA NA NA
4  1 2 NA NA NA NA
5  1 2 NA NA NA NA
6  1 2 NA NA NA NA
7  1 2 NA NA NA NA
8  1 2 NA NA NA NA
9  1 2 NA NA NA NA
10 1 2 NA NA NA NA