Use lapply to make three data.frames captured in a list l, each composed of one randomly sampled column v1 (selecting from integers 1:10, with length = 20), and the second being v2 composed of lowercase letters, randomly selected using sample, also of length 20.
The iterator in the lapply should be 10, 20, 30, which become the random seeds for the sampling (in the body of the lapply)
After making l, use a for loop to iterate through each element of l, writing each out to a folder external/data/ in your project.
Change the name of each as part of the iteration, so that l[[1]] is written out as external/data/dataset1.csv, etc. Hint: you can use paste0 to make each file path and name.
After writing these out, use another lapply to read back in the three datasets into a new list l2. Bonus: Use dir to programmatically read in the file paths from your external/data folder.
Working with data, continued
do.call / bind_rows
sz <-100for(i in1:3) {set.seed(i) d <-data.frame(id =1:sz,v1 =runif(sz, min =2, max =12),grp =paste0("g", i) ) %>%mutate(v2 = v1 +rnorm(sz, mean =2, sd =2)) %>%select(id, v1, v2, grp) readr::write_csv(d, file =file.path(tempdir(), paste0("dataset", i, ".csv")))}# ggplot(d) + geom_point(aes(x = v1, y = v2))fs <-list.files(tempdir(), pattern ="dataset.*.csv", full.names =TRUE)# dat <- do.call(rbind, lapply(fs, readr::read_csv))dat <-bind_rows(lapply(fs, readr::read_csv))plot(dat$v1, dat$v2)