An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120714/698168a4/attachment.pl>
writing data into files whose names are in a vector
6 messages · Raghuraman Ramachandran, Jeff Newmiller
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120714/31d9b2fc/attachment.pl>
You have to wrap the write calls in some kind of loop. For your example, you could use a for loop. If you have multiple rows of one name you might look into the dlply function from the plyr package and use lapply to do the actual write calls.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
Raghuraman Ramachandran <optionsraghu at gmail.com> wrote:
GuRus
How do I use the write function (or write.table or write.csv) to
achieve
the following please?
age=c(32,37,39)
names=c("john","peter","jake")
I would like create in a directory 3 files each named as
john.csv,peter.csv
and jake.csv and each file have data from the age vector. That is
jon.csv
will contain 32, peter.csv will contain 37 and jake.csv will contain
39.
Thanks for the help.
Raghu
[[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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120714/bc9f9fec/attachment.pl>
you should lapply over a vector of indices (e.g. seq_along(names)) and extract out the subsets of data in your mywrite function before you write them.
Is this homework?
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
Raghuraman Ramachandran <optionsraghu at gmail.com> wrote:
In the same context I tried the following but with no results.
mywrite=function(a,b){
write.csv(a,paste(b,".csv")
}
lapply(age,mywrite(age,names))
which produced:
Error in cat(list(...), file, sep, fill, labels, append) :
argument 1 (type 'list') cannot be handled by 'cat'
Thanks once again.
Raghu
On Sat, Jul 14, 2012 at 3:22 PM, Raghuraman Ramachandran <
optionsraghu at gmail.com> wrote:
GuRus How do I use the write function (or write.table or write.csv) to
achieve
the following please?
age=c(32,37,39)
names=c("john","peter","jake")
I would like create in a directory 3 files each named as
john.csv,peter.csv and jake.csv and each file have data from the age
vector. That is jon.csv will contain 32, peter.csv will contain 37
and
jake.csv will contain 39. Thanks for the help. Raghu
[[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.
I/O is not greatly improved by vectorization, except that whole files being read or written as units is faster than looping over rows.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
Raghuraman Ramachandran <optionsraghu at gmail.com> wrote:
Thx Jeff. I could use a loop no doubt but I thought that will reduce the speed and I was thinking there could be a vectorisation idea. I will try your way. Cheers. On Sat, Jul 14, 2012 at 3:51 PM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us>wrote:
You have to wrap the write calls in some kind of loop. For your
example,
you could use a for loop. If you have multiple rows of one name you
might
look into the dlply function from the plyr package and use lapply to
do the
actual write calls.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go
Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live
Go...
Live: OO#.. Dead: OO#..
Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#.
rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity. Raghuraman Ramachandran <optionsraghu at gmail.com> wrote:
GuRus
How do I use the write function (or write.table or write.csv) to
achieve
the following please?
age=c(32,37,39)
names=c("john","peter","jake")
I would like create in a directory 3 files each named as
john.csv,peter.csv
and jake.csv and each file have data from the age vector. That is
jon.csv
will contain 32, peter.csv will contain 37 and jake.csv will contain
39.
Thanks for the help.
Raghu
[[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.