r - strsplit by variable separator -


i have strings of data separated " " needs split columns. there easy way split data every nth separator. example, first value in x tells first 4 values in y correspond first trial. second value in x tells next 3 values in y correspond second trial, , on.

x <- c("4 3 3", "3 3 3 2 3") y <- c("110 88 77 66 55 44 33 22 33 44 11 22 11", "44 55 66 33 22 11 22 33 44 55 66 77 88 66 77 88") 

the goal this:

structure(list(session = 1:2, trial.1 = structure(1:2, .label = c("110 88 77", "44 55 66"), class = "factor"), trial.2 = structure(c(2l, 1l), .label = c("33 22 11", "66 55 44"), class = "factor"), trial.3 = structure(1:2, .label = c("22 33 44", "23 33 44"), class = "factor"), trial.4 = structure(c(na, 1l), .label = "55 66", class = "factor"), trial.5 = structure(c(na, 1l), .label = "77 88 66", class = "factor")), .names = c("session", "trial.1", "trial.2", "trial.3", "trial.4", "trial.5"), class = "data.frame", row.names = c(na, -2l)) 

ideally, values y need dropped resulting data frame, , uneven row lengths should filled na's.

this maybe useful

dumx<-strsplit(x,' ') dumy<-strsplit(y,' ') dumx<-lapply(dumx,function(x)(cumsum(as.numeric(x)))) dumx<-lapply(dumx,function(x){mapply(seq,c(1,x+1)[-(length(x)+1)],x,simplify=false)}) ans<-mapply(function(x,y){lapply(x,function(w,z){z[w]},z=y)},dumx,dumy) 

i leave convert resulting list dataframe :)


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

c++ - Accessing inactive union member and undefined behavior? -

php - Get uncommon values from two or more arrays -