Create a data frame from an existing data frame with some condition.

I am looking for an easy way to frame NA's in my dataset:

Gender Age
M 20
M 21
F 30
F 26
F 29

I have to frame different age mean for each gender. Unfortunately I see no possibility to do it without creating new column and then putting in again into the dataset. Even I tried below codes but it is not working:

mans <- train[train[, "Gender"]=="male", ]
womens <- train[train[, "Gender"]=="female", ]
mf <- mean(train$Age, na.rm = TRUE)  
mm <- mean(train$Age, na.rm = TRUE)
train[train$Age == NA & train$Gender == "male", "Age"] <- mf

Error in '[<`, mans$Age == NA & mans$Sex == "male",
missing values are not allowed in subscripted assignments of data frames

Any suggestion?
Thanks in advance.



  • In this way you can find mean age of each Gender

    dataf_old <- data.frame(Gender = c(rep("M", 500), rep("F", 500)),
    Age = round(c(rnorm(500, 35, 2), rnorm(500, 25, 2))))
    dataf_NA <- data.frame(Gender = sample(c("M", "F"), 10, rep = T),
    Age = NA)

    dataf_old %>%
    group_by(Gender) %>%
    summarise(Mean = mean(Age))

Sign In or Register to comment.