9

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

这里只是一个玩具示例

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)

错误:输入中不存在键列“值”。

虽然下面工作正常:

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
4

3 回答 3

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)
于 2016-06-06T11:22:22.393 回答
3

使用data.table

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

关于在tidyr函数中传递名称,这个答案也可能有所帮助(几个小时前发布)。

于 2016-06-06T11:26:37.350 回答
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
于 2018-07-27T14:12:01.193 回答