Reputation: 3
I am trying to replace values of a column observation, given a set of conditions. The data is as given:
Sex Age
male 34.5
female NA
male 62
male NA
I am I want to replace the data where the sex is female and the age value is NA by a value.
I am currently using the for loop as:
for(i in 1:length(data$Sex){
if(data$Sex[i]=="male"){
if(is.na(data$Age[i])){
data$Age[i] <- 30.7
}
}
}
and this work perfectly fine. I was curious if I got it right or is there a computationally easier method to get this right.
Thank you in advance. :)
Upvotes: 0
Views: 41
Reputation: 4554
Try this:
data[data$Sex=='female' & is.na(data$Age),'Age']<-30.7
Upvotes: 0
Reputation: 326
Assuming you want to replace "female" as described in the text (rather than "male" as done in your code):
library(dplyr)
data %>%
mutate(Age = if_else(Sex == "female" & is.na(Age), 30.7, Age))
Output:
# A tibble: 4 x 2
Sex Age
<chr> <dbl>
1 male 34.5
2 female 30.7
3 male 62.0
4 male NA
Upvotes: 1