在dplyr中基于vector创建新列 [英] Create new columns based on vector in dplyr
问题描述
使用 dplyr
可以轻松地使用 mutate
创建新列:
With dplyr
it is easy to create a new column using mutate
:
df <- data.frame(v1 = 1:3, v2 = c('a','b','c'))
> mutate(df, newcol = NA)
v1 v2 newcol
1 1 a NA
2 2 b NA
3 3 c NA
我们还可以使用 mutate_at
使用向量创建多个新列(显示为
We can also create multiple new columns with a vector using mutate_at
(shown here):
> cnames <- c('newcol1', 'newcol2', 'newcol3')
> mutate_at(df, cnames, funs(log(v1)))
v1 v2 newcol1 newcol2 newcol3
1 1 a 0.0000000 0.0000000 0.0000000
2 2 b 0.6931472 0.6931472 0.6931472
3 3 c 1.0986123 1.0986123 1.0986123
是否有一种简单的方法使用 dplyr
将这些新列初始化为NA?
Is there a simple way to initialize these new columns as NA using dplyr
?
例如, mutate_at(df,cnames,funs(v1 * NA))
会提供所需的结果,但这似乎是间接的.我想要的是类似以下内容的东西:
For example, mutate_at(df, cnames, funs(v1 * NA))
gives the desired result, but that seems indirect. What I would like is something along the lines of:
mutate_at(df, cnames, funs(. = NA)) # Error: Can't create call to non-callable object
我们不需要知道其他任何列的名称.
where we don't need to know the names of any other columns.
(我知道这可以通过 df [,cnames]<-NA
来解决,但是我正在寻找使用 dplyr
函数的解决方案)
(I know this is simply solved with df[ , cnames] <- NA
, but I'm looking for a solution using dplyr
functions)
使用 dplyr
的更高版本,示例变为:
Using later versions of dplyr
the example becomes:
mutate_at(df, all_of(cnames), funs(log(v1)))
推荐答案
您可以这样做.
library(dplyr)
df %>%
`is.na<-`(cnames)
# v1 v2 newcol1 newcol2 newcol3
#1 1 a NA NA NA
#2 2 b NA NA NA
#3 3 c NA NA NA
我希望一个%>%
足够 dplyr
.;)
这篇关于在dplyr中基于vector创建新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!