在R中如何按变量区分每个主题计算2个均值。

huangapple go评论70阅读模式
英文:

How do I alculate 2 means per subject differentiated by a variable in R

问题

这是我在stack overflow上的第一个问题。我尝试了一些研究,但没有取得进展。我试图在R中计算不同变量区分的每个子项目的两个不同的均值。
我创建了这个示例数据集:

airquality <- data.frame(subject = c("CityA", "CityA", "CityA", "CityA", "CityA", "CityA", "CityA", "CityA",
                                  "CityB", "CityB", "CityB", "CityB", "CityB", "CityB", "CityB", "CityB",
                                  "CityC", "CityC", "CityC", "CityC", "CityC", "CityC", "CityC", "CityC"),
                         code = c("AA_high", "BB_high", "AA_low", "BB_low", "AB_high", "AB_low", "BA_high", "BA_low",
                                  "AA_high", "BB_high", "AA_low", "BB_low", "AB_high", "AB_low", "BA_high", "BA_low",
                                  "AA_high", "BB_high", "AA_low", "BB_low", "AB_high", "AB_low", "BA_high", "BA_low"),
                         measure = c(2, 6, 5, 3, 5, 5, 4, 6,
                                   7, 8, 6, 7, 4, 12, 9, 7,
                                   8, 12, 11, 9, 15, 11, 10, 16))
print(airquality)

目标是每个主题有2个均值:第一个均值是所有代码以"AA"或"BB"开头的测量的均值。每个主题的第二个均值应该是以"AB"或"BA"开头的代码的测量均值。

我从这里开始:

df1 <- filter(airquality, substr(code, 1, 2) == "AA" | substr(code, 1,2) == "BB") 
df1 <- df1 %>% 
  group_by(subject) %>% 
  mutate(mean_AABB = mean(measure))
View(df1)

df2 <- filter(airquality, substr(code, 1, 2) == "AB" | substr(code, 1,2) == "BA") 
df2 <- df2 %>% 
  group_by(subject) %>% 
  mutate(mean_ABBA = mean(measure))
View(df2)

但这似乎不起作用,我需要将这些均值添加到数据框中,以便我可以进行更多的均值计算。
所以下一步是将这些类型的均值添加到数据框中。

非常感谢帮助!
非常感谢,Maike

英文:

This is my first question on stack noverflow. I have tried some reasearch, but I am not getting there. I am trying to calculate two differents means per subjet which are differentiated by a variable in R.
I created this example data set:

airquality &lt;- data.frame(subject = c(&quot;CityA&quot;, &quot;CityA&quot;,&quot;CityA&quot;,&quot;CityA&quot;, &quot;CityA&quot;, &quot;CityA&quot;, &quot;CityA&quot;, &quot;CityA&quot;,
                                  &quot;CityB&quot;,&quot;CityB&quot;,&quot;CityB&quot;, &quot;CityB&quot;, &quot;CityB&quot;, &quot;CityB&quot;, &quot;CityB&quot;, &quot;CityB&quot;,
                                  &quot;CityC&quot;, &quot;CityC&quot;, &quot;CityC&quot;, &quot;CityC&quot;, &quot;CityC&quot;, &quot;CityC&quot;, &quot;CityC&quot;, &quot;CityC&quot;),
                         code = c(&quot;AA_high&quot;, &quot;BB_high&quot;, &quot;AA_low&quot;, &quot;BB_low&quot;, &quot;AB_high&quot;, &quot;AB_low&quot;, &quot;BA_high&quot;, &quot;BA_low&quot;,
                                  &quot;AA_high&quot;, &quot;BB_high&quot;, &quot;AA_low&quot;, &quot;BB_low&quot;, &quot;AB_high&quot;, &quot;AB_low&quot;, &quot;BA_high&quot;, &quot;BA_low&quot;,
                                  &quot;AA_high&quot;, &quot;BB_high&quot;, &quot;AA_low&quot;, &quot;BB_low&quot;, &quot;AB_high&quot;, &quot;AB_low&quot;, &quot;BA_high&quot;, &quot;BA_low&quot;),
                         measure = c(&quot;2&quot;, &quot;6&quot;, &quot;5&quot;, &quot;3&quot;, &quot;5&quot;, &quot;5&quot;, &quot;4&quot;, &quot;6&quot;,
                                   &quot;7&quot;, &quot;8&quot;, &quot;6&quot;, &quot;7&quot;, &quot;4&quot;, &quot;12&quot;, &quot;9&quot;, &quot;7&quot;,
                                   &quot;8&quot;, &quot;12&quot;, &quot;11&quot;, &quot;9&quot;, &quot;15&quot;, &quot;11&quot;, &quot;10&quot;, &quot;16&quot;))
