professor professor - 1 year ago 86
R Question

summary of data in data frame [r]

>id = c(1,2,3,4)
>desc = c("A", "B", "B", "A")
>df = data.frame(id, desc)

id descr
1 A
2 B
3 B
4 A

how can I reshape the df to look like
1 2
4 3

I've tried dcast, table, etc.

both are character variable. I basically want to go from long to wide but idk what syntax to use

Answer Source

We can use unstack (assuming that there are equal number of elements in 'id' for 'desc')

unstack(df, id~desc)
#  A B
#1 1 2
#2 4 3