Re: [R] Break up a data frame

From: David Winsemius <dwinsemius_at_comcast.net>
Date: Thu, 20 Mar 2008 14:01:37 +0000 (UTC)

"Ravi S. Shankar" <ravis_at_ambaresearch.com> wrote in news:A36876D3F8A5734FA84A4338135E7CC3033BE60E_at_BAN-MAILSRV03.Amba.com:

> Hi R users,
>
>
>
> I have a dataframe in the below format
>
> xyz 01/03/2007 15.25 USD
>
> xyz 01/04/2007 15.32 USD
>
> xyz 01/02/2008 23.22 USD
>
> abc 01/03/2007 45.2 EUR
>
> abc 01/04/2007 45.00 EUR
>
> abc 01/02/2008 68.33 EUR
>
>
>
> I want to change the above data into the below format
>
>
>
>
>
> xyz 01/03/2007 15.25 USD
> abc
> 01/03/2007 45.2 EUR
>
> xyz 01/04/2007 15.32 USD
> abc
> 01/04/2007 45.00 EUR
>
> xyz 01/02/2008 23.22 USD
> abc
> 01/02/2008 68.33 EUR
>
>

Seeing what appeared to be wordwrap, I interpreted your request as asking for display of "xyz" rows adjacent to "abc" rows. If that is the case, then this seems to work for the toy example:

> xz <- read.table("clipboard")
> xz

   V1 V2 V3 V4

1 xyz 01/03/2007 15.25 USD
2 xyz 01/04/2007 15.32 USD
3 xyz 01/02/2008 23.22 USD
4 abc 01/03/2007 45.20 EUR
5 abc 01/04/2007 45.00 EUR
6 abc 01/02/2008 68.33 EUR


> cbind(xz[xz$V1=="xyz",],xz[xz$V1=="abc",])
V1 V2 V3 V4 V1 V2 V3 V4 1 xyz 01/03/2007 15.25 USD abc 01/03/2007 45.20 EUR
2 xyz 01/04/2007 15.32 USD abc 01/04/2007 45.00 EUR 3 xyz 01/02/2008 23.22 USD abc 01/02/2008 68.33 EUR

If it was instead a request for USD next to EUR, then the needed modifications should be obvious.

-- 
David Winsemius

______________________________________________
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.
Received on Thu 20 Mar 2008 - 14:03:42 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 20 Mar 2008 - 14:30:24 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive