英文:
add marginal frequency to three way frequency table
问题
我想创建一个包含三个(加权)变量和边际频率的交叉频率表。
这是一个可重现的示例:
library(stats)
library(dplyr)
data("cms_patient_care", package = "tidyr")
weights <- rnorm(252, 1, 0.1)
df <- cbind(cms_patient_care, weights)
crosstabs <- xtabs(weights ~ facility_name + type + measure_abbr, data = df)
现在我想要每个measure_abbr按facility_name和type的边际频率。
到目前为止,我已经使用以下函数获得了facility_name和type每个元素的边际频率,并使用addmargins()
函数添加了边际频率:
marg <- function(x) {x[1] / sum(x) * 100}
crosstabs2 <- addmargins(crosstabs, margin = 3, FUN = marg)
我该如何获得其余部分?
英文:
I want to create a cross frequency table of three (weighted) variables with marginal frequency.
Here is a reproducible example
library(stats)
library(dplyr)
data("cms_patient_care", package = "tidyr")
weights <- rnorm(252, 1,0.1)
df <- cbind(cms_patient_care, weights)
crosstabs <- xtabs(weights ~ facility_name + type + measure_abbr, data=df)
Now I'd like to have the marginal frequency of each measure_abbr by facility_name * type
So far, I've managed to have the marginal frequency of the first element of each facility_name*type with this function and addmargins()
marg <- function(x) {x[1]/sum(x)*100}
crosstabs2 <- addmargins(crosstabs, margin=3, FUN = marg)
How can I get the rest of it ?
答案1
得分: 0
如果有人想要做相同的事情,我已经放弃使用addmargins()的想法,因为输出必须与边际中指定的维度相同,所以为了使其工作,您需要执行以下操作,这实际上没有用:
marg <- function(x) {x[1]/sum(x)*100}
marg2 <- function(x) {x[2]/sum(x)*100}
marg3 <- function(x) {x[3]/sum(x)*100}
...
crosstabs2 <- addmargins(crosstabs, margin=c(3,3,3,...), FUN = c(marg, marg2, marg3,...))
我已经成功使用一个小函数来完成相同的操作:
- data是xtabs的输出
- variable_name是要获取边际频率的变量名称(在引号内),在示例中为"measure_abbr"。请注意,我使用position来选择变量。
freq_marg <- function(data, variable_name){
df <- as.data.frame(data) %>%
group_by(across(1:which(colnames(.) == variable_name) - 1)) %>%
mutate(Somme_Freq = sum(Freq)) %>%
ungroup() %>%
mutate(RowPercent = Freq / Somme_Freq)
return(df)
}
crosstabs2 <- freq_marg(crosstabs,"measure_abbr")
英文:
In case someone is looking to do the same, I've dropped the idea of using addmargins() because the output has to be the same dimension as stated in margin, so in order for it to work, you'd need to do the following, which is really unuseful
marg <- function(x) {x[1]/sum(x)*100}
marg2 <- function(x) {x[2]/sum(x)*100}
marg3 <- function(x) {x[3]/sum(x)*100}
...
crosstabs2 <- addmargins(crosstabs, margin=c(3,3,3,...), FUN = c(marg, marg2, marg3,...))
I've managed to do the same using a small function:
- data is the output of xtabs
- variable_name is the name of the variable (between quotation marks) you want the marginal frequency of (in the example : "measure_abbr"). Note that I use position to select variables.
freq_marg <- function(data, variable_name){
df <- as.data.frame(data) %>%
group_by(across(1:which(colnames(.) == variable_name) - 1)) %>%
mutate(Somme_Freq = sum(Freq)) %>%
ungroup() %>%
mutate(RowPercent = Freq / Somme_Freq)
return(df)
}
crosstabs2 <- freq_marg(crosstabs,"measure_abbr")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论