0

The dataframe contains information in this format:

Type        Value
-------------------
catA        one
catB        two
catA        three

My target is to convert the dataframe to this format (Type's values as columns):

catA        catB
-----------------
 one          -
  -          two
three         -

I have been looking into "dummy variables" but it's not what I need. ¿Could someone give me some ideas?

Yari
  • 867
  • 3
  • 9
  • 13

3 Answers3

3
 library(reshape2)
 df <- data.frame(Type=c("catA","catB","catA"),value=c("one","two","three"))
 df
 #   Type value
 # 1 catA   one
 # 2 catB   two
 # 3 catA three

 dcast(df,value~Type)
 #   value  catA catB
 # 1   one   one <NA>
 # 2 three three <NA>
 # 3   two  <NA>  two

 dcast(df,Type~value)
 #   Type  one three  two
 # 1 catA  one three <NA>
 # 2 catB <NA>  <NA>  two

To preserve the order of value

 df$Type <- factor(df$Type,c("catA","catB"))
 df$value <- factor(df$value,c("one","two","three"))

 dcast(df,Type~value)
 #   Type  one  two three
 # 1 catA  one <NA> three
 # 2 catB <NA>  two  <NA>

 dcast(df,value~Type)
 #   value  catA catB
 # 1   one   one <NA>
 # 2   two  <NA>  two
 # 3 three three <NA>
Sowmya S. Manian
  • 3,723
  • 3
  • 18
  • 30
1

You have a long format table and you want to have a wide format. Use dcast() function from reshape2 package for that.

Sowmya S. Manian
  • 3,723
  • 3
  • 18
  • 30
Rockbar
  • 1,081
  • 1
  • 20
  • 31
0

Since you are not working with a rectangular data set, it may make more sense to store the result in a list. This can be accomplished with unstack:

unstack(df, form=Value~Type)
$catA
[1] "one"   "three"

$catB
[1] "two"

data

df <- read.table(header=T, text="Type        Value
catA        one
catB        two
catA        three")
lmo
  • 37,904
  • 9
  • 56
  • 69