1

I have a dataframe (all elements numerics) that looks like this:

var1  var2  var3
1     1     10
1     3     30
1     2     20
2     6     30
2     4     20
2     2     10

I'd like to rearrange in this way (var1 should be rownames without repetition and var2 should be the first row):

   1   2   3   4   6
1  10  20  30  NA  NA
2  NA  10  NA  20  30

Note that colunm 5 is skipped because var2 doesnt have '5' value. I have problems to choose a kind method to handle with this.

Which R function/package do you recommend me? Is it easier to get that with Linux commands?

  • 1
    This should work for you: `reshape(dat, idvar = "var1", timevar = "var2", direction = "wide")` or `tidyr::spread(dat, var2, var3)` ; `dat` is the name of your data. – markus Apr 10 '19 at 12:08

1 Answers1

1

You can use dcast from reshape2 package-

> reshape2::dcast(dt,var1~var2, value.var = "var3")

    var1  1  2  3  4  6
  1    1 10 20 30 NA NA
  2    2 NA 10 NA 20 30

Note- If your data is large then use dcast.data.table from data.table package.

> data.table::setDT(dt)
> dcast.data.table(dt,var1~var2,value.var = "var3")
   var1  1  2  3  4  6
1:    1 10 20 30 NA NA
2:    2 NA 10 NA 20 30
Rushabh Patel
  • 2,672
  • 13
  • 34