I need to create a new column which is a function of two or three other columns, one of which contains some missing data (NAs). However, when I use dplyr
's mutate
function, the new column contains all NAs.
See the example below:
rand_df <- data.frame(replicate(10,sample(0:10,200,rep=TRUE))) # random df
names(rand_df) <- letters[seq(from=1, to=10)] #renaming header
rand_df$c[2:20] <- NA # introducing NAs
head(rand_df)
a b c d e f g h i j
1 3 1 8 2 4 3 1 9 2 9
2 6 1 NA 1 2 8 8 6 0 9
3 5 7 NA 2 4 1 7 7 3 0
4 10 8 NA 6 6 7 0 2 2 0
5 4 1 NA 9 3 8 2 2 5 2
6 10 8 NA 3 10 2 10 4 5 5
Trying to create a new column
rand_df <- rand_df %>% mutate(k = 141 * min((c/88.42), 1))
head(rand_df):
a b c d e f g h i j k
1 3 1 8 2 4 3 1 9 2 9 NA
2 6 1 NA 1 2 8 8 6 0 9 NA
3 5 7 NA 2 4 1 7 7 3 0 NA
4 10 8 NA 6 6 7 0 2 2 0 NA
5 4 1 NA 9 3 8 2 2 5 2 NA
6 10 8 NA 3 10 2 10 4 5 5 NA
I know I could simply use a for loop to cycle through rows individually and skip those containing NAs, but I would like to think there's a better way to do this.