【问题标题】:R: Remove rows based on columns containing ? in the stringR:根据包含的列删除行?在字符串中
【发布时间】:2022-11-03 01:47:29
【问题描述】:
MarkerName Allele1 Allele2 Weight Zscore P-value Direction
10:1167075 a g 218.00 2.446 0.01446 ?+
7:77652992 t c 218.00 2.076 0.03789 ?-
X:24811075 a g 315.00 2.463 0.01378 +?
4:15645706 t c 315.00 2.582 0.009817 -?
5:13478320 g a 315.00 2.872 0.00222 ++

我正在尝试使用这种格式对数据框进行子集化,以删除所有包含 ? 的行。我遇到的问题是 +/- 符号被识别为运算符符号,R 给我以下错误:

错误:无效的正则表达式“?+”,原因“重复运算符的使用无效”

我的目标是拥有一个如下所示的数据框:

MarkerName Allele1 Allele2 Weight Zscore P-value Direction
5:13478320 g a 315.00 2.872 0.002 ++

【问题讨论】:

  • ? 是正则表达式中的保留字符,表示前一个字符/组是可选的(0 或 1)。您可以转义它"\\?" 或使用fixed=TRUEstackoverflow.com/a/22944075/3358272 是一个很好的参考。

标签: r


【解决方案1】:

这应该这样做:

df %>% filter(str_detect(Direction,'\?', negate = T))

例子:

d = tibble(a = 1:3, b = c('+?', '?+', '++'))

      a b    
  <int> <chr>
1     1 +?   
2     2 ?+   
3     3 ++ 


d %>% filter(str_detect(b,'\?', negate = T))


      a b    
  <int> <chr>
1     3 ++

【讨论】:

    猜你喜欢
    • 2014-04-10
    • 2015-07-04
    • 2019-03-15
    • 1970-01-01
    • 2019-04-25
    相关资源
    最近更新 更多