Home > Software design >  Collapsing Rows in R: cannot coerce class ‘"formula"’ to a data.frame (R)
Collapsing Rows in R: cannot coerce class ‘"formula"’ to a data.frame (R)

Time:10-28

I have the following data in R:

  name = c("person1", "person2", "person1", "person2")
  iter = c(1,1,2,2)
  col1 = c(12, 33, 55, 8)
  col2 = c(4, 7, 88,2)
  col3 = c(11,1,6, 7)
  col4 = c(55, 2 , 4 ,5)
  
  orig_data = data.frame(name, iter, col1, col2, col3, col4)

This data looks like this:

     name iter col1 col2 col3 col4
1 person1    1   12    4   11   55
2 person2    1   33    7    1    2
3 person1    2   55   88    6    4
4 person2    2    8    2    7    5

I am trying to make the final result look like this:

   name iter col1_iter1 col1_iter2 col2_iter1 col2_iter2 col3_iter1 col3_iter2 col4_iter1 col4_iter2
1 person1    1         12         55          4         88         11          6         55          4
2 person2    2         33          8          7          2          1          7          2          5

I did some reseach and found that the "data.table" library in R might be able to solve this problem. I tried the following code in R:

library(data.table)
  
  dcast_data = data.frame(setDT(orig_data), iter ~ name, value.var = c('col1', 'col2', 'col3', 'col4'))

But this gives the following error:

Error in as.data.frame.default(x[[i]], optional = TRUE) : 
  cannot coerce class"formula"’ to a data.frame

Can someone please show me what I am doing wrong?

CodePudding user response:

Since you want to get the data in wide format I think you are looking for dcast function instead of data.frame. Also the formula that is used should be changed.

library(data.table)
dcast(setDT(orig_data), name~iter, value.var = c('col1', 'col2', 'col3', 'col4'))

#      name col1_1 col1_2 col2_1 col2_2 col3_1 col3_2 col4_1 col4_2
#1: person1     12     55      4     88     11      6     55      4
#2: person2     33      8      7      2      1      7      2      5
  • Related