将列名传递给R tidyr spread

时间:2016-06-06 11:08:04

标签: r tidy spread

我试图将带有列名的对象传递给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)
  

错误:键列&#39;值&#39;在输入中不存在。

虽然下面工作正常:

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

3 个答案:

答案 0 :(得分:13)

我们可以使用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)

答案 1 :(得分:3)

使用data.table

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

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

答案 2 :(得分:0)

看起来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