【发布时间】:2020-06-29 16:15:58
【问题描述】:
我的样本数据如下:
panelID= c(1:50)
year= c(2005, 2010)
country = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J")
urban = c("A", "B", "C")
indust = c("D", "E", "F")
sizes = c(1,2,3,4,5)
n <- 2
library(data.table)
library(dplyr)
set.seed(123)
DT <- data.table( country = rep(sample(country, length(panelID), replace = T), each = n),
year = c(replicate(length(panelID), sample(year, n))))
DT [, uniqueID := .I] # Creates a unique ID
DT[DT == 0] <- NA
DT$sales[DT$sales< 0] <- NA
DT <- as.data.frame(DT)
DT <- DT %>%
group_by(country) %>%
mutate(base_rate = as.integer(runif(1, 12.5, 37.5))) %>%
group_by(country, year) %>%
mutate(tax_rate = base_rate + as.integer(runif(1,-2.5,+2.5)))
我想创建一个额外的变量Vote,对于每个国家/地区-年份对,它要么是 1 要么是 0。
然后是另一个变量 Vote_won,如果是 Vote==1,则为 1 或 0。
我试过了:
DT <- DT %>%
group_by(country, year) %>%
mutate(Vote = sample(c(0,1),3)) %>%
group_by(country, year) %>%
mutate(Vote_won = ifelse(Vote=1, sample(c(0,1),1),0))
但它说:
sample.int(length(x), size, replace, prob) 中的错误: 'replace = FALSE' 时不能抽取大于总体的样本
【问题讨论】: