0

I am new to R and would like to read in a list of files as separate data frames, perform several operations on each, and save them out as separate files with dynamic file names. I am thinking I should use lappy, but not sure.

Here is the code I wrote that works for one file:

df <- read.fwf('USC00011084.dly', widths = c(21, rep(c(5, 1, 1, 1),31)))

df2 <- df[-c(3:5, 7:9, 11:13, 15:17, 19:21, 23:25, 27:29, 31:33, 35:37, 39:41, 43:45, 47:49, 51:53, 55:57, 59:61, 63:65, 67:69, 71:73, 75:77, 79:81, 83:85, 87:89, 91:93, 95:97, 99:101, 103:105, 107:109, 111:113, 115:117, 119:121, 123:125)] 

df2[df2=="-9999"]<-NA

df$new <- rowSums(df2[,2:32], na.rm = TRUE)

df2["Total"] <- df$new

colnames(df2) <- c("StationDateType", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "28", "30", "31", "TotalMonthly")

Prcp <- df2[grep("PRCP", df2$StationDateType),]

write.table(Prcp, "USC00011084Prcp.txt", sep="\t", row.names=FALSE)

How can I do this for a list of files in a directory? Any ideas? Thank you.

Raad
  • 2,675
  • 1
  • 13
  • 26
  • 2
    What does "dynamic" mean to you? And have you searched on `list.files`? There must be hundreds of similar _already_ _answered_ questions on SO. – IRTFM Feb 25 '17 at 19:43
  • Thanks - I was able to use the recommendation from the other author, as well as piece together an alternative using Sys.glob. – user2267031 Feb 26 '17 at 19:55

1 Answers1

0

You can try this... You can get a list of your files:

files <- list.files(getwd())

Write a function that performs the analysis you want and writes the results to table, as you have done. Here we use tools::file_path_sans_ext to extract the filename (without the file type extension), and at the end use it to name the table to be saved to txt.

myFunction <- function(files){
  fileName <- tools::file_path_sans_ext(files)
  df <- read.fwf(files, widths = c(21, rep(c(5, 1, 1, 1),31)))
  # rest of your code
  # ...
  write.table(Prcp, paste0(fileName, "Prcp.txt"), sep="\t", row.names=FALSE)
}

You can use lapply to run your function on each file in files.

lapply(files, function(x) myFunction(x))
Djork
  • 3,319
  • 1
  • 16
  • 27
  • @ R.S. Thank you for this. I followed your instructions and it works well. I have one question. After I run lapply on six test files, I receive the following: [[1]] NULL [[2]] NULL [[3]] NULL [[4]] NULL [[5]] NULL [[6]] NULL Any idea what this means? The .txt files that were generated look fine. Thx again. – user2267031 Feb 26 '17 at 19:38
  • @user2267031 `lapply` outputs to a list of same length the results of applying your function to the initial list, which in this case is the last line of myFunction. Since the last line is `write.table`, there is nothing to return. If you like, you can add this line after write.table `print(paste("Saving:", paste0(fileName, "Prcp.txt")))` and see the output. – Djork Feb 26 '17 at 19:54