[R] basic inquiry regarding write.csv

CJ Rubio cjrubio at kongju.ac.kr
Wed Feb 18 08:38:29 CET 2009


this is the code that i have so far:

> data.path <- file.path ("D:/documents/research/5 stations")
> setwd(data.path)
> getwd()

> data <- dir(".")
> num.files <- length(data)

>  for(i in 1:num.files) {
+  station.id <- substring(data[i], 1,8)
+  DF <- read.table(data[i], sep=",", blank.lines.skip = TRUE)
+  z <- zoo(DF[,4], as.Date(as.character(DF[,3]), "%m/%d/%Y"))
+  f <- function(x) time(x) [which.max(x)]
+  ix <- tapply(z, floor(as.yearmon(time(z))),f)
+  year <- (1988:2005)
+  date <- time(z[ix])
+  max.discharge <- coredata(z[ix])
+  data.info <- rbind(data.info, cbind(station.id, year, date,
max.discharge))
+  y <- split(data.info, data.info[station.id])
+  for (i in names(y)) {write.csv(y[[i]], file=paste(i, ".csv", sep=","))}
+ }

i have done the suggestion you gave me..  and there were no errors when i
run it.
hope this would further clarify my question.



So far, I cannot see any mistake, though the sep="" will be more
elegant that sep=",". Are you sure your working directory is
"E:/my_work_directory"? and is there any error msg?

BTW, a reproducible example will help to get better response from the list.

2009/2/18 CJ Rubio <cjrubio at kongju.ac.kr>:
>
> thanks for your reply.. is there something wrong with the code i have?
> because it doesn't write the file in the directory that i am using...
>
> for (i in names(y))
>>         {write.csv(y[[i]], file=paste(i, ".csv", sep=","))}
>
> thanks again..
>
>
>
> ronggui-2 wrote:
>>
>> If the "file" is a relative path, then it should be in the working
>> directory. Say, the working directory is E:/my_work_directory (of
>> course, you can get it by getwd()), and you export a data frame "a" to
>> csv by:
>> write.csv(a, file="a.csv"), then the file should be
>> "E:/my_work_directory/a.csv".
>>
>> Best
>>
>> 2009/2/18 CJ Rubio <cjrubio at kongju.ac.kr>:
>>>
>>> i have a loop which looks likes this:
>>>
>>>
>>>> data.info <- rbind(data.info, cbind(station.id, year, date,
>>>> max.discharge))
>>> +  y <- split(data.info, data.info[station.id])
>>> +  for (i in names(y))
>>>         {write.csv(y[[i]], file=paste(i, ".csv", sep=","))}
>>>
>>> i am wondering, where the file (which i am about to write in .csv
>>> format)
>>> will be saved? i looked at ?write.csv and it says there that :
>>>
>>> file                   either a character string naming a file or a
>>> connection open for writing. "" indicates output to the console.
>>>
>>> correct me if i'm wrong but the way i undestand it is, i should have a
>>> file
>>> or a working directory where the .csv will be written.  if for example i
>>> have a working directory "E:/my_work_directory", how can i save this
>>> splitted files in the same directory?
>>>
>>> can anybody please enlighten me more with write.csv and the argument
>>> file?
>>>
>>> thanks..
>>>
>>> --
>>> View this message in context:
>>> http://www.nabble.com/basic-inquiry-regarding-write.csv-tp22073053p22073053.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> 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.
>>>
>>
>>
>>
>> --
>> HUANG Ronggui, Wincent
>> Tel: (00852) 3442 3832
>> PhD Candidate
>> Dept of Public and Social Administration
>> City University of Hong Kong
>> Homepage: http://ronggui.huang.googlepages.com/
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> 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.
>>
>>
>
> --
> View this message in context:
> http://www.nabble.com/basic-inquiry-regarding-write.csv-tp22073053p22073393.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>



-- 
HUANG Ronggui, Wincent
Tel: (00852) 3442 3832
PhD Candidate
Dept of Public and Social Administration
City University of Hong Kong
Homepage: http://ronggui.huang.googlepages.com/

______________________________________________
R-help at r-project.org mailing list
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.



-- 
View this message in context: http://www.nabble.com/basic-inquiry-regarding-write.csv-tp22073053p22073615.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list