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. 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!!!! #what I want the above to be # "c:/2012/Q1_2012/pL_profile.txt", print (filePath) # the following line does not work 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: " 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]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.