R中具有加权数据的频率表 [英] Frequency tables with weighted data in R
本文介绍了R中具有加权数据的频率表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要按年龄和婚姻状况计算出个人的频率,因此通常我会使用:
I need to calculate the frequency of individuals by age and marital status so normally I'd use:
table(age, marital_status)
但是,数据采样后每个人的权重都不同.如何将其纳入频率表?
However each individual has a different weight after the sampling of the data. How do I incorporate this into my frequency table?
推荐答案
您可以使用软件包survey
中的函数svytable
,也可以使用rgrs
中的wtd.table
.
You can use function svytable
from package survey
, or wtd.table
from rgrs
.
rgrs
现在称为questionr
:
df <- data.frame(var = c("A", "A", "B", "B"), wt = c(30, 10, 20, 40))
library(questionr)
wtd.table(x = df$var, weights = df$wt)
# A B
# 40 60
使用dplyr
也是可能的:
library(dplyr)
count(x = df, var, wt = wt)
# # A tibble: 2 x 2
# var n
# <fctr> <dbl>
# 1 A 40
# 2 B 60
这篇关于R中具有加权数据的频率表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文