vendredi 13 avril 2018

how to impute missing values for different values in a column

I have a df:

Context <- c(HUM, HUM, DEV, HUM, DEV, HUM, DEV)
Amount <- c(100, 150, NA, NA, 500, 150, 600)

What I am interest in is imputing the missing value for when Context = DEV and When Context = HUM. So I want to inpute 2 different values in Context.

I have tried making an "if function," but something doesn't really work.

First I found the average for HUM and DEV in Context:

df %>%
  group_by(Context) %>%
  summarise(mean_amount = mean(Amount, na.rm = TRUE))

I then assigned the mean value for HUM and Dev

mean_hum <- 133
mean_dev <- 550

Then to impute a value for when Context = DEV and When Context = HUM:

df$impute_amount <- df %>%
  if (Context == "HUM") {
  ifelse(is.na(df$Amount), mean_hum, df$Amount)
  }if (Context == "Dev"){
    ifelse(is.na(df$Amount), mean_dev, 
df$Amount)
  }

However, i get the message: Error: unexpected '}' in " }"

Where am i going wrong??

I hope that someone can help me move on from here.

Thank you!

Aucun commentaire:

Enregistrer un commentaire