0

我有以下代码。我想找到具有字母数字值的单元格,它还应该忽略 na 或 NA 的单元格。

我怎样才能修改我的代码呢?所需的 R 命令应返回以下新列的结果

真,真,假,假,真,假,假

我尝试了命令 3 和 4,但它们失败了:(

> newcolumn=c(1,2,"na","NA","abc","","*")

> grepl("[[:alnum:]]", newcolumn)
[1]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE

> grepl("[[:alnum:]] | na", newcolumn)
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE

> grepl(c("[[:alnum:]]","na"), newcolumn)
[1]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE
Warning message:
In grepl(c("[[:alnum:]]", "na"), newcolumn) :
  argument 'pattern' has length > 1 and only the first element will be used

> grepl("[[:alnum:]]" | "na" | "NA", newcolumn)
Error in "[[:alnum:]]" | "na" : 
  operations are possible only for numeric, logical or complex types

> str(newcolumn)
 chr [1:7] "1" "2" "na" "NA" "abc" "" "*"

============================update1====================== =========

newcolumn2<-newcolumn[grepl("(?=(?i)na(N)?(*SKIP)(*F))|[[:alnum:]]|(?=(?i)nan(*SKIP)(*F))|(?=(?i)null(*SKIP)(*F))", newcolumn, perl=TRUE)]

我如上所述更新了我的代码,因为我也想识别 na、nan、null 及其变体。但是“空部分不起作用。我应该做些什么改变?

4

1 回答 1

1

尝试:

 grepl("(?=(?i)na(*SKIP)(*F))|[[:alnum:]]", newcolumn, perl=TRUE)
 #[1]  TRUE  TRUE FALSE FALSE  TRUE FALSE FALSE

(?i)表示不区分大小写。所以它应该匹配na, NA, nA, 或Na(*SKIP)(*F)在模式中使匹配失败。|现在符号右侧的图案即。[[:alnum:]]将是匹配的那个。

更新

 newcolumn <- c(1,2,"na","NA","abc","","*", "NaN", "nan", "nAn")
 grepl("(?i)na(N)?(*SKIP)(*F)|[[:alnum:]]", newcolumn, perl=TRUE)
 # [1]  TRUE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
于 2014-10-06T18:44:00.233 回答