[英]R: grepl select first charachter on a string
I apologize in advance, this might be a repeat question. 我事先表示歉意,这可能是一个重复的问题。 However, I just spent the two last hours over stackoverflow, and can't seem to find a solution. 但是,我只花了两个小时来解决stackoverflow问题,似乎找不到解决方案。
I want to use grepl
to detect rows that begin with a digit, that's what I tried to use but It didn't give me the rigt answer: 我想使用grepl
来检测以数字开头的行,这是我尝试使用的方法,但是它并没有给我带来严格的答案:
grep.numeric=as.data.frame(grepl("^[:digit:]",df_mod$name))
I guess that the problem is from the regular expression "^[:digit:]"
, but I couldn't figure it out. 我想问题出在正则表达式"^[:digit:]"
,但是我无法弄清楚。
UPDATE 更新
My dataframe looks like this, It's huge, but below is an example: 我的数据框看起来像这样,它很大,但是下面是一个示例:
ID mark name
1 whatever name product
2 whatever 10 product
3 whatever 250 product
4 another_mark other product
I want to detect products which their names begin with a number. 我想检测名称以数字开头的产品。
UPDATE 2 更新2
applying grep.numeric=grepl("^[[:digit:]]",df_mod$name)
on the example below give me the right answer which is: 在下面的示例中应用grep.numeric=grepl("^[[:digit:]]",df_mod$name)
给我正确的答案是:
grep.numeric
[1] FALSE TRUE TRUE FALSE
But, what drive me crazy is when I pply this fuction to my real dataframe: 但是,令我抓狂的是当我将此功能应用于真实数据框时:
grep.numeric=grepl("^[[:digit:]]",df_mod[217,]$nom)
give me this result: 给我这个结果:
grep.numeric
[1] FALSE
But actually, what I have is this : 但是实际上,我有这个:
df_mod[217,]$nom
[1] 100 lipo 30 gélules
Please help me. 请帮我。
Apparently, some of your values have leading spaces, so you could either modify your regex to (or something similar) 显然,您的某些值前导空格,因此您可以将正则表达式修改为(或类似内容)
grepl("^\\s*[[:digit:]]", df_mod$name)
Or use the built in trimws
function 或使用内置的trimws
功能
grepl("^[[:digit:]]", trimws(df_mod$name))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.