r - Resample with replacement by group -
this first post please bear me. below small sample of data. actual dataset has on 4,000 individual ids , each id can have anywhere 1 2 hundred separate dollar amounts assigned it.
id dollars 001 17000 001 18000 001 23000 002 64000 002 31000 003 96000 003 164000 003 76000
what i'm trying can best explained using example. want generate 5 random samples, replacement, each id. each sample have size of 5 or 5 randomly sampled dollar values. final result have 20,000 separate samples (5 samples, per 4000 ids, each containing 5 randomly selected dollar amounts id). doing in order compare distributions of dollars in each sample fellow samples same id.
as of right now, i'm attempting garner such answer using code referenced below. should point out when run script receive error 'results must atomic'. i'm not sure if need add additional steps or what.
x <- function(func) { func<-(lapply(1:5, function(i) sample(data$dollars, size=5, replace=true))) } grouped.samples<-ddply(data,.variables="id",.fun=x)
i’m sorry in advance if question posed unclear; had difficulty articulating problem i'm having.
thanks in advance help
using data.table
:
library(data.table) dt = as.data.table(your_df) dt[, dollars[sample.int(.n, 5, true)], = id] # id v1 # 1: 1 17000 # 2: 1 18000 # 3: 1 18000 # 4: 1 23000 # 5: 1 17000 # 6: 2 31000 # 7: 2 31000 # 8: 2 31000 # 9: 2 31000 #10: 2 64000 #11: 3 96000 #12: 3 96000 #13: 3 76000 #14: 3 164000 #15: 3 76000
Comments
Post a Comment