I have a data set with age and income variables. The income column has some missing values. I want to write a code where I can get all the values of age variable for all missing values of income variable only i.e. the result of my code would display the ages where income value is NA. I was trying to do this using dplyr function but I am stuck. I know this is simple but I have been unable to do this. Could anyone suggest the code for this. Thanks
Asked
Active
Viewed 40 times
-2
-
2Why don't you add your code so people can see it and offer suggestions? – pmac89 Apr 02 '17 at 22:13
-
dat%%>%filter(??)%>%select(age). I am stuck in the filter function part, I don't know what to write here if i want to filter the data set "dat" containing missing values of income. – Rookie Apr 02 '17 at 22:23
-
1When you ask a question present example data and code http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. `dat=data.frame(income=c(runif(5), rep(NA, 5)), age = c(rep(1:5, each=2)))` Then you can filter it `dat %>% filter(is.na(income))` – B Williams Apr 02 '17 at 22:25
-
Its simply `dat[is.na(dat$income),]`. No any package needed. – 989 Apr 02 '17 at 22:31
1 Answers
0
Some test data:
data<-data.frame(Age = c(23,28,27,31,42,26,48,23,32,31),
income = c(20000, NA, 30000, 25000, NA, NA, 35000, NA, 40000, 250000))
> data
Age income
1 23 20000
2 28 NA
3 27 30000
4 31 25000
5 42 NA
6 26 NA
7 48 35000
8 23 NA
9 32 40000
10 31 250000
Explicitly using the dplyr
package:
require(dplyr)
data %>% filter(is.na(income))
Age income
1 28 NA
2 42 NA
3 26 NA
4 23 NA

989
- 12,579
- 5
- 31
- 53

Andrew Haynes
- 2,612
- 2
- 20
- 35