[R] How to convert a character into a filename?

David Winsemius dwinsemius at comcast.net
Wed Sep 22 00:39:45 CEST 2010


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
> frame

So 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  
> useful

write.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 column

A 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



More information about the R-help mailing list