print(airquality)

The aim is to have 2 means per subject: The first mean is the mean of all the measures which codes start with "AA" or "BB".
The second mean per subject should be the mean of the measures of all the codes which start with "AB" or "BA".

I started with this:

df1 &lt;- filter(airquality, substr(code, 1, 2) == &quot;AA&quot; | substr(code, 1,2) == &quot;BB&quot;) 
df1 &lt;- df1 %&gt;% 
  group_by(subject) %&gt;% 
  mutate(mean_AABB = mean(measure))
View(df1)

df2 &lt;- filter(airquality, substr(code, 1, 2) == &quot;AB&quot; | substr(code, 1,2) == &quot;BA&quot;) 
df2 &lt;- df2 %&gt;% 
  group_by(subject) %&gt;% 
  mutate(mean_ABBA = mean(measure))
View(df2)

But this doesn't seem to work and I need to have columns added to the dataframe, so I can do more calculations with the means.
So the next step would be to add those type of means to the dataframe.

I am very grateful for help!
Thanks very much,
Maike

答案1

得分: 1

怎么样:

library(dplyr)

airquality |>
group_by(first_two_equal = substr(code, 1, 1) == substr(code, 2, 2)) |>
mutate(measure = as.numeric(measure), ## !
mean = mean(measure)
)


(请注意,你示例数据中的`measure`是类型为"character",所以你需要将其转换为"numeric")

**编辑**
对于扩展的分组,将额外的因素包括在`group_by`中,如下所示:

airquality |>
group_by(subject,
first_two_equal = substr(code, 1, 1) == substr(code, 2, 2)
) |>
mutate(measure = as.numeric(measure), ## !
mean = mean(measure)
)

输出:
  • A tibble: 24 x 5

Groups: subject, first_two_equal [6]

subject code measure first_two_equal mean
<chr> <chr> <dbl> <lgl> <dbl>
1 CityA AA_high 2 TRUE 4
2 CityA BB_high 6 TRUE 4
3 CityA AA_low 5 TRUE 4
4 CityA BB_low 3 TRUE 4
5 CityA AB_high 5 FALSE 5
6 CityA AB_low 5 FALSE 5


<details>
<summary>英文:</summary>

What about:

library(dplyr)

airquality |>
group_by(first_two_equal = substr(code, 1, 1) == substr(code, 2, 2)) |>
mutate(measure = as.numeric(measure), ## !
mean = mean(measure)
)


(mind that `measure` of your example data is mode &quot;character&quot; so you&#39;ll need to convert it to &quot;numeric&quot;)

**edit**
For extended grouping, include the additional factors in the `group_by` like so:

airquality |>
group_by(subject,
first_two_equal = substr(code, 1, 1) == substr(code, 2, 2)
) |>
mutate(measure = as.numeric(measure), ## !
mean = mean(measure)
)

output:
  • A tibble: 24 x 5

Groups: subject, first_two_equal [6]

subject code measure first_two_equal mean
<chr> <chr> <dbl> <lgl> <dbl>
1 CityA AA_high 2 TRUE 4
2 CityA BB_high 6 TRUE 4
3 CityA AA_low 5 TRUE 4
4 CityA BB_low 3 TRUE 4
5 CityA AB_high 5 FALSE 5
6 CityA AB_low 5 FALSE 5



</details>



huangapple
  • 本文由 发表于 2023年7月31日 22:02:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804377.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定