Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Subset data in R where cells values are in a certain range – R

I have large data like the following:

Age_group_1<-c(18,18,18,21,21,21,30,36,36,42,42)
Age_group_2<-c(15,15,17,19,19,21,25,25,25,33,33)
Age_group_3<-c(12,12,13,13,22,22,25,25,27,27,30)
Age_group_4<-c(8,10,15,15,25,25,28,28,30,30,30)

Mydata<-data.frame(Age_group_1,Age_group_2,Age_group_3,Age_group_4)

> Mydata
   Age_group_1 Age_group_2 Age_group_3 Age_group_4
1           18          15          12           8
2           18          15          12          10
3           18          17          13          15
4           21          19          13          15
5           21          19          22          25
6           21          21          22          25
7           30          25          25          28
8           36          25          25          28
9           36          25          27          30
10          42          33          27          30
11          42          33          30          30

I want to remove values that are less then 18 and values that are over 35.
I have tried to use subset, but I dont get the result that I’m looking for

New_data<-na.omit(subset(Mydata,(18<=Mydata & Mydata <= 35)))

This is the result that I’m looking:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>New_data
          Age_group_1 Age_group_2 Age_group_3 Age_group_4
1           18          NA          NA          NA
2           18          NA          NA          NA
3           18          NA          NA          NA
4           21          19          NA          NA
5           21          19          22          25
6           21          21          22          25
7           30          25          25          28
8           NA          25          25          28
9           NA          25          27          30
10          NA          33          27          30
11          NA          33          30          30

Any suggestion on how I can achieve the end result?

>Solution :

You want this:

Mydata[18 > Mydata | 35 < Mydata] <- NA

#    Age_group_1 Age_group_2 Age_group_3 Age_group_4
# 1           18          NA          NA          NA
# 2           18          NA          NA          NA
# 3           18          NA          NA          NA
# 4           21          19          NA          NA
# 5           21          19          22          25
# 6           21          21          22          25
# 7           30          25          25          28
# 8           NA          25          25          28
# 9           NA          25          27          30
# 10          NA          33          27          30
# 11          NA          33          30          30
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading