Abi K
Abi K

Reputation: 651

Breaking R code to the next line for file paths

Why this works:

PC<-read.csv('./public-transportation/san-francisco/passenger-count.csv', header=TRUE)

But the following doesn't work in R:

inputFile <-paste('./public-transportation/',
'san-francisco/passenger-count.csv')
PC<-read.csv(inputFile, header=TRUE)

All I am doing is passing a variable that holds the filename? Sorry, I am relatively new to R. This is kinda baffling me a bit. Any help is appreciated..

Upvotes: 2

Views: 592

Answers (1)

Joshua Ulrich
Joshua Ulrich

Reputation: 176728

This doesn't work because the default separator in paste is a space. So you need to set sep="", use paste0, or use file.path.

# paste with sep=""
inputFile <- paste('./public-transportation/',
  'san-francisco/passenger-count.csv', sep="")
# paste0
inputFile <- paste0('./public-transportation/',
  'san-francisco/passenger-count.csv')
# file.path
inputFile <- file.path('./public-transportation/',
  'san-francisco/passenger-count.csv')

Upvotes: 9

Related Questions