how to use a variable in grepl function within a loop-R -
i new r and, have problems looping , grepl functions have data like:
str(peptidesfilter) 'data.frame': 78389 obs. of 130 variables: $ sequence : chr "aaaaaiggr" "aaaaaiggrpnyygneggr" "aaaaassnpgggpemvr" "aaaaavggr" ... $ first.amino.acid : chr "a" "a" "a" "a" ... $ protein.group.ids : chr "1" "1;2;4" "2;5 "3" "4;80" ...
i want filter data according $ protein.group.ids using grepl function below
peptidesfilter.new <- peptidesfilter[grepl('(^|;)2($|;)', peptidesfilter$protein.group.ids),]
i want loop every individual data ( e.g 1, 2, 3, etc...) , re-write name of data frame containing variable peptidesfilter.i
=1 while( <= n) { peptidesfilter.[[i]] <- peptidesfilter[grepl('(^|;)i($|;)', peptidesfilter$protein.group.ids),] i=i+1 }
i have 2 problems, main 1 in grep1 function not recognized variable , how can re-name filtered data in way contain variable.
any ideas?
for grepl problem can use paste0
example:
paste0('(^|;)',i,'($|;)')
for loop , can :
ll <- lapply(seq(1:4),function(x) peptidesfilter[grepl(paste0('(^|;)',x,'($|;)'), peptidesfilter$protein.group.ids),])
then can transform data.frame:
do.call(rbind,ll) sequence first.amino.acid protein.group.ids 1 aaaaaiggr 1 2 aaaaaiggrpnyygneggr 1;2;4 21 aaaaaiggrpnyygneggr 1;2;4 3 aaaaassnpgggpemvr 2;5 4 aaaaavggr 3 22 aaaaaiggrpnyygneggr 1;2;4
Comments
Post a Comment