我已根据各种条件创建了多个数据帧.现在我想对不同的数据帧进行采样,但是我想在采样后删除它们.我试过dplyrs sample_n:
sample_n(df, 4)
问题是这不会删除行,我是否需要一些递归循环,一旦它们被采样将删除行?或者是否有一些方便的功能可以帮助我?
适合我.
# generate data a <- data.frame(letters = letters[1:5], var = rnorm(5)) b <- data.frame(letters = letters[6:10], var = rnorm(5)) c <- data.frame(letters = letters[11:15], var = rnorm(5)) xy <- list(a, b, c) set.seed(357) # set seed for reproducibility dfsample <- sample(seq_len(length(xy)), 1) # sample out one data.frame xy[[dfsample]] letters var 1 a 1.51348192 2 b -0.60657737 3 c 0.51828252 4 d -0.05352487 5 e -1.34303266 # remove random row, notice the minus sign in front of the sample xy[[dfsample]] <- xy[[dfsample]][-sample(1:nrow(xy[[dfsample]]), 1), ] xy[[dfsample]] letters var 2 b -0.60657737 3 c 0.51828252 4 d -0.05352487 5 e -1.34303266