将列名传递给 R tidyr spread

Passing a column name to R tidyr spread

我正在尝试将具有列名称的对象传递给传播函数,但它没有读取对象内部的值,而是尝试使用对象名称本身

这里只是一个玩具示例

library(tidyr)
d = (1:4)
n = c("a"," a", "b","b") 
s = c(1, 2,5,7) 

df = data.frame(d,n, s) 

Value <- n
data_wide <- spread(df, Value , s)

Error: Key column 'Value' does not exist in input.

虽然以下工作正常:

data_wide <- spread(df, n, s)
d  a  a  b
1 1 NA  1 NA
2 2  2 NA NA
3 3 NA NA  5
4 4 NA NA  7

我们可以使用spread_()将变量名作为字符串传递:

library(tidyr)
# dummy data
df1 <- data.frame(d = (1:4),
                  n = c("a", "a", "b", "b") ,
                  s = c(1, 2, 5, 7)) 

myKey <- "n"
myValue <- "s"
spread_(data = df1, key_col = myKey , value_col = myValue)

使用data.table

library(data.table)
 dcast(setDT(df), eval(as.name(myValue))~ eval(as.name(myKey)), value.var=myValue)

关于在 tidyr 函数中传递名称,这个 也可以提供帮助(几个小时前发布)。

看起来 tidyr 现在可以自动识别它 - 它可以双向工作:输入列名或将它们放入字符串中

stocks <- data.frame(
    time = as.Date('2009-01-01') + 0:9,
    X = rnorm(10, 0, 1),
    Y = rnorm(10, 0, 2),
    Z = rnorm(10, 0, 4)
)
stocksm <- stocks %>% gather(stock, price, -time) # make it to skinny table

# make it wide table
stocksm %>% spread(stock, price)

# time          X           Y          Z
# 1  2009-01-01  0.7444343 -0.07030219  0.9140019
# 2  2009-01-02  1.1988507  2.98659296  5.3044361
# 3  2009-01-03 -0.4344259 -0.11526884 -3.8380602
# 4  2009-01-04  0.8154400  2.08313458 -0.1152524
# 5  2009-01-05  1.1965647 -0.59055846  3.5647410
# ...



# it's same if put column names in strings
stocksm %>% spread('stock', 'price')

# time          X           Y          Z
# 1  2009-01-01  0.7444343 -0.07030219  0.9140019
# 2  2009-01-02  1.1988507  2.98659296  5.3044361
# 3  2009-01-03 -0.4344259 -0.11526884 -3.8380602
# 4  2009-01-04  0.8154400  2.08313458 -0.1152524
# 5  2009-01-05  1.1965647 -0.59055846  3.5647410



# or put in string variables
col1 = 'stock'
col2 = 'price'
stocksm %>% spread(col1, col2)

# time          X           Y          Z
# 1  2009-01-01  0.7444343 -0.07030219  0.9140019
# 2  2009-01-02  1.1988507  2.98659296  5.3044361
# 3  2009-01-03 -0.4344259 -0.11526884 -3.8380602
# 4  2009-01-04  0.8154400  2.08313458 -0.1152524
# 5  2009-01-05  1.1965647 -0.59055846  3.5647410