Reputation: 6796
I have below code. I want to find cells that have alphanumeric values and it should also ignore the cells that are na or NA.
How can I modify my code to that? the required R command should return below result for newcolumn
True, True, false, false, True, false, false
I tried commands 3 and 4, but they failed :(
> 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)]
I updated my code as above as i want to recognize na, nan, null and their variants too. But "null part is not working. What changes should i make?
Upvotes: 0
Views: 2056
Reputation: 887881
Try:
grepl("(?=(?i)na(*SKIP)(*F))|[[:alnum:]]", newcolumn, perl=TRUE)
#[1] TRUE TRUE FALSE FALSE TRUE FALSE FALSE
(?i)
means case-insensitive. So it should match na
, NA
, nA
, or Na
. (*SKIP)(*F)
in the pattern makes the match to fail. Now the pattern in the right side of |
symbol ie. [[:alnum:]]
would be the one that would be matched.
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
Upvotes: 1