【问题标题】:Calculation groups with specific columns in rr 中具有特定列的计算组
【发布时间】:2020-02-27 18:39:21
【问题描述】:

我的数据是这样的模式

df1<-read.table(text="Car1	Car2	Car3	Time1	Time2	Time3
22	33	90	20	90	20
11	45	88	10	80	30
22	33	40	40	10	10
11	45	40	10	10	40
11	45	88	10	12	60
22	45	90	60	20	100",header=TRUE)

我想根据 Car 和 time 计算平均值和 SD。要点是 Car 1 对应 Time1,Car2 对应 Time 2,Car3 对应 Time3,以此类推。

我想得到下表:

Car1	Mean	SD
11	10	0
22	40	20
Car2		
33	xx	xx
45	xx	xx
Car3		
40	xx	xx
88	xx	xx
90	xx	xx

我试过了:

df1 %>% group_by(Car1,Car2,Car3) %>% 
summarise(mean=mean(Time,SD=sd(Time))

不幸的是,它不起作用。有什么帮助吗?

【问题讨论】:

    标签: r tidyverse


    【解决方案1】:

    你也可以使用包data.table

    library(data.table)
    melt(setDT(df1), 
         measure = patterns("Car", "Time"), 
         value.name = c("Car", "Time"), 
         variable.name = "group"
         )[, .(Mean = mean(Time), Sd = sd(Time)), .(group, Car)]
    
    #     group   Car  Mean       Sd
    # 1:      1    22  40.0 20.00000
    # 2:      1    11  10.0  0.00000
    # 3:      2    33  50.0 56.56854
    # 4:      2    45  30.5 33.28163
    # 5:      3    90  60.0 56.56854
    # 6:      3    88  45.0 21.21320
    # 7:      3    40  25.0 21.21320
    

    【讨论】:

      【解决方案2】:

      这是pivot_longer 的一个选项,我们从“宽”格式重塑为“长”格式并按“组1”索引和“汽车”分组,得到“时间”的meansd summarise'时间'

      library(dplyr)
      library(tidyr)
      df1 %>% 
        pivot_longer(cols = everything(), names_to = c(".value", "group"),
              names_sep="(?<=[a-z])(?=\\d+)") %>%
        group_by(group, Car) %>% 
        summarise(Mean = mean(Time), SD = sd(Time))
      # A tibble: 7 x 4
      # Groups:   group [3]
      #  group   Car  Mean    SD
      #  <chr> <int> <dbl> <dbl>
      #1 1        11  10     0  
      #2 1        22  40    20  
      #3 2        33  50    56.6
      #4 2        45  30.5  33.3
      #5 3        40  25    21.2
      #6 3        88  45    21.2
      #7 3        90  60    56.6
      

      【讨论】:

        【解决方案3】:

        假设您可以轻松地将数据分为时间和汽车,那么您可以使用loop 执行此操作,假设您将数据分为您提供的结构。

        cars <- df1[1:3]
        Time <- df1[4:6]
        
        ls <- list()
        for(i in 1:ncol(cars)) {
          ls[[i]] <- aggregate(Time[i], by = cars[i], FUN = function(x) c(mean(x), sd(x)))
        }
        

        ls

        结果数据为:

        df1 <- structure(list(Car1 = c(22L, 11L, 22L, 11L, 11L, 22L), Car2 = c(33L, 
        45L, 33L, 45L, 45L, 45L), Car3 = c(90L, 88L, 40L, 40L, 88L, 90L
        ), Time1 = c(20L, 10L, 40L, 10L, 10L, 60L), Time2 = c(90L, 80L, 
        10L, 10L, 12L, 20L), Time3 = c(20L, 30L, 10L, 40L, 60L, 100L)), class = "data.frame", row.names = c(NA, 
        -6L))
        

        【讨论】:

          【解决方案4】:
          lapply(split.default(df1, gsub("\\D+", "", names(df1))), function(x){
              d = gsub("\\D+", "", names(x)[1])
              x %>%
                  group_by(!!sym(paste0("Car", d))) %>%
                  summarise(mean = mean(!!sym(paste0("Time", d))),
                            sd = sd(!!sym(paste0("Time", d)))) %>%
                  ungroup()
          })
          

          【讨论】:

          • @user202,立即查看
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-12-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多