简体   繁体   English

使用 grepl R 子集列表

[英]Subset a list with grepl R

I'm trying to subset a list which contains dataframes:我正在尝试对包含数据框的列表进行子集化:

Folkersen_IL6.txt
Folkersen_IL16.txt
Folkersen_IL1.txt
Height.txt
Met_Ile.out.txt 
etc

I've tried this with the following code:我已经使用以下代码进行了尝试:

protein <- list.txt[grepl('Folkersen', list.txt)]

This line only returns me a list of 1 (containing Folkersen_IL16.txt).此行仅返回 1 的列表(包含 Folkersen_IL16.txt)。 How can I get a subset containing all 3 desired dataframes?如何获得包含所有 3 个所需数据帧的子集?

Try grepping the names of the list:尝试 grepping 列表的名称:

protein <- list.txt[grepl('Folkersen', names(list.txt))]

If the names of your list are not currently the data frame names, then just alter your script to populate your list using the data frame names as you add each element.如果列表的名称当前不是数据框名称,则只需更改脚本以在添加每个元素时使用数据框名称填充列表。

Another option is startsWith另一种选择是startsWith

protein <- list.txt[startsWith(names(list.txt), 'Folkersen')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM