2

I am somewhat of an R newbie, am struggling with writing code for what seems like simple logic, and would appreciate any help! I am trying to impute a constant value of 1 for NA cells in each row of my data set but only for rows that have 2 or less NA cells. Ultimately, I will also be computing a new column with row-wise means after imputation. If one line of code code automagically achieve all of these things, that would be great!

Here is an example data set to work with.

tData <- data.frame(subID=c(1001,1002,1003,1004),
b1=c(1,1,2,NA),
b2=c(NA,1,1,NA),
b3=c(NA,2,2,NA),
b4=c(2,NA,1,NA))

I have been looking at various base and dplyr code examples but am riding the struggle bus.

kaho
  • 23
  • 3

2 Answers2

2

You can do this in these two lines.

tData[is.na(tData) & rowSums(is.na(tData)) <= 2] <- 1
tData |> cbind(row_means=rowMeans(tData[-1]))
#   subID b1 b2 b3 b4 row_means
# 1  1001  1  1  1  2      1.25
# 2  1002  1  1  2  1      1.25
# 3  1003  2  1  2  1      1.50
# 4  1004 NA NA NA NA        NA

Data:

tData <- structure(list(subID = c(1001, 1002, 1003, 1004), b1 = c(1, 1, 
2, NA), b2 = c(NA, 1, 1, NA), b3 = c(NA, 2, 2, NA), b4 = c(2, 
NA, 1, NA)), class = "data.frame", row.names = c(NA, -4L))
jay.sf
  • 60,139
  • 8
  • 53
  • 110
0

We can do this like this:

library(dplyr)

tData %>% 
  mutate(across(-subID, ~ifelse(rowSums(is.na(tData[2:5])) <= 2 & is.na(.), 1, .))) %>%
  rowwise() %>%
  mutate(mean_value = mean(c_across(-subID), na.rm = TRUE))
 subID    b1    b2    b3    b4 mean_value
  <dbl> <dbl> <dbl> <dbl> <dbl>      <dbl>
1  1001     1     1     1     2       1.25
2  1002     1     1     2     1       1.25
3  1003     2     1     2     1       1.5 
4  1004    NA    NA    NA    NA     NaN  
TarJae
  • 72,363
  • 6
  • 19
  • 66
  • 1
    Also, can you help me understand the function of the tilde (~) before ifelse in this case? – kaho Jul 23 '23 at 14:04
  • Sure. First start here with time you will learn more about anonymous functions and tilde sign. You just need time. – TarJae Jul 23 '23 at 14:08