1

Anyone know how can I split a column to multiple ones? For example: I want to split column "score" and "class", then make the values of column "grade" as column name. In my data, I have 50 different values in column "grade" instead of only two in the example below. In the data frame 2, the row names are the values of column "class" in data frame 1.

data frame 1

class   grade   score
A          a    12
B          a    45
C          a    75
D          a    18
E          a    6
A          b    45
B          b    92
C          b    78
D          b    36
E          b    39

data frame 2

    a   b
A   12  45
B   45  92
C   75  78
D   18  36
E   6   39
wsda
  • 1,335
  • 1
  • 12
  • 16

3 Answers3

4

Base R's unstack does this out of the box:

unstack(df, score ~ grade)
#   a  b
#1 12 45
#2 45 92
#3 75 78
#4 18 36
#5  6 39

As does xtabs:

as.data.frame.matrix(xtabs(score ~ class + grade, data=df))

#   a  b
#A 12 45
#B 45 92
#C 75 78
#D 18 36
#E  6 39
thelatemail
  • 91,185
  • 12
  • 128
  • 188
2
library(reshape2)

dcast(df, class ~ grade, value.var = "score")
  class  a  b
1     1 12 45
2     2 45 92
3     3 75 78
4     4 18 36
5     5  6 39

df <- structure(list(class = c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 
5L), grade = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L), .Label = c("a", "b"), class = "factor"), score = c(12L, 
45L, 75L, 18L, 6L, 45L, 92L, 78L, 36L, 39L)), .Names = c("class", 
"grade", "score"), class = "data.frame", row.names = c(NA, -10L
))
2

Another option is spread from library(tidyr)

library(tidyr)
spread(df1, grade, score)
akrun
  • 874,273
  • 37
  • 540
  • 662