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

> df

id      descr
1       A
2       B
3       B
4       A

How can I reshape the df to look like:

A   B   
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

Jaap
  • 81,064
  • 34
  • 182
  • 193

1 Answers1

2

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
akrun
  • 874,273
  • 37
  • 540
  • 662