Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- participant <- 1:30
- tasks <- c('a','b','c')
- session <- 1:20
- # Make sure our folder exists for saving into.
- baseDir <- './AllTheFiles/'
- if (!dir.exists(baseDir)) {dir.create(baseDir)}
- # Here is a pretty traditional computer-languagy way to do it:
- for (p in participant) {
- for (tsk in tasks) {
- for (s in session) {
- # This step just generates random data to write.
- currentDf <- data.frame(
- rndNum1 = rnorm(10),
- rndNum2 = runif(10)
- )
- # Construct a different file name for each case.
- flNm <- paste(p, tsk, s, sep = "_")
- # Write the file. Note the paste0() statement, which ensures that the
- # files end up in a folder called "AllTheFiles" (in the current working
- # directory). It also adds the txt file extension.
- write.table(
- currentDf,
- file = paste0(baseDir, flNm, ".txt")
- )
- }
- }
- }
- # A slightly more R-y way to do it
- combos <- expand.grid(participant, tasks, session, stringsAsFactors = F)
- apply(combos, MARGIN = 1, function (combo) {
- # This code is the same as in the for-loop way, but referencing the combos
- # object instead of looping directly on the original objects.
- p <- combo[1]
- tsk <- combo[2]
- s <- combo[3]
- currentDf <- data.frame(
- rndNum1 = rnorm(10),
- rndNum2 = runif(10)
- )
- flNm <- paste(p, tsk, s, sep = "_")
- write.table(
- currentDf,
- file = paste0(baseDir, flNm, ".txt")
- )
- }
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement