Dear list, How to convert a character to a filename? such as: x <- "height" # "height" here is actually a name of a colume in a data frame filename <- paste("plant,x") write.csv (data, file="C:/plant/filename.csv) # having trouble with this statement, how to 'write' the filename here? All I want from above is to write 'data' to a file named plantheight.csv (the ultimate goal is to use a loop to transfer each column of a data frame into a separated file named from that column e.g. to create many files with different names but the names are all like: plantheight.csv, plantweight.csv, and height, weight.., are column names of a data frame) Thank you! Tuc Aug. [[alternative HTML version deleted]]
file <- foo.data filename <- paste("plant", x, sep="") out <- paste("C:/", "plant/", filename, ".csv",sep="") write.csv(file, out) On Tue, Sep 21, 2010 at 5:14 PM, Tucson August <tucsonaugust at gmail.com> wrote:> Dear list, > > How to convert a character to a filename? > such as: > > x <- "height" ?# "height" here is actually a name of a colume in a ?data > frame > filename <- paste("plant,x") > write.csv (data, file="C:/plant/filename.csv) ?# having trouble with this > statement, how to 'write' the filename here? > > All I want from above is to write 'data' to a file named plantheight.csv > > (the ultimate goal is to use a loop to transfer each column of a data frame > into a separated file named from that column > e.g. to create many files with different names but the names are all like: > plantheight.csv, plantweight.csv, and height, weight.., are column names of > a data frame) > > Thank you! > > Tuc Aug. > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Stephen Sefick ____________________________________ | Auburn University? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? | | Department of Biological Sciences? ? ? ? ?? | | 331 Funchess Hall? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | | Auburn, Alabama? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? | | 36849? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | |___________________________________| | sas0025 at auburn.edu? ? ? ? ? ? ? ? ? ? ? ? ? ?? | | http://www.auburn.edu/~sas0025? ? ? ? ? ?? | |___________________________________| Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods.? We are mammals, and have not exhausted the annoying little problems of being mammals. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -K. Mullis "A big computer, a complex algorithm and a long time does not equal science." ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -Robert Gentleman
On Sep 21, 2010, at 6:14 PM, Tucson August wrote:> Dear list, > > How to convert a character to a filename?Filenames are character mode so that shouldn't be a problem.> such as: > > x <- "height" # "height" here is actually a name of a colume in a > data > frameSo you need to learn how to refer to column and that's not it. Try: plant["height"]> filename <- paste("plant,x") # Would produce "plant,x" ... not > usefulwrite.table function takes a dataframe or matrix or something that can be coerce to one of those.> write.csv (data, file="C:/plant/filename.csv ) # having trouble > with this^^ You forgot the closing quotes ... unless that is you expected the "c:/ plant/" and the ".csv" to be automatically prepended and appended and the filename you created to be substituted. That's not going to happen without some greater effort. You can smush together character values like this: paste("c:/path/plant/", filename, ".csv", sep="")> statement, how to 'write' the filename here? > > All I want from above is to write 'data' to a file named > plantheight.csv > > (the ultimate goal is to use a loop to transfer each column of a > data frame > into a separated file named from that columnA column name may not be a first class object.> e.g. to create many files with different names but the names are all > like: > plantheight.csv, plantweight.csv, and height, weight.., are column > names of > a data frame)Did you read the Posting Guide that suggested a reproducible example?> > Thank you! > > Tuc Aug. > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
Nordlund, Dan (DSHS/RDA)
2010-Sep-21 23:12 UTC
[R] How to convert a character into a filename?
> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Tucson August > Sent: Tuesday, September 21, 2010 3:15 PM > To: r-help at r-project.org > Subject: [R] How to convert a character into a filename? > > Dear list, > > How to convert a character to a filename? > such as: > > x <- "height" # "height" here is actually a name of a colume in a > data > frame > filename <- paste("plant,x") > write.csv (data, file="C:/plant/filename.csv) # having trouble with > this > statement, how to 'write' the filename here? > > All I want from above is to write 'data' to a file named > plantheight.csv > > (the ultimate goal is to use a loop to transfer each column of a data > frame > into a separated file named from that column > e.g. to create many files with different names but the names are all > like: > plantheight.csv, plantweight.csv, and height, weight.., are column > names of > a data frame) > > Thank you! > > Tuc Aug. >For illustration purposes I will call your data frame, your.data . You can loop through the column names of your.data, construct an 'out' file name and then write the data. Something like for(i in names(your.data)){ out <- paste("C:/plant/plant", i, ".csv",sep="") write.csv(your.data[i], out, row.names=FALSE) } You didn't tell us whether you wanted row names witten to the file or not, so I decided not. Likewise, I decided to write column names (the default). Hope this is helpful, Dan Daniel J. Nordlund Washington State Department of Social and Health Services Planning, Performance, and Accountability Research and Data Analysis Division Olympia, WA 98504-5204