我有以下代码。我想找到具有字母数字值的单元格,它还应该忽略 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 及其变体。但是“空部分不起作用。我应该做些什么改变?