简体   繁体   English

在 r 中添加多列包括 dataframe 中的 na

[英]adding multiple columns include na in dataframe in r

I have dataframe like this:我有这样的 dataframe :

在此处输入图像描述

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.如果一行中有任何数值,我想通过忽略NA创建一个新列,该列是其他列的总和。 But if all value (like the second row) in a row are na, the sum column gets NA .但是,如果一行中的所有值(如第二行)都是 na,则 sum 列将获得NA

Let's say that your data frame is called df假设您的数据框称为 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.请注意,如果您的数据框有其他列,则需要将df调用限制为仅apply于您所追求的列。

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.由于这是您在 SO 上的第一个活动,您应该看一下它,它描述了如何制作最小且可重现的示例。 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.我把它变成了一个 tribble,然后使用rowSums来计算你想要的数字。

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)reprex package (v0.3.0) 于 2020 年 6 月 15 日创建

You can count the NA values in df .您可以计算df中的NA值。 If in a row there is no non- NA value you can assign output as NA or calculate row-wise sum otherwise using rowSums .如果连续没有非NA值,您可以将 output 分配为NA或使用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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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