当我提供大小向量时,splitstackshape :: stratified函数出现错误,如果您要跨层设置不同的采样配额(分层不成比例抽样中相当普遍的情况),这是必需的。错误涉及带有“ setnames”的函数部分。这是一个可重现的示例:
require(data.table)
require(splitstackshape)
V1<-as.factor(rbinom(1000,2,0.4))
V2<-as.factor(rbinom(1000,2,0.3))
levels(V1)<-c("apple", "banana", "orange")
levels(V2)<-c("America", "Europe", "Africa")
df <- data.frame(V1,V2)
df<-as.data.table(df)
#population counts by groups
countbygroup<-df[df$V1 %in% c("apple", "banana") & df$V2 %in% c("Europe", "Africa"),.N,.(V1,V2)]
#take 50% observations from all groups in the population
size_vector<-round(countbygroup$N*0.50,0)
names(size_vector)<-paste(countbygroup$V1, countbygroup$V2, sep=" ")
#...except for one group, where I want to sample all of the population observations
size_vector[names(size_vector) %in% c("apple Africa")] <- countbygroup[V1 == "apple" & V2 == "Africa",N]
#An error occurs.
sample<-stratified(df, group = c("V1", "V2"),
select = list(V1 = c("apple", "banana"), V2 = c("Europe", "Africa"))
, size = size_vector)
这是我得到的错误:
setnames(data.table(names(size),ss = size),c(group,“ ss”))中的错误 :无法为2列数据分配3个名称。表
您对发生的事情有任何了解吗?