在R中进行分组和识别最大值

发布于 2025-01-24 13:26:08 字数 791 浏览 0 评论 0原文

我正在尝试在R中进行一些模拟,而我被困在我需要做的循环中。我能够在一次迭代中得到我需要的东西,但是试图编码循环使我失望。这就是我在一次迭代中所做的。

    Subjects <- c(1,2,3,4,5,6)
    Group <- c('A','A','B','B','C','C')
    Score <- rnorm(6,mean=5,sd=1)
    
    Example <- data.frame(Subjects,Group,Score)
    
    library(dplyr)
    
    Score_by_Group <- Example %>% group_by(Group) %>% summarise(SumGroup = sum(Score))
    Score_by_Group$Top_Group <- ifelse(Score_by_Group[,2] == max(Score_by_Group[,2]),1,0)

Group     SumGroup    Top_Group      
1 A         8.77          0
2 B         6.22          0
3 C         9.38          1

我需要做的是,运行上述“ X”时间,每次该组具有最高分数时,请将其添加到先前的结果中。因此,例如,如果循环为x = 10,我将需要这样的结果:

Group    Top_Group      
1 A          3
2 B          5
3 C          2

I am trying to do some simulations in R and I am stuck on the loop that I need to be doing. I am able to get what I need in one iteration but trying to code the loop is throwing me off. This is what i am doing for one iteration.

    Subjects <- c(1,2,3,4,5,6)
    Group <- c('A','A','B','B','C','C')
    Score <- rnorm(6,mean=5,sd=1)
    
    Example <- data.frame(Subjects,Group,Score)
    
    library(dplyr)
    
    Score_by_Group <- Example %>% group_by(Group) %>% summarise(SumGroup = sum(Score))
    Score_by_Group$Top_Group <- ifelse(Score_by_Group[,2] == max(Score_by_Group[,2]),1,0)

Group     SumGroup    Top_Group      
1 A         8.77          0
2 B         6.22          0
3 C         9.38          1

What I need my loop to do is, run the above 'X' times and every time that group has the Top Score, add it to the previous result. So for example, if the loop was to be x=10, I would need a result like this:

Group    Top_Group      
1 A          3
2 B          5
3 C          2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柏拉图鍀咏恒 2025-01-31 13:26:08

如果您不介意放弃for循环,我们可以使用Replicate重复代码,然后将输出绑定在一起,然后汇总。

library(tidyverse)

run_sim <- function()
{
  Subjects <- c(1, 2, 3, 4, 5, 6)
  Group <- c('A', 'A', 'B', 'B', 'C', 'C')
  Score <- rnorm(6, mean = 5, sd = 1)

  Example <- data.frame(Subjects, Group, Score)

  Score_by_Group <- Example %>%
    group_by(Group) %>%
    summarise(SumGroup = sum(Score)) %>%
    mutate(Top_Group = +(SumGroup == max(SumGroup))) %>%
    select(-SumGroup)
}


results <- bind_rows(replicate(10, run_sim(), simplify = F)) %>%
  group_by(Group) %>% 
  summarise(Top_Group = sum(Top_Group))

输出

  Group Top_Group
  <chr>     <int>
1 A             3
2 B             3
3 C             4

If you don't mind forgoing the for loop, we can use replicate to repeat the code, then bind the output together, and then summarize.

library(tidyverse)

run_sim <- function()
{
  Subjects <- c(1, 2, 3, 4, 5, 6)
  Group <- c('A', 'A', 'B', 'B', 'C', 'C')
  Score <- rnorm(6, mean = 5, sd = 1)

  Example <- data.frame(Subjects, Group, Score)

  Score_by_Group <- Example %>%
    group_by(Group) %>%
    summarise(SumGroup = sum(Score)) %>%
    mutate(Top_Group = +(SumGroup == max(SumGroup))) %>%
    select(-SumGroup)
}


results <- bind_rows(replicate(10, run_sim(), simplify = F)) %>%
  group_by(Group) %>% 
  summarise(Top_Group = sum(Top_Group))

Output

  Group Top_Group
  <chr>     <int>
1 A             3
2 B             3
3 C             4
静待花开 2025-01-31 13:26:08

我认为这应该有效:

library(dplyr)

Subjects <- c(1,2,3,4,5,6)
Group <- c('A','A','B','B','C','C')

Groups <- c('A','B','C')
Top_Group <- c(0,0,0)


x <- 10

for(i in 1:x) {
    Score <- rnorm(6,mean=5,sd=1)
    
    Example <- data.frame(Subjects,Group,Score)
    

    
    Score_by_Group <- Example %>% group_by(Group) %>% summarise(SumGroup = sum(Score))
    Score_by_Group$Top_Group <- ifelse(Score_by_Group[,2] == max(Score_by_Group[,2]),1,0)

    Top_Group <- Top_Group + Score_by_Group$Top_Group
}

tibble(Groups, Top_Group)

I think this should work:

library(dplyr)

Subjects <- c(1,2,3,4,5,6)
Group <- c('A','A','B','B','C','C')

Groups <- c('A','B','C')
Top_Group <- c(0,0,0)


x <- 10

for(i in 1:x) {
    Score <- rnorm(6,mean=5,sd=1)
    
    Example <- data.frame(Subjects,Group,Score)
    

    
    Score_by_Group <- Example %>% group_by(Group) %>% summarise(SumGroup = sum(Score))
    Score_by_Group$Top_Group <- ifelse(Score_by_Group[,2] == max(Score_by_Group[,2]),1,0)

    Top_Group <- Top_Group + Score_by_Group$Top_Group
}

tibble(Groups, Top_Group)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文