0

I am uploading multiple files containing the same type of information but different values. Then I create a list to manage the data easier with:

filenames <- list.files(path = "D:/Paths",
                        pattern = "path-+.*txt")

paths <- lapply(filenames, function(x) read.delim(x, header=F))

But the name of the paths disappear once I execute the second command, any suggestion to keep the name for each dataframe inside the list()?

Biocrazy
  • 403
  • 2
  • 15
  • Try with `setNames` or `names` i.e. `names(paths) <- filenames` – akrun Dec 11 '17 at 16:52
  • In particular, you can do `lapply(setNames(, filenames), function(x) ...)` – Frank Dec 11 '17 at 16:54
  • I am trying [to do what they explain in this link](https://stackoverflow.com/questions/36880068/for-loop-use-in-r-to-use-multiple-files-in-a-directory) but names keep disappearing. I tried with your suggestions but I get the following error: `Error in match.fun(FUN) : argument "FUN" is missing, with no default` – Biocrazy Dec 11 '17 at 17:01

1 Answers1

2

Use sapply which keeps names by default. Also no need for an anonymous function, you can just pass header = F through with ... like this:

paths = sapply(filenames, read.delim, header = F, simplify = FALSE)

(simplify = FALSE ensures that you get a list output just like lapply.)

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294