string - R strsplit, nested lists blues -


i facing issue in r in want split strings on comma , further split on semicolon, keep first item before semicolon i.e. ee , jj below. have tried bunch of things nested lists seem convoluted!

here's doing:

d <- c("aa,bb,cc,dd,ee;e,ff",        "gg,hh,ii,jj;j")  e=strsplit(d,",") myfun2 <- function(x,arg1) {  strsplit(x,";") } f=lapply(e,myfun2) f=  [[1]]  [[1]][[1]]  [1] "aa"   [[1]][[2]]  [1] "bb"   [[1]][[3]]  [1] "cc"   [[1]][[4]]  [1] "dd"   [[1]][[5]]  [1] "ee" "e"    [[1]][[6]]  [1] "ff"   [[2]]  [[2]][[1]]  [1] "gg"   [[2]][[2]]  [1] "hh"   [[2]][[3]]  [1] "ii"   [[2]][[4]]  [1] "jj" "j"  

here's output want

correct output= [[1]] [1] "aa" "bb" "cc" "dd" "ee" "ff"  [[2]] [1] "gg" "hh" "ii" "jj" 

i have tried bunch of things using lapply nested list "f" , used "[[" , "[" no success.

any appreciated. (i know missing silly, can't figure out right now!)

this code

d <- c("aa,bb,cc,dd,ee;e,ff", "gg,hh,ii,jj;j") e <- strsplit(d,",") myfun2 <- function(x,arg1) { strsplit(x,";") } f <- lapply(e,myfun2) 

if start f, next step be

lapply(f,function(x) mapply(`[`,x,1)) 
 [[1]] [1] "aa" "bb" "cc" "dd" "ee" "ff"  [[2]] [1] "gg" "hh" "ii" "jj" 

basically, need inner , outer type apply function go down 2 levels of nesting.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -