I spend sometime try to figure out how to mutate NA
values on multiple rows on row perspective in tibble
, the tibble
has 3 observations and 6 variables, generate below:
df <- data.frame(ID = c(1, 2, 3),
Score1 = c(90, 80, 70),
Score2 = c(66, 78, 86),
Score3 = c(NA, 86, 96),
Score4 = c(84, 76, 72),
Score5 = c(92, NA, 74))
sample_tibble <- as_tibble(df)
The tibble
looks as
# A tibble: 3 x 6
ID Score1 Score2 Score3 Score4 Score5
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 1 90 66 NA 84 92
2 2 80 78 86 76 NA
3 3 70 86 96 72 74
I have to use functions from tidyverse
(e.g mutate
, mutate_at
, rowwise
.. etc.), the target is to replace the NA
on row 1 (in Score3
column) and row 2 (in Score5
column) with the mean
of row 1 and row 2 respectively (mean
calculated with other values on row rather than NA
), so the ideal result should be after mutate
# A tibble: 3 x 6
ID Score1 Score2 Score3 Score4 Score5
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 1 90 66 83 84 92
2 2 80 78 86 76 80
3 3 70 86 96 72 74
The first NA
replace by mean(c(90, 66, NA, 84, 92), na.rm = TRUE)
as 83
The second NA
replace by mean(c(80, 78, 86, 76, NA), na.rm = TRUE)
as 80
Tried some code like below, and also check previous doc as Apply a function to every row of a matrix or a data frame or dplyr - using mutate() like rowmeans(), but the code never work since I am able to figure out body of mutate
function
sample_tibble[, -1] %>% rowwise() %>% mutate(...)
Not limited on rowwise
or mutate
(such as mutate_at
also good), is there any solution able to mutate row 1 and row 2 to reach the target format (Its great to mutate at same time, not as use for loop
to mutate twice), appreciate any solutions !