Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a column name to R tidyr spread

Tags:

r

tidy

spread

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
like image 862
user2963882 Avatar asked Jun 06 '16 11:06

user2963882


2 Answers

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)
like image 61
zx8754 Avatar answered Nov 16 '22 18:11

zx8754


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).

like image 3
akrun Avatar answered Nov 16 '22 18:11

akrun