0

I have a dataframe similar to below,

Name   | ID             |  SET  | COUNT  |
------ | ------         |------ | ------ |
Value  | 44000001005    | 0     | 24     |
Value  | 10000000019659 | 0     | 29     |
Value  | 10000000019659 | 1     | 5      |

The result that I need is something like,

Name   | ID             |  0    | 1      |
------ | ------         |------ | ------ |
Value  | 44000001005    | 24    | 0      |
Value  | 10000000019659 | 29    | 5      |

Can this be done or would I have to re-work the data set? I am relatively new to R, so I may have missed some very obvious logic, but would appreciate if anyone could guide me. Thank you.

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
RBK
  • 375
  • 2
  • 5
  • 12

1 Answers1

2

If you want to change the format from a long to a wide format you can use the spread function from the tidyr package. There are other packages and possibilities, but this is my favorite.

If you are new to R, be aware that you have to install the package first with install.packages("tidyr").

Name <- c("Value","Value","Value")
ID <- c(6546465445,5464564,5464564)
SET <- c(0,0,1)
COUNT <- c(24,29,5)

df <- cbind.data.frame(Name,ID,SET,COUNT,stringsAsFactors=FALSE)

library(tidyr)

spread(data=df,key=SET,value = COUNT,fill=0) -> df_wide

see the documentation ?spread for details about the function.

PhiSeu
  • 301
  • 2
  • 9
  • 1
    This works perfect. Thank you so much. The documentation helped me get a clearer picture of the function. – RBK Aug 14 '16 at 11:49