R:计算数据框中的行数,匹配字符位于字符串的指定位置
R: Count number of rows in data frame, with matching character in specified position of string
我有一个包含字符列的数据框:
strings
1 a;b;c;d
2 g;h;i;j
3 k;m
4 o
我想计算字符串(行)在字符串中特定位置的特定字符的数量。
例如
Get count of number of strings with 3rd character as one of the
characters in this set: {a,b,m}.
The output should be 2 in this case, since only the 1st and 3rd row
have any characters in {a,b,m} as their 3rd character within the
string.
我只能使用此代码查找任何包含 'b':
的字符串
sum(grepl("b",df))
但是,这对于上述任务来说还不够好。
请指教
试试这个:
sum(substr(df$strings,3,3) %in% c("a","b","m"))
或者,如果您想使用 ;
作为分隔符,您可以这样做:
sum(sapply(strsplit(df$strings,";"),function(x) x[2] %in% c("a","b","m")))
你可以试试grepl
:
x = c('a;b;c;d','g;h;i;j','k;m','o')
sum(grepl('^.{2}[abm]', x))
#[1] 2
我有一个包含字符列的数据框:
strings
1 a;b;c;d
2 g;h;i;j
3 k;m
4 o
我想计算字符串(行)在字符串中特定位置的特定字符的数量。
例如
Get count of number of strings with 3rd character as one of the characters in this set: {a,b,m}.
The output should be 2 in this case, since only the 1st and 3rd row have any characters in {a,b,m} as their 3rd character within the string.
我只能使用此代码查找任何包含 'b':
的字符串sum(grepl("b",df))
但是,这对于上述任务来说还不够好。 请指教
试试这个:
sum(substr(df$strings,3,3) %in% c("a","b","m"))
或者,如果您想使用 ;
作为分隔符,您可以这样做:
sum(sapply(strsplit(df$strings,";"),function(x) x[2] %in% c("a","b","m")))
你可以试试grepl
:
x = c('a;b;c;d','g;h;i;j','k;m','o')
sum(grepl('^.{2}[abm]', x))
#[1] 2