Add a column with count of NAs and Mean

前端 未结 3 1225
南笙
南笙 2020-11-30 12:46

I have a data frame and I need to add another column to it which shows the count of NAs in all the other columns for that row and also the mean of the non-NA values. I think

3条回答
  •  孤城傲影
    2020-11-30 12:50

    You can try this:

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

提交回复
热议问题