简体   繁体   中英

adding multiple columns include na in dataframe in r

I have dataframe like this:

在此处输入图像描述

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 .

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.

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)

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM