我们可以做
我有一个df,我想按id计算c列
structure(list(id = c(14, 14, 15, 15, 15, 26, 26, 26, 26), a = c(1,
2, NA, 7, NA, 2, NA, 2, 3), b = c(2, 4, 8, NA, 1, 4, 2, 9, 8),
c = c(2.3, 4.4, 1.3, 5.4, 3, NA, 1, 0, 3)), class = "data.frame", row.names = c(NA,
-9L))
id a b c
1 14 1 2 2.3
2 14 2 4 4.4
3 15 NA 8 1.3
4 15 7 NA 5.4
5 15 NA 1 3.0
6 26 2 4 NA
7 26 NA 2 1.0
8 26 2 9 0.0
9 26 3 8 3.0
我想用我所采用的所有阈值的结果做一个df。这将导致:
thres_range <- seq(1, 3) # values I want to try as threshold
id thres1 thres2 thres3
1 14 0 0 1
2 15 0 1 1
3 26 1 2 2
我已经获得了按id计数c列library(dplyr)
thres_range <- seq(1, 3) # values I want to try as threshold
fun <- function(thres) {
w <- paste0("thres", thres) # give column name e.g. thres2, thres3 etc
df %>% group_by(id) %>%
summarise(w = sum(c < thres, na.rm=TRUE))
}
sapply(thres_range, function(L) fun(L))
任何建议将不胜感激!提前致谢! :D
我有一个df,我想按id计算c列
我们可以做
library(dplyr)
library(purrr)
map(thres_range, ~
df %>%
group_by(id) %>%
summarise(w = sum(c < .x, na.rm = TRUE))) %>%
reduce(inner_join, by = 'id') %>%
rename_at(vars(starts_with('w')), ~ str_c('thresh', seq_along(.)))
# A tibble: 3 x 4
# id thresh1 thresh2 thresh3
# <dbl> <int> <int> <int>
#1 14 0 0 1
#2 15 0 1 1
#3 26 1 2 2
我们可以做