1

I hope you can help me with this problem in R:

I have a list like this one (this is dummy data and the real one has like 20 variables and 20 records in the list)...

  Name    |    Type      |   value
  var1    |   double[1]  |   27.3
  var2    |   char[1]    |   'Texas'
  var1    |   double[1]  |   21.4
  var2    |   char[1]    |   'California'
  var3    |   double[1]  |   -1.0

and I would like to put it as a dataframe like this (considering not all records are filled in all variables):

     var1      var2      var3
  1  27.3    'Texas'      NA
  2  21.4  'California'  -1.0

Any idea of how can I do it?

alistaire
  • 42,459
  • 4
  • 77
  • 117
  • To use `tidyr::spread`, you'd need to make an index so it's clear what should end up in which row, e.g. `library(tidyverse); df %>% mutate(row = cumsum(c(0, diff(parse_number(Name))) != 1)) %>% select(-Type) %>% spread(Name, value) %>% type_convert()` The `type_convert` fixes the types all converted to character by mixing them earlier. – alistaire Jun 01 '18 at 14:46
  • @allstaire -- I like the use of diff/parse, but, wouldn't it not work if a grouping had a var1 and var3 but not a var2? – Pawel Jun 01 '18 at 14:54

0 Answers0