简体   繁体   English

在R中使用grepl计算要匹配的模式字符串的数量

[英]Counting the number of pattern strings to match using grepl in R

I've written a script related to data analysis and I'm trying to make it as flexible as possible so that it will be useful to multiple teams that store/use their data in different ways. 我已经编写了一个与数据分析有关的脚本,并试图使其尽可能地灵活,以便对以不同方式存储/使用其数据的多个团队有用。 I'm also trying to make it as simple to use as possible because I know people using it won't be as adept at R as I am. 我还试图使它尽可能简单,因为我知道使用它的人不会像我那样擅长R。 I have a couple of user-defined variables that must be changed before use: 我有几个用户定义的变量,在使用前必须进行更改:

headers = "B|M1|M2|M3"
keepscore = 3

I use this information to remove columns of unnecessary data by finding the strings in "headers" and keeping only those columns: 我使用此信息通过在“标头”中查找字符串并仅保留那些列来删除不必要的数据列:

x = x[ ,grepl(headers, names(x))]

"M1", "M2", and "M3" are columns that contain data to keep for analysis, but the number of these columns is variable dependent on the team. “ M1”,“ M2”和“ M3”是包含要保留用于分析的数据的列,但是这些列的数量取决于团队而有所不同。 "keepscore" is the number of those columns. “ keepscore”是这些列的数量。 Rather than having to have the user enter "keepscore" = 3, what I want to do is have the script extrapolate how many search strings there are in headers. 我要做的不是让用户输入“ keepscore” = 3,而是让脚本推断出标头中有多少个搜索字符串。 The number of strings is 4, so keepscore should be 4 - 1 = 3. So, is there a way to count the number of search strings in "headers"? 字符串的数量为4,因此keepscore应该为4-1 =3。那么,有没有办法计算“标头”中搜索字符串的数量?

Are you looking for this : 您在寻找这个吗:

length(unlist(strsplit(headers,'|',fixed=TRUE)))-1

## [1] 3

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

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