0

Here is the data set, say name is DS.

       Abc    Def   Ghi
1      41     190   67
2      36     118   72
3      12     149   74
4      18     313   62
5      NA      NA   56
6      28      NA   66
7      23     299   65
8      19      99   59
9       8      19   61
10     NA     194   69

How to get a new dataset DSS where value of column Abc is greater than 25, and value of column Def is greater than 100.It should also ignore any row if value of atleast one column in NA.

I have tried few options but wasn't successful. Your help is appreciated.

1
  • 1
    na.omit(DS[with(DS, Abc > 20 & Def > 100), ]) Commented Jan 15, 2015 at 11:58

1 Answer 1

3

There are multiple ways of doing it. I have given 5 methods, and the first 4 methods are faster than the subset function.

R Code:

# Method 1:
DS_Filtered <- na.omit(DS[(DS$Abc > 20 & DS$Def > 100), ]) 
# Method 2: which function also ignores NA
DS_Filtered <- DS[ which( DS$Abc > 20 & DS$Def > 100) , ]
# Method 3:
DS_Filtered <- na.omit(DS[(DS$Abc > 20) & (DS$Def >100), ])

# Method 4: using dplyr package
DS_Filtered <- filter(DS, DS$Abc > 20, DS$Def >100)
DS_Filtered <- DS %>% filter(DS$Abc > 20 & DS$Def >100)

# Method 5: Subset function by default ignores NA
DS_Filtered <- subset(DS, DS$Abc >20 & DS$Def > 100) 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.