I have a data with the format as data_format1. Based on it, I want to convert the cases to variables to get data_format2.
you can find data_format1 here:
ArticleID<-c(1, 2, 3, 3, 4)
Word<-c("a", "b", "b", "c", "c")
Freq<-c(2, 4, 6, 3, 2)
data_format1<-cbind(ArticleID, Word, Freq)
data_format1
ArticleID Word Freq
[1,] "1" "a" "2"
[2,] "2" "b" "4"
[3,] "3" "b" "6"
[4,] "3" "c" "3"
[5,] "4" "c" "2"
data_format2 is given here:
ArticleID_t<-c(1, 2, 3, 4)
a<-c(2, 0, 0, 0)
b<-c(0, 4, 6, 0)
c<-c(0, 0, 3, 2)
data_format2<-cbind(ArticleID_t, a, b, c)
data_format2
ArticleID_t a b c
[1,] 1 2 0 0
[2,] 2 0 4 0
[3,] 3 0 6 3
[4,] 4 0 0 2
This is a classical reshape problem – converting data from a tall format to a wide format. The
reshape2package is ideal for this.Note: You need to convert your data into a
data.framefirst. Remember thatcbindwill take your data and create an array, rather than adata.frame. So in the solution I usedata.frame(...)to recreate your data.See
?reshape2::dcastfor more information.