rloopspaste

Changing variable name for multiple csv file in loop R


I am new to R so I am confused with the data structure. Basically I have 10 csv files, from 2011-2021, named kbr_year.csv respectively. This csv file contains dataframes, of which I only want to retrieve data from a column of the dataframe. And with that I want to use the loop, so that I can read all of the files, and then appending it as a character. However, I ran into problem, as the code does not execute the loop.

Here is how I'd read the code one by one:

text2021 <- read.csv2("kbr_2021.csv", header=TRUE, sep=";")
text2021_2 <- text2021[!duplicated(text2021$Full.context), ]
text2021_chr <- as.character(text2021_2["Full.context"])

And here is how I execute the loop, using the paste function to make sure that the file name changes as the loop execute:

for (i in 2011:2021)
{
  k <- c()
  lk <- paste("kbr_", i, sep="")
  lk2 <- paste(lk, ".csv", sep="")
  t <- paste("text", i, sep="")
  t <- read.csv2(lk2, header=TRUE, sep=";")
  t <- t[!duplicated(t$Full.context), ]
  k <- as.character(t["Full.context"])
}

However, I only get 1 file read, and that is the file from 2021.

How can I make sure that the loop execute from 2011-2021?

Thanks

I tried to use the paste() function and execute it on a loop

for (i in 2011:2021)
{
  k <- c()
  lk <- paste("kbr_", i, sep="")
  lk2 <- paste(lk, ".csv", sep="")
  t <- paste("text", i, sep="")
  t <- read.csv2(lk2, header=TRUE, sep=";")
  t <- t[!duplicated(t$Full.context), ]
  k <- as.character(t["Full.context"])
}

But it did not work


Solution

  • Your loop would work better like this as you're overwriting k at the beginning of each iteration of the for loop

    kList <- c()
    fileList <- list.files(path=paste("kbr_", i, ".csv", sep=""),pattern='.csv')
    for (file in fileList)
    {
      dfT <- read.csv2(file)
      dfT <- dfT[!duplicated(dfT$Full.context), ]
      kList <- c(kList,dfT$Full.context)
    }