使用dplyr添加具有NAs和R的平均值的列

sac*_*inv 13 r dplyr

我有一个数据框,我需要添加另一列,它显示该行的所有其他列中的NA的数量以及非NA值的平均值.我认为可以在dplyr中完成.

> df1 <- data.frame(a = 1:5, b = c(1,2,NA,4,NA), c = c(NA,2,3,NA,NA))
> df1
  a  b  c
1 1  1 NA
2 2  2  2
3 3 NA  3
4 4  4 NA
5 5 NA NA
Run Code Online (Sandbox Code Playgroud)

我想改变另一个列,该列计算该行中的NA数量,另一列显示该行中所有NON-NA值的平均值.

请协助.

mal*_*atr 15

library(dplyr)

count_na <- function(x) sum(is.na(x))

df1 %>%
  mutate(means = rowMeans(., na.rm = T),
         count_na = apply(., 1, count_na))
Run Code Online (Sandbox Code Playgroud)


Jer*_*y T 9

如此处所述/sf/answers/2641244861/

df1 <- data.frame(a = 1:5, b = c(1,2,NA,4,NA), c = c(NA,2,3,NA,NA))

df1 %>%
  mutate(means = rowMeans(., na.rm = T),
         count_na = rowSums(is.na(.)))
Run Code Online (Sandbox Code Playgroud)

在选定的列上工作(此处的示例适用于列a和列c):

df1 %>%
  mutate(means = rowMeans(., na.rm = T),
       count_na = rowSums(is.na(select(.,one_of(c('a','c'))))))
Run Code Online (Sandbox Code Playgroud)


小智 8

你可以试试这个:

#Find the row mean and add it to a new column in the dataframe
df1$Mean <- rowMeans(df1, na.rm = TRUE)

#Find the count of NA and add it to a new column in the dataframe
df1$CountNa <- rowSums(apply(is.na(df1), 2, as.numeric))
Run Code Online (Sandbox Code Playgroud)