gpt4 book ai didi

r - 如何从 R 数据帧中获取条件结果

转载 作者:行者123 更新时间:2023-12-01 23:52:02 25 4
gpt4 key购买 nike

这是我在这里的第一条消息。我正在尝试解决 edX R 类(class)中的 R 练习,但我陷入了困境。如果有人可以帮助我解决它,那就太好了。以下是给出的数据框和问题:

> students
height shoesize gender population
1 181 44 male kuopio
2 160 38 female kuopio
3 174 42 female kuopio
4 170 43 male kuopio
5 172 43 male kuopio
6 165 39 female kuopio
7 161 38 female kuopio
8 167 38 female tampere
9 164 39 female tampere
10 166 38 female tampere
11 162 37 female tampere
12 158 36 female tampere
13 175 42 male tampere
14 181 44 male tampere
15 180 43 male tampere
16 177 43 male tampere
17 173 41 male tampere

Given the dataframe above, create two subsets with students whose height is equal to or below the median height (call it students.short) and students whose height is strictly above the median height (call it students.tall). What is the mean shoesize for each of the above 2 subsets by population?

我已经能够创建两个子集 students.tallstudents.short(均按 TRUE/FALSE 显示答案), 但我不知道如何通过人口获得平均值。数据应该像这样显示:

                    kuopio     tampere
students.short xxxx xxxx
students.tall xxxx xxxx

如果你能帮帮我,非常感谢!

最佳答案

我们可以根据中位数高度拆分

# // median height
medHeight <- median(students$height, na.rm = TRUE)

# // split the data into a list of data.frames using the 'medHeight'
lst1 <- with(students, split(students, height > medHeight))

然后遍历 list 使用来自 base Raggregate

lapply(lst1, function(dat) aggregate(shoesize ~ population, 
data = dat, FUN = mean, na.rm = TRUE))

但是,我们不需要创建两个单独的数据集或一个列表。可以通过将“人口”和使用 logical vector

创建的“grp”分组来完成
library(dplyr)
students %>%
group_by(grp = height > medHeight, population) %>%
summarise(shoesize = mean(shoesize))

关于r - 如何从 R 数据帧中获取条件结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63344865/

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