First of all, let me say this community is very helpful. As for my questions:

I have some data with likert responses 1-4 with 8 as I don't know and 9 as NA.

I was able to replace my 8s and 9s with NAs using:

df %>% mutate_all(~ifelse(.x %in% c(8, 9), NA, .x))

and did that for each variable.

My problem now is that I have good looking data with NAs, but I am trying to convert groups of variables into continuous scale items. For instance, ir1, ir2, and ir3, have Likert responses 1-4 to add up to ir_scale ranging from 0 - 12. The issue is when I add them:

irscale <- df$ir1 + df$ir2 + df$ir3

Any row that had an NA returns NA for the sum. e.g. 1+3+NA = NA , I want it to return 4.

I can't turn NAs into 0 as I am going to use regressions later. Thoughts? Here is an example data set:

https://drive.google.com/file/d/1HwtjJ-nChRwVuVQhAYucB_JkUagVtByj/view?usp=sharing

2 Answers

1
akrun On

One option is rowSums, which can take care of the NA with na.rm = TRUE

df %>%
      mutate(irscale = rowSums(.[paste0("ir", 1:3)], na.rm = TRUE))

Or using base R

irscale <- rowSums(df[paste0("ir", 1:3)], na.rm = TRUE)
1
MatthewR On

You can use rowSums for this. na.rm=T is a common parameter for dealing with NAs

mtcars[ 3, 3] <- NA
mtcars$newvar <- rowSums( mtcars[ , 3:5], na.rm=T)