r - How to make triplet set of columns and apply function to it -
i have dataframe mycols
. can see mycols has ^sqn
, ^wes
, ^wgs
column sets. want make triplet set of sqn
, wes
, wgs
columns (i dont want include (pile:up)$
, :ad$
columns) in same order (you can see extension same sqn,wes , wgs every set) across mycols
. in other word, want make group of sqn, wes , wgs have same extension. have function called myfunc
. want apply function each triple set formed.
mycols<- c("sqn:idh2:g515t:r172m","wes:idh2:g515t:r172m" ,"wes:idh2:g515t:r172m:ad:(pile:up)", "wgs:idh2:g515t:r172m","sqn:jak1:a1432t:t478s", "wes:jak1:a1432t:t478s" ,"wes:jak1:a1432t:t478s:ad:(pile:up)","wgs:jak1:a1432t:t478s","sqn:jak1:t1868c:v623a","wes:jak1:t1868c:v623a","wes:jak1:t1868c:v623a:ad","wes:jak1:t1868c:v623a:ad:(pile:up)", "wgs:jak1:t1868c:v623a")
result:
triplet1 "sqn:idh2:g515t:r172m", "wes:idh2:g515t:r172m", "wgs:idh2:g515t:r172m" triplet2 "sqn:jak1:a1432t:t478s","wes:jak1:a1432t:t478s","wgs:jak1:a1432t:t478s", triplet3 "sqn:jak1:t1868c:v623a","wes:jak1:t1868c:v623a","wgs:jak1:t1868c:v623a"
so can call function triplet1, triple 2, triplet3...
we can logical index of strings don't have 'p(p)ile:up' or 'ad' (at end) grepl
. subset 'mycols' 'i1'. create grouping variable using sub
removing prefix part starts alphabetic characters including first :
, split
'mycols1'.
i1 <- !grepl('(?i)(p)ile|ad$', mycols) mycols1 <- mycols[i1] split(mycols1, sub('[^:]+:', '', mycols1)) #$`idh2:g515t:r172m` #[1] "sqn:idh2:g515t:r172m" "wes:idh2:g515t:r172m" "wgs:idh2:g515t:r172m" #$`jak1:a1432t:t478s` #[1] "sqn:jak1:a1432t:t478s" "wes:jak1:a1432t:t478s" "wgs:jak1:a1432t:t478s" #$`jak1:t1868c:v623a` #[1] "sqn:jak1:t1868c:v623a" "wes:jak1:t1868c:v623a" "wgs:jak1:t1868c:v623a"
Comments
Post a Comment