string - Model a data.table using more vectors as reference (in R) -


a few days ago asked question: creating new columns splitting variable many variables (in r) . @ end used code:

nbtabelle <- data.table(val=netz) attach(nbtabelle) for(level_var in namesvec){ nbtabelle[, eval(level_var) := ifelse(substr(eval(val), 7, 8) == level_var, val, 0)] } 

now want modify code transform table (it example, true table huge!):

    year val_base     val       01       02       03       04  1: 2008 000000   00000001 00000001        0        0        0  2: 2008 000000   00000002        0 00000002        0        0  3: 2008 000000   00000003        0        0 00000003        0  4: 2009 000000   00000003        0        0 00000003        0  5: 2008 000000   00000004        0        0        0 00000004  6: 2009 111111   11111101 11111101        0        0        0  7: 2010 111111   11111101 11111101        0        0        0  8: 2011 111111   11111101 11111101        0        0        0  9: 2012 111111   11111101 11111101        0        0        0 10: 2013 111111   11111101 11111101        0        0        0 11: 2014 111111   11111101 11111101        0        0        0 12: 2009 111111   11111102        0 11111102        0        0 13: 2010 111111   11111102        0 11111102        0        0 14: 2011 111111   11111102        0 11111102        0        0 15: 2012 111111   11111102        0 11111102        0        0 16: 2009 111111   11111103        0        0 11111103        0 17: 2010 111111   11111103        0        0 11111103        0 18: 2009 111111   11111104        0        0        0 11111104 19: 2010 111111   11111104        0        0        0 11111104 20: 2011 111111   11111104        0        0        0 11111104 

into this:

   year val_base      val       01       02       03       04 1  2008 000000   00000001 00000001 00000002 00000003 00000004 4  2009 000000   00000003        0        0 00000003        0 6  2009 111111   11111101 11111101 11111102 11111103 11111104 7  2010 111111   11111101 11111101 11111102 11111103 11111104 8  2011 111111   11111101 11111101 11111102        0 11111104 9  2012 111111   11111101 11111101 11111102        0        0 10 2013 111111   11111101 11111101        0        0        0 11 2014 111111   11111101 11111101        0        0        0 

indeed i´m interested in knowing how many variables 01, 02, etc... per year , per val_base there (the next step sum of variables 01, 02, etc. per year).

i thought solve problem nesting more ifelse functions inside loop. idea like:

for(level_var in namesvec){   for(i in 1:19){   dt2[, eval(level_var) := ifelse(substr(eval(val), 7, 8) == level_var, val, ifelse(eval(val[i]) != val[i+1],0,val[i]))]  } } 

but wrong..


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -