0

lets say data is 'ab':

a <- c(1,2,3,NA,5,NA)
b <- c(5,NA,4,NA,NA,6)
ab <-c(a,b)
  1. I would like to have new variable which is sum of the two but keeping NA's as follows: desired output:
ab$c <-(6,2,7,NA,5,6)

so addition of number + NA should equal number

I tried following but does not work as desired:

ab$c <- a+b

gives me : 6 NA 7 NA NA NA

Also don't know how to include "na.rm=TRUE", something I was trying.

  1. I would also like to create third variable as categorical based on cutoff <=4 then event 1, otherwise 0: desired output:
ab$d <-(1,1,1,NA,0,0)

I tried:

ab$d =ifelse(ab$a<=4|ab$b<=4,1,0)
print(ab$d)

gives me logical(0)

Thanks!

Phil
  • 7,287
  • 3
  • 36
  • 66
nnet
  • 39
  • 4

1 Answers1

0
a <- c(1,2,3,NA,5,NA)
b <- c(5,NA,4,NA,NA,6)

dfd <- data.frame(a,b)

dfd$c <- rowSums(dfd, na.rm = TRUE)
dfd$c <- ifelse(is.na(dfd$a) & is.na(dfd$b), NA_integer_, dfd$c)
dfd$d <- ifelse(dfd$c >= 4, 1, 0)
dfd

   a  b  c  d
1  1  5  6  1
2  2 NA  2  0
3  3  4  7  1
4 NA NA NA NA
5  5 NA  5  1
6 NA  6  6  1
Phil
  • 7,287
  • 3
  • 36
  • 66
  • Thanks! this is working and the solution. However, how to select specific columns only for rowsums statement when we have other columns that need not be selected in this sum i.e. specify which columns to add? – nnet Jan 23 '23 at 05:31
  • This works for the above comment. ```dfd$c<- rowSums(dfd[, c(1, 2)], na.rm=TRUE)``` ```dfd$c <- ifelse(is.na(dfd$a) & is.na(dfd$b), NA_integer_, dfd$c)``` – nnet Jan 23 '23 at 05:38