r - Convert a string with concatenated indices and values to a vector of values -


i have data frame this:

v2      v3 1.000   2:3,3:2,5:2, 2.012   1:5,2:4,6:3, 

the second column v3, consists of 'index-value' pairs, each pair separated ,.

within each 'index-value' pair, number preceeding : vector index. number after : corresponding value. e.g. in first row, vector indices 2, 3, , 5, , corresponding values 3, 2, , 2.

indices not represented in string should have value 0 in resulting vector.

i wish convert 'index-value' vector vector of values.

thus, 2 strings above expected result is:

v2     v3 1.000  c(0,3,2,0,2,0) 2.012  c(5,4,0,0,0,3)    

we make use of data.table package use tstrsplit function. removes intermediate step. try this:

require(data.table) df$v3<-lapply(   lapply(strsplit(as.character(df$v3),",",fixed=true),tstrsplit,":"),    function(x) {res<-numeric(6);res[as.numeric(x[[1]])]<-as.numeric(x[[2]]);res}) #     v2               v3 #1 1.000      0,3,2,0,2,0 #2 2.012      5,4,0,0,0,3 
  • we first split each element of v3 using comma (,)
  • we split again using : separator;
  • we create numeric vector of length 6;
  • we fill values according described logic.

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -