I would like to reorder the position of NA in a column, within each level of another categorical variable. For example, with this data frame:
df <- data.frame(fact=c(1,1,1,2,2,2), id=rep(1:6), value=c(NA,44,23,NA,NA,76))
I would like to change a new column, for example:
df$newvar <= c(44,23,NA,76,NA,NA)
I would think the following would work, but it is not:
dfb <- df %>% group_by(fact) %>% mutate(newvar = df$value[order(is.na(df$value))])
Any ideas on how I can do this?
source
share