计算每个组i数据帧的分位数并分配NA?

我编造了这个例子来解释我的问题:

 df= structure(list(group = structure(c(1L, 1L, 2L, 2L, 10L, 10L
   ), .Label = c("Eve", "ba", "De", "De","Mi", "C", "O", "W", 
"as", "ras", "Cro", "ics"), class = "factor"), ds = c(8, 8, 
 1, 4, 4, 6), em = c(1, 3, 8,2, 7, 3)), row.names = c(74567L, 
74568L, 74570L, 74576L, 74577L, 74578L), class = "data.frame")

我需要每个组将em和ds的所有值分配给NA

 > quantile 90 = NA

 < quantile 10 = NA
评论
nsunt
nsunt

Here's a way to do it for each group and each numeric variable using dplyr and ifelse:

library(dplyr)

df %>% 
   group_by(group) %>% 
   mutate(ds = ifelse(ds > quantile(ds, .9) | ds > quantile(ds, .9), NA, ds),
          em = ifelse(em > quantile(em, .9) | em > quantile(em, .9), NA, em))
#> # A tibble: 6 x 3
#> # Groups:   group [3]
#>   group    ds    em
#>   <fct> <dbl> <dbl>
#> 1 Eve       8     1
#> 2 Eve       8    NA
#> 3 ba        1    NA
#> 4 ba       NA     2
#> 5 ras       4    NA
#> 6 ras      NA     3

Created on 2020-05-17 by the reprex package (v0.3.0)

点赞
评论