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
  • 77,147
  • 31
  • 174
  • 185

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
  • 789,025
  • 32
  • 460
  • 575