9

I am trying to pass an object with the column name to the spread function, but instead of reading the value inside the object it just tries to use the object name itself

Here just a toy example

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.

whilst below works fine:

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
zx8754
  • 52,746
  • 12
  • 114
  • 209
user2963882
  • 625
  • 1
  • 8
  • 19

3 Answers3

13

We can use spread_() to pass variable names as strings:

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)
zx8754
  • 52,746
  • 12
  • 114
  • 209
3

Using data.table

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

Regarding passing names in tidyr functions, this answer could also help (which was posted a couple of hours back).

Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662
0

looks like tidyr recognize it automatically now - it works both ways: type in the column names or put them in strings

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
YJZ
  • 3,934
  • 11
  • 43
  • 67