-3

I want to read csv files and then append the column names in each object based on the filename as follows:

for (fname in c("abc", "def", "ghi")) {
    assign(fname, read.csv(paste(HOMEDIR, fname, pnl.csv, sep="/")))
    names(?) = paste(names(?), fname, sep="."))
}

I am facing issues in the renaming part. What should replace ??

I have tried get(fname), as.name(fname), as.symbol(fname), etc.

kaushal agrawal
  • 360
  • 3
  • 21
  • hi, you should consider importing your files in a list, it would make it much easier to work with them afterwards, moreover if you want to apply the same codes to each – Cath Feb 28 '19 at 07:18
  • I feel like a broken record. Forget that `assign` exists. Your approach only leads to further pain ahead. Learn to use a list for a collection of objects, especially if you want to loop over them. – Roland Feb 28 '19 at 07:22
  • 1
    Relevant post, keep your files in a list object: https://stackoverflow.com/questions/11433432/how-to-import-multiple-csv-files-at-once – zx8754 Feb 28 '19 at 07:32
  • I don't understand why my answer is deleted? @zx8754 can you please explain why? – Sai Prabhanjan Reddy Feb 28 '19 at 07:46
  • 2
    @SaiPrabhanjanReddy All the answers should be deleted... question is wrong. The right solution to actual problem is in linked post, as simple as `myfiles <- lapply(list.files(pattern="*.csv"), read.delim)` – zx8754 Feb 28 '19 at 07:50
  • Then how come one answer exist till on the list? And if the question is wrong, that is our duty to say the posted person to write it with an example or with more clarity, etc.. but not delete the answers. Correct me if i am wrong. Thanks – Sai Prabhanjan Reddy Feb 28 '19 at 07:56
  • @zx8754 thanks for your unkind words. Few clarifications: 1) I believe the question is quite correct and unambiguous, since I got an accepted solution by Roland. 2) What you're telling is definitely **NOT** the right solution because you are answering a different question altogether - I do not have the urge to read all csv files, and they are NOT located in a single directory. – kaushal agrawal Feb 28 '19 at 07:59
  • 2
    Roland provided the answer, yes it works. Of course it is your choice. Using the linked post we can get a better solution: `myData <- lapply(c("abc", "def", "ghi"), function(i)read.csv(paste(HOMEDIR, i, "pnl.csv", sep = "/")))`. No need for `get/assign/forloop/as.name/as.symbol`. – zx8754 Feb 28 '19 at 08:07
  • 3
    @SaiPrabhanjanReddy I realize that you couldn't test your answer because there is no minimal reproducible example provided, but your answer was simply wrong. I wouldn't have deleted it, a downvote is the appropriate action in such a case. Kaushal, personally I would use `lapply` but a `for` loop is OK too and easier for beginners. – Roland Feb 28 '19 at 08:07
  • 2
    Please do not see my comments as personal, see it as info only. No offence intended. Apologies. – zx8754 Feb 28 '19 at 08:07
  • Thanks for the inputs @zx8754. This is quite elegant and helpful. I was trying to use lapply as you mentioned earlier, but as a beginner faced issues. – kaushal agrawal Feb 28 '19 at 08:09

1 Answers1

1

Use a list. You cannot combine get with names<- assignment because get returns a temporary copy of the object. And obviously you don't need to. Your approach is bad practice. Use a list.

mylist <- list()

for (fname in c("abc", "def", "ghi")) {
    mylist[[fname]] <- read.csv(paste(HOMEDIR, fname, pnl.csv, sep="/"))
    names(mylist[[fname]]) <- paste(names(mylist[[fname]]), fname, sep="."))
}
Roland
  • 127,288
  • 10
  • 191
  • 288