gpt4 book ai didi

r - 在 R 中使用均值、总和、长度和 sd 实现频率计数的更简单方法

转载 作者:行者123 更新时间:2023-12-02 08:21:20 25 4
gpt4 key购买 nike

我的任务是创建包含统计摘要的频率表。我的目标是创建一个可以简单地导出到 excel 的数据框。其中大部分可能在使用存储过程的 sql 中,但我决定在 R 中执行此操作。我正在学习 R,所以我可能会做很长的路要走。这是来自 getting-r-frequency-counts-for-all-possible-answers 的后续问题

给定

    Id <- c(1,2,3,4,5,6,7,8,9,10)
ClassA <- c(1,NA,3,1,1,2,1,4,5,3)
ClassB <- c(2,1,1,3,3,2,1,1,3,3)
R <- c(1,2,3,NA,9,2,4,5,6,7)
S <- c(3,7,NA,9,5,8,7,NA,7,6)
df <- data.frame(Id,ClassA,ClassB,R,S)

ZeroTenNAScale <- c(0:10,NA);

R.freq <- setNames(nm=c('answer','value'),data.frame(table(factor(df$R,levels=ZeroTenNAScale,exclude=NULL))));
R.freq[, 1] <- as.numeric(as.character( R.freq[, 1] ))
R.freq <- cbind(question='R',R.freq)

S.freq <- setNames(nm=c('answer','value'),data.frame(table(factor(df$S,levels=ZeroTenNAScale,exclude=NULL))));
S.freq[, 1] <- as.numeric(as.character( S.freq[, 1] ))
S.freq <- cbind(question='S',S.freq)

R.mean = mean(df$R, na.rm = TRUE)
R.length = sum(!is.na(df$R))
R.sd = sd(df$R, na.rm = TRUE)
R.sum = sum(df$R, na.rm = TRUE)

S.mean = mean(df$S, na.rm = TRUE)
S.length = sum(!is.na(df$S))
S.sd = sd(df$S, na.rm = TRUE)
S.sum = sum(df$S, na.rm = TRUE)

S.row <- cbind('S','sum',as.numeric(S.sum))
S.row <- setNames(nm=c('question','answer','value'),data.frame(S.row))
S.freq = rbind(S.freq, S.row )

S.row <- cbind('S','length',as.numeric(S.length))
S.row <- setNames(nm=c('question','answer','value'),data.frame(S.row))
S.freq = rbind(S.freq, S.row )

S.row <- cbind('S','mean',as.numeric(S.mean))
S.row <- setNames(nm=c('question','answer','value'),data.frame(S.row))
S.freq = rbind(S.freq, S.row )

S.row <- cbind('S','sd',as.numeric(S.sd))
S.row <- setNames(nm=c('question','answer','value'),data.frame(S.row))
S.freq = rbind(S.freq, S.row )

R.row <- cbind('R','sum',as.numeric(R.sum))
R.row <- setNames(nm=c('question','answer','value'),data.frame(R.row))
R.freq = rbind(R.freq, R.row )

R.row <- cbind('R','length',as.numeric(R.length))
R.row <- setNames(nm=c('question','answer','value'),data.frame(R.row))
R.freq = rbind(R.freq, R.row )

R.row <- cbind('R','mean',as.numeric(R.mean))
R.row <- setNames(nm=c('question','answer','value'),data.frame(R.row))
R.freq = rbind(R.freq, R.row )

R.row <- cbind('R','sd',as.numeric(R.sd))
R.row <- setNames(nm=c('question','answer','value'),data.frame(R.row))
R.freq = rbind(R.freq, R.row )

result <- rbind(R.freq,S.freq)
result <- cbind(filter='None',result)
result

我明白了

   filter question answer            value
1 None R 0 0
2 None R 1 1
3 None R 2 2
4 None R 3 1
5 None R 4 1
6 None R 5 1
7 None R 6 1
8 None R 7 1
9 None R 8 0
10 None R 9 1
11 None R 10 0
12 None R <NA> 1
13 None R sum 39
14 None R length 9
15 None R mean 4.33333333333333
16 None R sd 2.64575131106459
17 None S 0 0
18 None S 1 0
19 None S 2 0
20 None S 3 1
21 None S 4 0
22 None S 5 1
23 None S 6 1
24 None S 7 3
25 None S 8 1
26 None S 9 1
27 None S 10 0
28 None S <NA> 2
29 None S sum 52
30 None S length 8
31 None S mean 6.5
32 None S sd 1.8516401995451

这正是我要找的。我认为下一步是开始包装一些函数以简化代码,然后再开始添加来自 ClassA=1、ClassA=n+1 ... ClassA=NA、ClassB=1、ClassB= 的类似结果集2 ... ClassB=NA。有更简单的方法吗?

学习了Ernest A的答案后的新代码和 Imo

    # https://stackoverflow.com/questions/36790376/a-simpler-way-to-achieve-a-frequency-count-with-mean-sum-length-and-sd-in-r/36794422#36794422

# create the summary function
summaryStatistics <- function(x) {
xx <- na.omit(x)
c(table(factor(x, levels=0:10), useNA='always', exclude=NULL),
sum=sum(xx), length=length(x), mean=mean(xx), sd=sqrt(var(xx)))
}

# create the test data frame
Id <- c(1,2,3,4,5,6,7,8,9,10)
ClassA <- c(1,NA,3,1,1,2,1,4,5,3)
ClassB <- c(2,1,1,3,3,2,1,1,3,3)
R <- c(1,2,3,NA,9,2,4,5,6,7)
S <- c(3,7,NA,9,5,8,7,NA,7,6)
df <- data.frame(Id,ClassA,ClassB,R,S)

# create the result
result <- setNames(
nm=c('answer','question','value'),
as.data.frame(
as.table(
simplify2array(
lapply(
df[c('R', 'S')],
summaryStatistics
)
)
)
)
)

# change the order to question, answer, value
result <- result[, c(2, 1, 3)]

# add the filter
result <- cbind(filter='None',result)

# return the result
result

这要简单得多,也使我培训团队的其他任务变得更加简单。感谢Ernest AImo .

关于我对 R 的理解的下一个问题是 Using vectors in R to change the output of a function

最佳答案

减少代码大小的一个方法是将摘要统计信息包装在一个函数中:

myStats <- function(x) {
answer <- c("sum"=sum(x, na.rm = TRUE), "length"=sum(!is.na(x)),
"mean"=mean(x, na.rm = TRUE), "sd"=sd(x, na.rm = TRUE))

return(answer)
}

这将返回一个命名的摘要统计向量,按照您在输出中的顺序排序。然后,您可以rbind 返回的值以及您的频率表的名称:

R.stats <- myStats(df$R)
rbind(R.freq, data.frame("question"='R', "answer"=names(R.stats),
"value"=R.stats))

关于r - 在 R 中使用均值、总和、长度和 sd 实现频率计数的更简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36790376/

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