giorgioconti
giorgioconti

Reputation: 53

Reading files from loops

I have the following part of the code that contains two loops. I have some txt files, which I want to be read and analyzed in R separately, one by one. Currently, I face a problem of importing them to R. For example, the name of the first file is "C:/Users/User 1/Documents/Folder 1/1 1986.txt". To read it in R I have made the following loop:

## company
for(i in 1)
{
  ## year
  for(j in 1986)
  {
    df=read.delim(paste("C:/Users/User 1/Documents/Folder 1/", i, j, ".txt"), stringsAsFactors=FALSE, header=FALSE)
    df<-data.frame(rename(df, c("V3"="weight")))
  }
}

When I run the loop, I get the following error:

Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
  cannot open file 'C:/Users/User 1/Documents/Folder 1/ 13 1986 .txt': No such file or directory

How do I avoid those additional gaps that R assumes to exist in the name of the original file?

Upvotes: 0

Views: 124

Answers (3)

JackStat
JackStat

Reputation: 1653

I would avoid the loop in this case and go with lapply.

Files <- list.files('C:/Users/User 1/Documents/Folder 1/', pattern = "*.txt")

fileList <- lapply(Files, FUN =- function(x){
  df <- read.delim(x, stringsAsFactors=FALSE, header=FALSE)
  df <- data.frame(rename(df, c("V3"="weight")))
  return(df)
})

do.call('rbind', fileList)

Upvotes: 1

Marie-Louise
Marie-Louise

Reputation: 419

Because I don't know how your files look like exactly, maybe this won't help you... But this is how I read in files with a loop:

First: setting the working directory

setwd("/Users/User 1/Documents/Folder 1")

Then I always save my data as one excel file with different sheets. For this example I have 15 different sheets in my excel file named 2000-2014, the first sheet is called "2000", the second "2001" and so on.

sheets <- list() # creating empty list named sheets
for(i in 1:15){
 sheets[[i]] <- read_excel("2000-2014.xlsx", sheet = i) # every sheet will be one layer of the list sheets
 k <- c(2000:2014)
 sheet[[i]]$Year <- k[i] # to every listlayer I add a column "Year", matching the actual year my data is from 
}

No I want my data from 2000 to 2014 merged in one big data frame. I can still analyse them one by one!

data <- do.call(rbind.data.frame, sheets)

To tidy my data all in one and to get it into the form Hadley Wickham and ggplot2 like it (http://vita.had.co.nz/papers/tidy-data.pdf) I restructure it:

data_restructed <- data %>% 
  as.data.frame() %>% 
  tidyr::gather(key = "categories", value = "values", 2:12) 

2:12 because in my case columns 2:12 contain all the values while column 1 contains countrienames. Now you have all your data in one big dataframe and can analyse them seperated to specific variables like the year or the category or year AND category and so on.

Upvotes: 1

m0nhawk
m0nhawk

Reputation: 24258

You should replace paste with paste0.

By default, paste use spaces as a separator, thus yielding the obtained result. And paste0 use nothing as a separator.

Upvotes: 1

Related Questions