r - [R - 在跳过不完整行的多个列中的行,如何删除最小值和最大值

我花了一个夏天收集了一些数据,我试图用一种简单的方法来分析,

每一行都是一个参与者,其中包含参与者ID ,

它需要能够遍历所有的行,

这就是数据框(eumR )的样子。

> eumR town id eumr1 eumr2 eumr3 eumr4 eumr5 1 WLS SA4001 NA NA NA NA NA 2 WLS SA4002 NA NA NA NA NA 3 WLS SA4003 NA NA NA NA NA 4 WLS SA4004 NA NA NA NA NA 5 WLS SA4005 NA NA NA NA NA .... 80 BVL SA4080 12.6 15.2 13.6 14.9 14.2 81 BVL SA4081 15.2 16.5 16.5 17.3 20.5 82 BVL SA4082 15.1 14.8 14.5 14.6 16.7 83 BVL SA4083 15.6 15.8 15.8 15.5 13.2 84 BVL SA4084 15.7 15.8 17.3 17.9 20.8

时间:

你可以尝试以下操作:


eumR <- eumR[complete.cases(eumR),]



eumR$mean <- apply(eumR, 1, function(x) {


 x <- as.numeric(x[3:7])


 mean(x[!(x %in% c(min(x),max(x)))])})



以下是tidyverse方法:


library(tidyverse) 



eumR %>% 


 pivot_longer(-c(town,id), names_to ="variable", values_to ="value") %>%


 group_by(id) %>%


 filter( any(!is.na(value)) & value != min(value) & value != max(value)) %>%


 mutate(id2 = paste0("eumr", 1:3)) %>%


 ungroup() %>%


 select(-variable) %>%


 pivot_wider(names_from = id2, values_from = value) %>%


 mutate(mean = rowMeans(select(.,contains("eumr"))))



# town id eumr1 eumr2 eumr3 mean


# <fct> <fct> <dbl> <dbl> <dbl> <dbl>


#1 BVL SA4080 13.6 14.9 14.2 14.2


#2 BVL SA4081 16.5 16.5 17.3 16.8


#3 BVL SA4082 15.1 14.8 14.6 14.8



数据:


eumR <- structure(list(town = structure(c(2L, 2L, 2L, 2L, 2L, 1L, 1L, 


1L), .Label = c("BVL","WLS"), class ="factor"), id = structure(1:8, .Label = c("SA4001", 


"SA4002","SA4003","SA4004","SA4005","SA4080","SA4081","SA4082"


), class ="factor"), eumr1 = c(NA, NA, NA, NA, NA, 12.6, 15.2, 


15.1), eumr2 = c(NA, NA, NA, NA, NA, 15.2, 16.5, 14.8), eumr3 = c(NA, 


NA, NA, NA, NA, 13.6, 16.5, 14.5), eumr4 = c(NA, NA, NA, NA, 


NA, 14.9, 17.3, 14.6), eumr5 = c(NA, NA, NA, NA, NA, 14.2, 20.5, 


16.7)), class ="data.frame", row.names = c("1","2","3","4", 


"5","80","81","82"))



...