Hi, I am new at R and have been looking for a guide on how to do this with no avail. I have a data set similar to this: X Y 1 2 3 3 4 9 2 4 6 3 9 1 1 7 0 2 5 6 3 8 and I need it to be split into something like this: X Y 1 2 3 3 4 9 2 4 6 3 X Y 9 1 1 7 0 2 5 6 3 8 I am thinking I need to write a loop because my data set is much larger than this (1000) and I need it all organized into smaller data sets of 5's. Can someone point me in the write direction? Thanks, Andy -- View this message in context: http://r.789695.n4.nabble.com/Splitting-a-data-list-into-smaller-data-lists-tp4389142p4389142.html Sent from the R help mailing list archive at Nabble.com.
Splitting a data list into smaller data lists
3 messages · lawonga, David Winsemius, Jeff Newmiller
On Feb 14, 2012, at 8:02 PM, lawonga wrote:
Hi, I am new at R and have been looking for a guide on how to do this with no avail. I have a data set similar to this: X Y 1 2 3 3 4 9 2 4 6 3 9 1 1 7 0 2 5 6 3 8
If by dataset you mean a daaframe named (for discussion purposes) "dfrm", then this will give you the first five row dfrm[1;5, ] And this will give you the rest: dfrm[ -(1:5), ]
and I need it to be split into something like this: X Y 1 2 3 3 4 9 2 4 6 3 X Y 9 1 1 7 0 2 5 6 3 8 I am thinking I need to write a loop because my data set is much larger than this (1000) and I need it all organized into smaller data sets of 5's. Can someone point me in the write direction?
Oh .... So you want to split into packets of 5 rows each? try: split( dfrm, 1:NROW(dfrm) %/% 5 )
David Winsemius, MD West Hartford, CT
Comments below.
On Tue, 14 Feb 2012, lawonga wrote:
Hi, I am new at R and have been looking for a guide on how to do this with no avail. I have a data set similar to this: X Y 1 2 3 3 4 9 2 4 6 3 9 1 1 7 0 2 5 6 3 8 and I need it to be split into something like this: X Y 1 2 3 3 4 9 2 4 6 3 X Y 9 1 1 7 0 2 5 6 3 8 I am thinking I need to write a loop because my data set is much larger than this (1000) and I need it all organized into smaller data sets of 5's. Can someone point me in the write direction?
"Right" direction?
Type
?split
and
?":"
and
?"%/%"
and
?nrow
at the R command line
Read the "Introduction to R" (I2R) focusing on vector indexing. Whenever
possible you should prefer to use vector indexing instead of a for loop
(there are certainly reasons to use for loops, but in most cases where
they might be appropriate there are iterator functions like lapply that
lead to more succinct and possibly faster code).
To use the resulting list, you will probably want to read
?lapply
and/or
?"[["
although the discussion of lists in I2R may be more digestible.
Depending on how you want to process data in the bigger picture, you may
find the plyr package helpful.
---------------------------------------------------------------------------
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