获取所有分组组合的摘要,例如 SAS 中的过程摘要

发布于 2025-01-11 12:14:26 字数 3227 浏览 2 评论 0原文

(我确实知道我的问题与此相同:R 函数等效SAS 中的过程摘要 但作为一个新用户,我无法评论解决方案以询问详细信息或解释,而且我无法让其中任何一个工作。)

我正在尝试将脚本从 SAS 转换为 R。目标是获得跨多个变量的数据库的广泛摘要。

起始基地是这样的:

学生 IDFlag1Flag2Flag3other flags...WeightScore
code1level1Afirstsmth~~212
code23level5CThirdsmth~else~39

最后我想要这样的东西:

Flag1Flag2Flag3其他标志...nb 学生加权平均值std devmin第一个四分位数...最大nb 学生在第一个十分位数...nb 学生在最后十分位数
level1A首先smth~~510.961.51..................
level5所有第三个smth~else~15008.702.73............。 .....

在 SAS 中这真的很容易,因为 proc Summary 为每种可能的分组组合进行汇总,但在 R 中,您只能获得最低级别的分组。 有 9 个不同级别的分组,共有 512 种组合,我认为应该有一种方法来循环某些工作。

我认为我应该这样做:

1-列出数据帧中的所有不同组合:

Flag1Flag2Flag3
.All.All.All
.All.Allfirst
.All.Allsecondary
.AllA.All
.AllB.All
LV1。所有.所有
LV2.所有.所有
.所有A第一
.所有A第二
.所有B第一
.所有B第二
LV1.所有第一
LV1.所有第二
LV2.所有第一
LV2.所有第二
LV1A.
所有LV1B.所有
LV2A.所有
LV2B.
所有LV1A第一个
LV1A第二个
LV1B
一个LV1B第二个
LV2A
一个LV2A第二个
LV2B第一个
LV2B第二个

2- 创建一个 2^n 长度的循环,将调用以下函数:

3- 该函数将从最后一行开始dataframe,然后输出一个数据帧,其中包含按某些变量+列进行分组的摘要。All 表示不用于分组的变量

4-使用bind_rows 相互堆栈循环的每次迭代

(I do understand that my question is equivalent to this one : R function equivalent to proc summary in SAS
But being a new user, I can't comment on the solutions to ask details or explanations and I can't get any of them to work.)

I'm trying to convert a script from SAS to R. The objective is to get a wide summary of a database across multiple variables.

The starting base is like this :

Student IDFlag1Flag2Flag3other flags...weightscore
code1level1Afirstsmth~~212
code23level5Cthirdsmth~else~39

And in the end I want something like this :

Flag1Flag2Flag3other flags...nb of studentsweighted meanstd devmin1st quartile...maxnb of students in fist decile...nb of students in last decile
level1Afirstsmth~~510.961.51..................
level5.Allthirdsmth~else~15008.702.73..................

In SAS it was really easy because proc summary does the summary for each combination of grouping possible, but in R, you only get the lowest level of grouping.
With 9 different levels of grouping that's 512 combinations and I think there should be a way to loop some of the work.

Here's how I think I should proceed :

1- List all the different combinations in a dataframe :

Flag1Flag2Flag3
.All.All.All
.All.Allfirst
.All.Allsecond
.AllA.All
.AllB.All
LV1.All.All
LV2.All.All
.AllAfirst
.AllAsecond
.AllBfirst
.AllBsecond
LV1.Allfirst
LV1.Allsecond
LV2.Allfirst
LV2.Allsecond
LV1A.All
LV1B.All
LV2A.All
LV2B.All
LV1Afirst
LV1Asecond
LV1Bfirst
LV1Bsecond
LV2Afirst
LV2Asecond
LV2Bfirst
LV2Bsecond

2- Make a 2^n length loop that will call the following function :

3- The function would take a line from the last dataframe and then output a dataframe that would contain the summary grouping by some variables + columns with .All for the variables not used for grouping

4- stack each iteration of the loop on each other using bind_rows

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

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

发布评论

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

评论(1

勿挽旧人 2025-01-18 12:14:26

我在解决这个问题时遇到了多个障碍,但最终得到了一个令人满意的解决方案:

#import the data
testbase <- read_excel("testbase.xlsx")
#list all the grouping variables
variables = c(quo(Flag1), quo(Flag2),quo(Flag3))
#create the powerset of the list of variables
listevars=powerSet(variables,length(variables),rev=FALSE)

for (i in 1:length(listevars)){
  testbase=ungroup(testbase)
  if (length(listevars[[i]])!=0){
    testbase=group_by(testbase,!!!listevars[[i]])
  }
  resumepartiel=summarize(testbase,weighted.mean(score,weight))
  varexcl=variables[!(variables %in% listevars[[i]])]
  if (length(varexcl)!=0){
    for(j in 1:length(varexcl)){
      colonne=data.frame(c(rep(".All",times = nrow(resumepartiel))))
      colonne=setNames(colonne,as_name(varexcl[[j]]))
      resumepartiel=bind_cols(colonne,resumepartiel)
    }
  }
  if(i==1){
    resume=resumepartiel
  }
  else{
    resume=bind_rows(resume,resumepartiel)
  }
}

这段代码将输出我想要的三个变量,并且仅输出加权平均值,但添加更多变量或更多汇总函数是微不足道的。

I encountered multiple hurdles solving this problem but I ended with a satisfying solution :

#import the data
testbase <- read_excel("testbase.xlsx")
#list all the grouping variables
variables = c(quo(Flag1), quo(Flag2),quo(Flag3))
#create the powerset of the list of variables
listevars=powerSet(variables,length(variables),rev=FALSE)

for (i in 1:length(listevars)){
  testbase=ungroup(testbase)
  if (length(listevars[[i]])!=0){
    testbase=group_by(testbase,!!!listevars[[i]])
  }
  resumepartiel=summarize(testbase,weighted.mean(score,weight))
  varexcl=variables[!(variables %in% listevars[[i]])]
  if (length(varexcl)!=0){
    for(j in 1:length(varexcl)){
      colonne=data.frame(c(rep(".All",times = nrow(resumepartiel))))
      colonne=setNames(colonne,as_name(varexcl[[j]]))
      resumepartiel=bind_cols(colonne,resumepartiel)
    }
  }
  if(i==1){
    resume=resumepartiel
  }
  else{
    resume=bind_rows(resume,resumepartiel)
  }
}

this code will output what I want for three variables and only the weighted mean but adding more variables or more summary functions is trivial.

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