如何在loop-R中使用grepl函数中的变量
我是R新手,在循环和grepl函数方面有一些问题 我有一个类似的数据:如何在loop-R中使用grepl函数中的变量,r,loops,variables,R,Loops,Variables,我是R新手,在循环和grepl函数方面有一些问题 我有一个类似的数据: str(peptidesFilter) 'data.frame': 78389 obs. of 130 variables: $ Sequence : chr "AAAAAIGGR" "AAAAAIGGRPNYYGNEGGR" "AAAAASSNPGGGPEMVR" "AAAAAVGGR" ... $ First.amino.acid : c
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" ...
我想使用下面的grepl函数根据$Protein.group.IDs过滤数据
peptidesFilter.new <- peptidesFilter[grepl('(^|;)2($|;)',
peptidesFilter$Protein.group.IDs),]
peptidesFilter.new对于grepl问题,您可以使用paste0
例如:
paste0('(^|;)',i,'($|;)')
对于循环,您可以这样做:
ll <- lapply(seq(1:4),function(x)
peptidesFilter[grepl(paste0('(^|;)',x,'($|;)'),
peptidesFilter$Protein.group.IDs),])
对于grepl问题,您可以使用paste0
例如:
paste0('(^|;)',i,'($|;)')
对于循环,您可以这样做:
ll <- lapply(seq(1:4),function(x)
peptidesFilter[grepl(paste0('(^|;)',x,'($|;)'),
peptidesFilter$Protein.group.IDs),])
非常感谢!paste0(“(^ |)”,i“($)”)解决了grep1问题,我无法理解的是lapply,基本上我想基于ID分割数据,然后进一步处理每个分割的数据。例如,Sequence信息。do.call(rbind,ll),它再次合并所有数据。当x=1、x=2等时,是否可以创建单个数据帧(如肽过滤器X1、肽过滤器X2等)。。。还是我看错你了?@Samir我想名单就是你想要的。对不起,我现在明白了。非常感谢,非常感谢!paste0(“(^ |)”,i“($)”)解决了grep1问题,我无法理解的是lapply,基本上我想基于ID分割数据,然后进一步处理每个分割的数据。例如,Sequence信息。do.call(rbind,ll),它再次合并所有数据。当x=1、x=2等时,是否可以创建单个数据帧(如肽过滤器X1、肽过滤器X2等)。。。还是我看错你了?@Samir我想名单就是你想要的。对不起,我现在明白了。非常感谢