I am trying to write a function to read files from a directory and then either print the head of the file or summary of the head of the file in R. My code is as below...
getmonitor <- function(id, directory, summarize = FALSE) {
if(id>=1 && id<10) {
te1 <- paste("00",id,sep="")
#print(te1)
} else if(id>10 && id<=99) {
te1 <- paste("0",id,sep="")
#print(te1)
} else {
te1 <- id
#print(te1)
}
filename = paste(directory, te1, sep = "/")
filename1 = paste(filename, "csv", sep = ".")
filename1
test <- read.csv(file = filename1)
if(summarize==TRUE) {
test1 <- summary(test)
} else {
test1 = test
}
return (test1)
}
When i use this function without summarize option it works fine as below....
data <- getmonitor(1, "specdata")
> head(data)
Date sulfate nitrate ID
1 2003-01-01 NA NA 1
2 2003-01-02 NA NA 1
3 2003-01-03 NA NA 1
4 2003-01-04 NA NA 1
5 2003-01-05 NA NA 1
6 2003-01-06 NA NA 1
But when i use the summary option then i getting the output with all quotes around the lines as below...
data <- getmonitor(101, "specdata", TRUE)
> head(data)
Date sulfate nitrate ID
"2005-01-01: 1 " "Min. : 1.700 " "Min. : 0.2490 " "Min. :101 "
"2005-01-02: 1 " "1st Qu.: 3.062 " "1st Qu.: 0.6182 " "1st Qu.:101 "
"2005-01-03: 1 " "Median : 4.345 " "Median : 1.0500 " "Median :101 "
"2005-01-04: 1 " "Mean : 6.267 " "Mean : 2.2679 " "Mean :101 "
"2005-01-05: 1 " "3rd Qu.: 7.435 " "3rd Qu.: 2.7825 " "3rd Qu.:101 "
"2005-01-06: 1 " "Max. :22.100 " "Max. :10.8000 " "Max. :101 "
I don't want any of the quotes for the lines. I even tried converting this into df but doesn't work. Where am i doing wrong?