I have a dataframe that looks like this:
x <- c('Jim', 'Jim', 'Jim', 'Sue', 'Sue', 'Sue')
y <- c(100, 200, 150, 40, 50, 30)
z <- c(5, 6, 4, 3, 4, 4)
num <- c(1, 2, 3, 1, 2, 3)
df <- data.frame(x,y,z,num)
And I need to transpose this so that i have a row for Jim and a row for Sue, with the values of y1, y2, y3, z1, z2, z3.
I know how to do this with data.table::dcast
but the Linux server I'm using is having difficulty loading this package. Therefore, I am trying to do it with the reshape
package or even just the reshape()
function, but running into difficulty.
# This gives the desired result
df1 <- data.table::dcast(setDT(df), x ~ num, value.var=c('y', 'z'))
# Trying to figure out what I'm missing here...
df2 <- reshape::cast(df, num ~ x, value = c('y', 'z'))