获取摘要函数中的分位数结果。

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

Getting the quantile result right in a summary function

问题

以下是你要的翻译部分:

我有这些数据

```R
dput(per_stack[1:4,])

它产生了以下输出:

结构(list(articles_ponderats2_migpunts = c(20.2083333333333, 
20.6666666666667, 23.8333333333333, 15.1666666666667), Tramspoblacio = structure(c(5L, 
4L, 4L, 4L), .Label = c("1 - 999", "1000 - 4999", "5000 - 9999", 
"10000 - 19999", "20000 - 49999", "50000 - 99999", "100000 - 200000", 
"Mayor de 200000"), class = "factor")), row.names = c(NA, 4L), class = "data.frame")

我已经创建了一个自定义函数来进行汇总

funcio_resum <- function(x) {
  n <- NROW(x)
  mean <- mean(x)
  median <- median(x)
  min <- min(x)
  max <- max(x)
  q1 <- quantile(x, 0.25)
  q3 <- quantile(x, 0.75)
  SD <- sd(x)
  IQR <- IQR(x)
  quantile <- quantile(x, probs = seq(0.1, 0.9, by = 0.1))
  hist <- hist(x)
  summary <- list(n = n, mean = mean, median = median, max = max, min = min, q1 = q1, q3 = q3, SD = SD, IQR = IQR)
  return(summary)
}

然后我应用它,然后将其转换为表格

resum <- tapply(index_articles_migpunts$articles_ponderats2_migpunts, index_articles_migpunts$Tramspoblacio, funcio_resum)
resum <- do.call(rbind, resum)

它可以正常工作,我得到了我想要的汇总表格,但是q1和q3列中的结果是这样的向量

q1
c(`25%` = 5)
c(`25%` = 11.4375)

而不是像平均值、中位数等一样简单显示结果。

q1
5
11.4375

我该如何改变这个?对于其他函数,它可以正常工作(平均值、最小值、最大值等),但对于q1和q3却不行...

我认为我可能需要更改函数的列表参数或者可能在do.call部分进行更改,但我不知道如何做...

谢谢!!



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

I have this data

dput(per_stack[1:4,])


which gives this output:

structure(list(articles_ponderats2_migpunts = c(20.2083333333333,
20.6666666666667, 23.8333333333333, 15.1666666666667), Tramspoblacio = structure(c(5L,
4L, 4L, 4L), .Label = c("1 - 999", "1000 - 4999", "5000 - 9999",
"10000 - 19999", "20000 - 49999", "50000 - 99999", "100000 - 200000",
"Mayor de 200000"), class = "factor")), row.names = c(NA, 4L), class = "data.frame")

I have created a custom-made function for doing the summary

funcio_resum <-function(x) {
n<-NROW(x)
mean<-mean(x)
median <- median (x)
min<-min (x)
max<-max(x)
q1 <- quantile(x, .25)
q3 <- quantile(x, .75)
SD<- sd(x)
IQR <- IQR(x)
quantile <- quantile(x, probs=seq(.1,.9, by =.1))
hist<-hist(x)
summary<-list(n=n,mean=mean, median = median, max=max,min=min, q1 = q1, q3 = q3, SD=SD, IQR = IQR)
return(summary)
}


Then I apply it and afterwards transform it to a table

resum <- tapply(index_articles_migpunts$articles_ponderats2_migpunts, index_articles_migpunts$Tramspoblacio, funcio_resum)
resum <- do.call (rbind, resum)


It works and I get the summary table I&#39;m searching for, but the result in the q1 and q3 columns is a vector like this


q1
c(25% = 5)
c(25% = 11.4375)


Instead of simply showing the result as it does with mean, median, etc. 

q1
5
11.4375


How can I change this? With the other functions it works fine (mean, min, max, etc.) but with q1 and q3 it does not... 

I think I may have to change something in the list parameter of the function or maybe in the do.call part, but I do not know how to do it... 

Thank you!!



</details>


# 答案1
**得分**: 1

以下是要翻译的内容:
你现在看到的是一个命名向量。要去掉这些名称,你可以在你的函数中使用以下代码:

q1 <- unname(quantile(x, .25))
q3 <- unname(quantile(x, .75))


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

What you are seeing is a named vector. To get rid of the names you can use 

q1 <- unname(quantile(x, .25))
q3 <- unname(quantile(x, .75))

in your function.

</details>



huangapple
  • 本文由 发表于 2023年3月3日 20:06:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626858.html
匿名

发表评论

匿名网友

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

确定