1

I'd like to create a list of multiple samples from a dataframe and join them back into the same number of list of dataframes.

In the example below, I create the data frame, replicate it 10 times and add a column of "0". I then take a N=3 sample the original df 10 times and give these "1".

I am having trouble joining the 10 larger dataframes with the list of samples.

Below is the example and the join I'm attempting. I've tried the all of the join variants in https://dplyr.tidyverse.org/reference/join.html but can't get close to what I am looking for.

library(tidyverse)

name <- c('A','B','C','D','E','F','G','H','I','J','K')
value <- c(3,5,2,4,7,2,9,3,7,3,7)

df <- data.frame(name,value)
colnames(df) <- c("name", "value")

dflist <- replicate(10, df, simplify = F)
dflist1 <- lapply(dflist, function(x){mutate(x, labels=0)})

dfsamp <- replicate(10, df %>% sample_n(3), simplify = F)
dfsamp1 <- lapply(dfsamp, function(x){mutate(x, labels=1)})

finaldf <- list()

for( i in 1:length(dflist))
{
  finaldf <- c(finaldf, dflist1[[i]] %>% nest_join(dfsamp1[[i]]))
}

I would expect something like the following for all 10 edited data frames:

names values label
A     3      0
B     5      1
C     2      0
D     4      1
E     7      0
F     2      0
G     9      0
H     3      0
I     7      1
J     3      0
K     7      1

Any help would be appreciated. Thanks

0 Answers0