r - Subtract multiple columns ignoring NA -
I am quite new to R and have run into an issue with NA. The answer to this question might be somewhere else but I could not find the answer. I'm trying to do the opposite of In other words, I want to do the same thing as NA, how Any help is greatly appreciated. just use rowSums () from which I'm trying to subtract
x2 and
x3 from
X1 < To generate / code>
x4 without the NA, I am currently using the code as follows:
& gt; X & LT; - data.frame (x1 = 3, x2 = c (4: 1, 2: 5), x3 = c (1, na)) gt; X $ x4 = x $ x1-x $ x2-x $ x3 & gt; X x 1 x 2 x 3 x 4 1 3 4 1 -2 2 3 3 NA 3 3 2 1 0 4 3 1 NA NA 5 3 2 1 0 6 3 3 NA 7 7 3 4 1 -2 8 3 5 NA NA
rowSums is passed to
na.rm = TRUE argument So that I can get this result:
x1x2x3x4 1 3 4 1 -2 2 3 3 NA 3 3 2 1 0 4 3 1 NA 2 5 3 2 1 0 6 3 3 NA 0 7 3 4 1 - 2 8 3 5 NA -2
rowSums :
< Code> & gt; X $ x4 & lt; - x $ x1 - rows (x [, 2: 3], na.rm = TRUE) & gt; Xx1x2x3x4 1 3 4 1 -2 2 3 3 NA 3 3 2 1 0 4 3 1 NA 2 5 3 2 1 0 6 3 3 NA 0 7 3 4 1 -2 8 3 5 NA -2
Comments
Post a Comment