[R] problem with creating a file path using paste0

David Winsemius dwinsemius at comcast.net
Wed Dec 2 07:40:18 CET 2015


> On Dec 1, 2015, at 8:01 PM, Carl Sutton via R-help <r-help at r-project.org> wrote:
> 
> I am relatively new to programming in R.   
> I have attempted to create a function to replace brute force code, and in doing so need to create a data path and file name via paste0 (The years and quarter subfolders are different for different years and quarters).   It file path created from paste0 code looks perfect to me but bombs.  I suspect the problem lies with : and / being r functions and thus changing them to character is a problem.

Forward slashes shoudl be acceptable for any OS.


>  However, if they are unquoted I get more error messages.   Identical states the two character strings are not identical.  Have I done something stupid or is there something happening I am not aware of.  Code script from RStudio follows: then console window results.
> require(data.table)
> #  load column names
> load("profileColNames")
> #  Start loading data, read in data, convert to data table for each quarterdataRead <- function(year,quarter){
>     years <- year-2000
>     years <- as.character(years)
>     quarter <- as.character(quarter)
>     print (years)
>     qtrName <- paste0("profile",years,"q",quarter)
>     print (qtrName)
>     qtr <- paste0("Q",quarter)
>     print (qtr)
>     filePath <- paste0("c",":",/year/,"Q",quarter,"_",year/,"pL_profile.txt")     note- this is the problem child!!!!

Right. Why are you putting forward slashes _outside_ the quotes??? Aren’t you getting errors from this.

I will parse out your arguments:

paste0("c”,
         ":”,
        /year/,    # It was spelled “years” above
        "Q”,
       quarter,
       "_”,
        year/,     # The only reason the misspelling of `years` is not being caught is the slash triggered an error first.
        "pL_profile.txt")


If those forward slashes are some misguided effort or spawn-of-HTML-demons to emphasize something, then you also need to pay attention to the posting guide when it asks for plain text. Try

filePath <- paste0("c:/“, years, “/Q”, quarter, "_”, years, “/pL_profile.txt")


> #what I want the above to be
> #    "c:/2012/Q1_2012/pL_profile.txt", 
>     print (filePath)
> #  the following line does not work    s


Well, I didn’t think it looked very promising.


>                                    without a correct file path it can't work
>     #qtrName <- read.csv(file = fileName, header = FALSE,
>      #                   stringsAsFactors = FALSE)
>     #names(qtrName) <- profileColNames
>    # return(qtrName)
> }
> data <- dataRead(2012,1)
> data
> identical(data,"c:/2012/Q1_2012/pL_profile.txt")
> #  the folowing typed line works
> profile12q1 <- read.csv (file = "c:/2012/Q1_2012/pL_profile.txt", 
>                          header = FALSE, sep = ",", stringsAsFactors=FALSE)
> and results from the console from first error message:
> 
> | +     print (qtr)
> +     filePath <- paste0("c",":",/year/,"Q",quarter,"_",year/,"pL_profile.txt")
> Error: unexpected '/' in:


Yes, as I suspected. WTF is the slash doing outside the quotes?


> "    print (qtr)
>    filePath <- paste0("c",":",/"
>> #what I want the above to be
>> #    "c:/2012/Q1_2012/pL_profile.txt", 
>>    print (filePath)
> Error in print(filePath) : object 'filePath' not found
>> #  the following line does not work
>>    #qtrName <- read.csv(file = fileName, header = FALSE,
>>     #                   stringsAsFactors = FALSE)
>>    #names(qtrName) <- profileColNames
>>   # return(qtrName)
>> }
> Error: unexpected '}' in "}"
>> data <- dataRead(2012,1)
> [1] "12"
> [1] "profile12q1"
> [1] "Q1"
> [1] "c:/2012/Q1_2012/pL-profile.txt"
>> data
> [1] "c:/2012/Q1_2012/pL-profile.txt"
>> identical(data,"c:/2012/Q1_2012/pL_profile.txt")
> [1] FALSE
>> #  the folowing typed line works
>> profile12q1 <- read.csv (file = "c:/2012/Q1_2012/pL_profile.txt", 
> +                          header = FALSE, sep = ",", stringsAsFactors=FALSE)
>> dim(profile12q1)
> [1] 74971   574
>> class(profile12q1)
> [1] "data.frame"
> |
> |  |
> | 
> | >  |
> 
> |
> 
>  Carl Sutton
> 
> 	[[alternative HTML version deleted]]

Plain text, plain text, PLAIN TEXT.

 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html

> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> 
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list