Need Help Executing Code Onto Multiple Files

I have the following code:

  1. files = list.files(path = "documents", pattern = "csv$")
  2. data = lapply(files, read.csv)
  3. data = as.data.frame(data)
  4. data = as.data.frame(t(data)
  5. data = dplyr::as_tibble(data, rownames = "X")

When I perform the last three lines of code on each csv file, they work perfectly fine. My issue is that I want to run this code on all csv files, but not all the files have the same number of rows and columns, and therefore I cannot run everything in one code like I've included above. How can I get around this issue of having different numbers of columns/rows per csv file?

I think it will work if you use lapply() at each step. This is the way to work with lists of things. The advantage of lists is that the things don't need to be the same type or shape.

files <- list.files(path = "documents", pattern = "csv$")
data <- lapply(files, read.csv)
data <- lapply(data, as.data.frame)
data <- lapply(data, t)
data <- lapply(data, dplyr::as_tibble, rownames = "X")

This seems to work pretty well! I have a general understanding of lapply/apply functions, however, could you explain to me the difference between apply and lapply?

It's all there in the help.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.