0

I have the following data frame.

Student    Score
Thomas     23.6
Sally      28.1
Chris      27.9
Morrison   32.5
Thomas     30.3
Sally      54.2
Morrison   44.3
Chris      99.2

How do I convert it into

Thomas    Sally    Morrison    Chris
23.6      28.1     32.5        27.9
30.3      54.2     44.3        99.2

NOTE: It doesn't have to have the exact order of the data frame above.

I tried transforming it with reshape2, reshape, dcast, melt, cbind, etc. I couldn't find anything that worked.

Pierre L
  • 28,203
  • 6
  • 47
  • 69
cooldood3490
  • 2,418
  • 7
  • 51
  • 66

1 Answers1

1

Use the dcast function in the reshape2 package.

d1 <- read.table(text="Student    Score
Thomas     23.6
Sally      28.1
Chris      27.9
Morrison   32.5
Thomas     30.3
Sally      54.2
Morrison   44.3
Chris      99.2", head=T, as.is=T)

library(dplyr)

d2 <- d1 %>% group_by(Student) %>% mutate(cn=1:n())

library(reshape2)

dcast(d2, cn~Student, value.var = "Score")
#   cn Chris Morrison Sally Thomas
# 1  1  27.9     32.5  28.1   23.6
# 2  2  99.2     44.3  54.2   30.3
Ven Yao
  • 3,680
  • 2
  • 27
  • 42