Reputation: 267
I want to delete the rows in my data frame where the var1
or var2
has a digit.
I've tried with different variations of regex but none of them seem to work.
Below is a simplified example looking for digits on var1
.
var1<-c("000","1","1","1","1","000256","1","shall","1","1","1","1","the","1",
"001","1","1","1","1","one")
var2<-c("people","0","00","000","1","2","3","begin","4","5","6","7","8","a",
"and","billion","hour","in","is","million")
val<-c(1639,1703,655,3542,3273,9658,2562,1027,3340,2236,971,783,1057,673,1658,
1367,843,1921,459,2589)
df<-data.frame(var1,var2,val)
df<-subset(df,df$var1!="[0-9]*")
What am I doing wrong?
Upvotes: 2
Views: 1953
Reputation: 21621
Same idea as lukeA but using filter()
from dplyr
library(dplyr)
df %>% filter(!grepl("[[:digit:]]", var1) & !grepl("[[:digit:]]", var2))
or slice()
df %>% slice(which(!grepl("[[:digit:]]", var1) & !grepl("[[:digit:]]", var2)))
or as per @akrun suggestion:
df %>% slice(intersect(grep("[^0-9]", var1), grep("[^0-9]", var2)))
Which gives:
# var1 var2 val
#1 shall begin 1027
#2 one million 2589
Upvotes: 2
Reputation: 54237
Try this
subset(df, !grepl("[0-9]", var1) & !grepl("[0-9]", var2))
# var1 var2 val
# 8 shall begin 1027
# 20 one million 2589
Upvotes: 3