Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

R data suppression that ignores non-integers

I am trying to suppress all values in my data frame that are less than 5.
data[data < 5] <- NA
For the most part this works fine. However I also have some values that are categorical age groups "0-19" "20-24" and so on. I guess R is doing the subtraction and removing these as well. Is there a way to do this that ignores any value that isn't an integer?

Edit: Some dummy data as an example:

dummydata<-as.data.frame(rbind(c('0-19','4','5'),c('20-24','6','1')))
> dummydata
     [,1]    [,2] [,3]
[1,] "0-19"  "4"  "5" 
[2,] "20-24" "6"  "1"   
dummydata[dummydata < 5] <- NA
> dummydata
     [,1] [,2] [,3]
[1,] NA   NA   "5" 
[2,] NA   "6"  NA  

Desired output:

     [,1]    [,2] [,3]
[1,] "0-19"  NA  "5" 
[2,] "20-24" "6"  NA 
question from:https://stackoverflow.com/questions/66058399/r-data-suppression-that-ignores-non-integers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

We can first find out values that have only numbers in them (no '-'), turn them to numeric and if it is less than 5 change to NA.

dummydata[] <- lapply(dummydata, function(x) {
                     tmp <- grepl('^\d+$', x)
                     x[tmp][as.numeric(x[tmp]) < 5] <- NA
                     x
               })

dummydata

#     V1   V2   V3
#1  0-19 <NA>    5
#2 20-24    6 <NA>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...