gpt4 book ai didi

r - 在 for 循环中使用 split()

转载 作者:行者123 更新时间:2023-12-04 07:24:19 26 4
gpt4 key购买 nike

我想创建一个循环来获取每个变量的因子水平的汇总拆分。例如,如果我想在“分组”变量中按因子级别进行汇总拆分,我将使用:

df %>%
select(grouping, length, weight) %>%
split(.$grouping) %>%
map(summary)
但是,我不确定如何将其放入循环中,以便根据数据框中每个感兴趣的变量的因子水平获得摘要。
例如,我可以得到 summary()对于数据框第 3 列和第 4 列中的变量,使用:
# Dummy data
length = sample(30:60, 10, replace = FALSE)
weight = sample(50:70, 10, replace = FALSE)
grouping = c("A", "A", "B", "A", "B", "A", "B", "B", "B", "A")
colour = c("Blue", "Green", "Green", "Green", "Blue", "Blue", "Blue", "Green", "Blue", "Green")
type = c("Case", "Control", "Case", "Case", "Case", "Control", "Control", "Case", "Control", "Case")
df = data.frame(length, weight, grouping, colour, type)

# Variables to loop
colNames <- names(df)[c(3:4)]

# Summary
for(i in colNames){
# Summary
summary <- df %>%
select(length, weight, .$colNames[i]) %>%
summary()

print(summary)
}
但是当按每个变量的因子水平分割时,我不能这样做:
# Variables to loop 
colNames = names(df)[c(3,4)]

# Summary
for(i in colNames){
df %>%
select(length, weight, .$colNames[i]) %>%
split(.$colNames[i]) %>%
summary()
}
我图 split(.colNames)是问题所在,但我不知道如何解决。感谢您的任何帮助!

最佳答案

您的代码有两个问题:

  • i已经是您的列的名称。因此 .$colNames[i]NULL .此问题已在 select 中出现.
  • 如果要使用包含列名的变量访问数据框中的列,则 $不管用。而是使用 [[ .

  • # Dummy data
    set.seed(42)
    length <- sample(30:60, 10, replace = FALSE)
    weight <- sample(50:70, 10, replace = FALSE)
    grouping <- c("A", "A", "B", "A", "B", "A", "B", "B", "B", "A")
    colour <- c("Blue", "Green", "Green", "Green", "Blue", "Blue", "Blue", "Green", "Blue", "Green")
    type <- c("Case", "Control", "Case", "Case", "Case", "Control", "Control", "Case", "Control", "Case")
    df <- data.frame(length, weight, grouping, colour, type)

    # Variables to loop
    colNames <- names(df)[c(3:4)]

    library(dplyr)
    library(purrr)

    # Summary
    for (i in colNames) {
    df %>%
    select(length, weight, all_of(i)) %>%
    split(.[[i]]) %>%
    map(summary) %>%
    map(print)
    }

    #> length weight grouping
    #> Min. :33.0 Min. :52.0 Length:5
    #> 1st Qu.:34.0 1st Qu.:53.0 Class :character
    #> Median :36.0 Median :54.0 Mode :character
    #> Mean :40.6 Mean :59.2
    #> 3rd Qu.:46.0 3rd Qu.:67.0
    #> Max. :54.0 Max. :70.0
    #> length weight grouping
    #> Min. :30 Min. :58.0 Length:5
    #> 1st Qu.:39 1st Qu.:60.0 Class :character
    #> Median :44 Median :63.0 Mode :character
    #> Mean :44 Mean :62.2
    #> 3rd Qu.:47 3rd Qu.:64.0
    #> Max. :60 Max. :66.0
    #> NULL
    #> length weight colour
    #> Min. :33.0 Min. :52.0 Length:5
    #> 1st Qu.:39.0 1st Qu.:53.0 Class :character
    #> Median :44.0 Median :58.0 Mode :character
    #> Mean :41.8 Mean :57.4
    #> 3rd Qu.:46.0 3rd Qu.:60.0
    #> Max. :47.0 Max. :64.0
    #> length weight colour
    #> Min. :30.0 Min. :54 Length:5
    #> 1st Qu.:34.0 1st Qu.:63 Class :character
    #> Median :36.0 Median :66 Mode :character
    #> Mean :42.8 Mean :64
    #> 3rd Qu.:54.0 3rd Qu.:67
    #> Max. :60.0 Max. :70

    关于r - 在 for 循环中使用 split(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68303154/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com