R 计算多变量x范围的平均值

R 计算多变量x范围的平均值,r,dplyr,tidyverse,purrr,R,Dplyr,Tidyverse,Purrr,我有很多列连续变量,我需要计算其中有多少在x范围内。我尝试了一种与此类似的冗长方法: library(tidyverse) mtcars %>% mutate("1" = if_else(mpg >= 0 & mpg <= 1, 1, 0), "5" = if_else(mpg >= 0 & mpg <= 5, 1, 0), "10" =

我有很多列连续变量,我需要计算其中有多少在x范围内。我尝试了一种与此类似的冗长方法:

library(tidyverse)

mtcars %>% 
  mutate("1" = if_else(mpg  >= 0 & mpg  <= 1, 1, 0),
         "5" = if_else(mpg  >= 0 & mpg  <= 5, 1, 0),
         "10" = if_else(mpg  >= 0 & mpg  <= 10, 1, 0),
         "20" = if_else(mpg  >= 0 & mpg  <= 20, 1, 0),
         "50" = if_else(mpg  >= 0 & mpg  <= 50, 1, 0),
         "100" = if_else(mpg  >= 0 & mpg  <= 100, 1, 0),
         "400" = if_else(mpg  >= 0 & mpg  <= 400, 1, 0),
         distance = round(mpg , 2)) %>% 
  select("1", "5", "10", "20", "50", "100", "400") %>% 
  map_df(mean) 


有没有一种优雅的方法可以在不复制和粘贴变量的情况下迭代此过程?

如果需要对所有数值列执行此操作,请循环上限值,然后使用summary with over to loop over the columns,计算用between创建的逻辑向量的平均值

或者,这可以在带有外部


如果我们需要对所有数值列执行此操作,则循环上限值,然后使用summary with over循环列,计算使用between创建的逻辑向量的平均值

或者,这可以在带有外部


这应该会产生相同的结果,并且是一种相当整洁的方法,我认为:

distance_thresholds <- tibble(
  threshold = c(1, 5, 10, 20, 50, 100, 400)
)

mtcars %>%
  left_join(distance_thresholds, by = character()) %>%
  group_by(threshold) %>%
  summarise(avg = mean(mpg <= threshold)) %>%
  pivot_wider(names_from = threshold, values_from = avg)

这应该会产生相同的结果,并且是一种相当整洁的方法,我认为:

distance_thresholds <- tibble(
  threshold = c(1, 5, 10, 20, 50, 100, 400)
)

mtcars %>%
  left_join(distance_thresholds, by = character()) %>%
  group_by(threshold) %>%
  summarise(avg = mean(mpg <= threshold)) %>%
  pivot_wider(names_from = threshold, values_from = avg)
Sappy和colMeans的意思是:

Sappy和colMeans的意思是:


使用MTCARS尝试imap\u dbllst1,5,10,20,50,100,400,~平均在0.x%>%之间作为可存储的\u row使用MTCARS尝试imap\u dbllst1,5,10,20,50,100,400,~平均在0.x%>%之间作为可存储的\u row伟大的解决方案!如何转换输出,使列成为阈值?@dano_uu您可以使用tout获得expectedGreat解决方案!我如何转换输出以使列成为阈值?@dano_uu您只需使用tout即可获得预期的结果
categ <- c(1, 5, 10, 20, 50, 100, 400)
out <- cbind(categ, outer(categ, mtcars,
     Vectorize(function(x, y) mean(y >= 0 & y <= x))))
out
#     categ    mpg     cyl    disp      hp drat      wt    qsec vs am gear    carb
#[1,]     1 0.0000 0.00000 0.00000 0.00000    0 0.00000 0.00000  1  1    0 0.21875
#[2,]     5 0.0000 0.34375 0.00000 0.00000    1 0.90625 0.00000  1  1    1 0.93750
#[3,]    10 0.0000 1.00000 0.00000 0.00000    1 1.00000 0.00000  1  1    1 1.00000
#[4,]    20 0.5625 1.00000 0.00000 0.00000    1 1.00000 0.90625  1  1    1 1.00000
#[5,]    50 1.0000 1.00000 0.00000 0.00000    1 1.00000 1.00000  1  1    1 1.00000
#[6,]   100 1.0000 1.00000 0.15625 0.28125    1 1.00000 1.00000  1  1    1 1.00000
#[7,]   400 1.0000 1.00000 0.90625 1.00000    1 1.00000 1.00000  1  1    1 1.00000
distance_thresholds <- tibble(
  threshold = c(1, 5, 10, 20, 50, 100, 400)
)

mtcars %>%
  left_join(distance_thresholds, by = character()) %>%
  group_by(threshold) %>%
  summarise(avg = mean(mpg <= threshold)) %>%
  pivot_wider(names_from = threshold, values_from = avg)
vals <- c(1, 5, 10, 20, 50, 100, 400)
colMeans(sapply(vals, function(x) mtcars$mpg >=0 & mtcars$mpg <= x))