我之前从我的数据中随机抽取了一个邮政编码样本,然后意识到我并没有在所有更高层次的统计单元中进行抽样。我有大约100万的邮政编码和7000中等输出统计单位。我希望样本有大致相同数量的邮政编码来自每个统计单位。
如何从每个高级统计单位随机抽取35个邮政编码?
我使用了下面的代码随机抽样250,000个邮政编码:
total.sample <- total[sample(1:nrow(total), 250000,
replace=FALSE),] 如何根据另一个列变量(例如高级统计单元(见下面数据结构中的msoa.rank ))指定一个随机的邮政编码样本配额?
数据库结构:
'data.frame': 1096289 obs. of 25 variables:
$ pcd : Factor w/ 986055 levels "AL100AB","AL100AD",..: 282268 282258
$ mbps2 : int 0 1 0 0 0 1 0 0 0 0 ...
$ averagesp : num 16 7.8 7.8 9.5 9.4 3.2 11.1 19.4 10.5 11.8 ...
$ mediansp : num 18.2 8 7.8 8.1 8.5 3.2 8.1 18.7 9.7 8.9 ...
$ nga : int 0 0 0 0 0 0 0 0 0 0 ...
$ x : int 533432 532192 533416 533223 532866 531394 532899 532744
$ total.dps : int 11 91 10 7 9 10 3 5 21 12 ...
$ connections.density: num 7.909 0.747 3.1 7.714 1.889 ...
$ urban : int 1 1 1 1 1 1 1 1 1 1 ...
$ gross.pay : num 36607 36607 36607 36607 36607 ...
$ p.tert : num 98.8 98.8 98.8 98.8 98.8 ...
$ p.kibs : num 70.3 70.3 70.3 70.3 70.3 ...
$ density : num 25.5 25.5 25.5 25.5 25.5 25.5 25.5 25.5 25.5 25.5 ...
$ p_m_s : num 93.5 93.5 93.5 93.5 93.5 ...
$ p_m_l : num 6.52 6.52 6.52 6.52 6.52 ...
$ p.edu : num 62.6 62.6 62.6 62.6 62.6 ...
$ p.claim : num 1.58 1.58 1.58 1.58 1.58 ...
$ p.non.white : num 21.4 21.4 21.4 21.4 21.4 21.4 21.4 21.4 21.4 21.4 ...
$ msoa.rank : int 2 2 2 2 2 2 2 2 2 2 ...
$ oslaua.rank : int 321 321 321 321 321 321 321 321 321 321 ...
$ nuts2.rank : int 22 22 22 22 22 22 22 22 22 22 ...
$ gor.rank : int 8 8 8 8 8 8 8 8 8 8 ...
$ cons : int 1 1 1 1 1 1 1 1 1 1 ...邮编
msoa.rank =每个中间输出统计单元的序数变量
发布于 2014-07-01 06:21:29
每个msoa.rank至少有35个邮政编码吗?这对data.table来说是很快的
#Create a data.table object
require(data.table)
total <- data.table(total)
#Sample by each msoa.rank group (take a sample that is size min(35,total size of msoa grp)
total.sample <- total[ , .SD[sample(1:.N,min(35,.N))], by=msoa.rank]下面是如何使用经典的iris数据集的示例。
iris < data.table(iris)
set.seed(2014)
iris.sample <- iris[ , .SD[sample(1:.N,min(10,.N))], by=Species]
summary(iris.sample$Sepal.Length)
Min. 1st Qu. Median Mean 3rd Qu. Max.
4.400 5.000 5.850 5.797 6.525 7.200 下面是另一个示例和摘要,以了解两者之间的差异
iris.sample2 <- iris[ , .SD[sample(1:.N,min(10,.N))], by=Species]
summary(iris.sample2$Sepal.Length)
Min. 1st Qu. Median Mean 3rd Qu. Max.
4.400 5.100 5.850 5.743 6.275 7.300 https://stackoverflow.com/questions/24499066
复制相似问题