0

I have dataframe like this:

enter image description here

I want to create a new column which is the sum of other columns by ignoring NA if there is any numeric value in a row. But if all value (like the second row) in a row are na, the sum column gets NA.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Sh B
  • 21
  • 4

3 Answers3

1

Let's say that your data frame is called df

cbind(df, apply(df, 1, function(x){if (all(is.na(x))) {NA} else {sum(x, na.rm = T)}))

Note that if your data frame has other columns, you will need to restrict the df call within apply to only be the columns you're after.

Daniel V
  • 1,305
  • 7
  • 23
1

As this is your first activity here on SO you should have a look to this which describes how a minimal and reproducible examples is made. This is certainly needed in the future, if you have more questions. An image is mostly not accepted as a starting point.

Fortunately your table was a small one. I turned it into a tribble and then used rowSums to calculate the numbers you seem to want.

df <- tibble::tribble(
  ~x, ~y, ~z,
  6000, NA, NA,
  NA, NA, NA,
  100, 7000, 1000,
  0, 0, NA
)

df$sum <- rowSums(df, na.rm = T)
df
#> # A tibble: 4 x 4
#>       x     y     z   sum
#>   <dbl> <dbl> <dbl> <dbl>
#> 1  6000    NA    NA  6000
#> 2    NA    NA    NA     0
#> 3   100  7000  1000  8100
#> 4     0     0    NA     0

Created on 2020-06-15 by the reprex package (v0.3.0)

MarBlo
  • 4,195
  • 1
  • 13
  • 27
0

You can count the NA values in df. If in a row there is no non-NA value you can assign output as NA or calculate row-wise sum otherwise using rowSums.

ifelse(rowSums(!is.na(df)) == 0, NA, rowSums(df, na.rm = TRUE))
#[1]  6000    NA 10000  8100     0

data

df <- structure(list(x = c(6000, NA, 10000, 100, 0), y = c(NA, NA, 
NA, 7000, 0), z = c(NA, NA, NA, 1000, NA)), class = "data.frame", 
row.names = c(NA, -5L))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213