[R] transform key value pair to column

Tan, Richard RTan at panagora.com
Thu Feb 19 23:53:39 CET 2009


Thank you, works! 

-----Original Message-----
From: Rowe, Brian Lee Yung (Portfolio Analytics) [mailto:B_Rowe at ml.com] 
Sent: Thursday, February 19, 2009 5:52 PM
To: Wacek Kusnierczyk; Tan, Richard
Cc: r-help at r-project.org
Subject: RE: [R] transform key value pair to column

Try this:

> dummy
  id code value
1  1   hi  10.3
2  1   lo   5.2
3  2   hi  19.4
4  3   hi  20.0
5  3   lo  12.0
6  4   lo   5.8

> reshape(dummy, idvar='id', timevar='code', direction='wide')
  id value.hi value.lo
1  1     10.3      5.2
3  2     19.4       NA
4  3     20.0     12.0
6  4       NA      5.8

Brian

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Wacek Kusnierczyk
Sent: Thursday, February 19, 2009 5:39 PM
To: Tan, Richard
Cc: r-help at r-project.org
Subject: Re: [R] transform key value pair to column


see ?stack, for example.

vQ

Tan, Richard wrote:
> Hi, is there a good way (instead of a time-consuming for loop) to 
> transfer a key/value pair dataframe to a dataframe with key as column 
> and value as row?  For example, I have a dataframe with three columns:
> id, code, value:
>  
> id,code,value
> 1,hi,10.3
> 1,lo,5.2
> 2,hi,19.4
> 3,hi,20
> 3,lo,12
> 4,lo,5.8
>  
> I want to get a dataframe like this:
>  
> id,hi,lo
> 1,10.3,5.2
> 2,19.4,NA
> 3,20,12
> 4,NA,5.8
>  
> Thank you,
> Richard
>
> 	[[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.
>

______________________________________________
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.

------------------------------------------------------------------------
--
This message w/attachments (message) may be privileged, confidential or
proprietary, and if you are not an intended recipient, please notify the
sender, do not use or share it and delete it. Unless specifically
indicated, this message is not an offer to sell or a solicitation of any
investment products or other financial product or service, an official
confirmation of any transaction, or an official statement of Merrill
Lynch. Subject to applicable law, Merrill Lynch may monitor, review and
retain e-communications (EC) traveling through its networks/systems. The
laws of the country of each sender/recipient may impact the handling of
EC, and EC may be archived, supervised and produced in countries other
than the country in which you are located. This message cannot be
guaranteed to be secure or error-free. References to "Merrill Lynch" are
references to any company in the Merrill Lynch & Co., Inc. group of
companies, which are wholly-owned by Bank of America Corporation.
Securities and Insurance Products: * Are Not FDIC Insured * Are Not Bank
Guaranteed * May Lose Value * Are Not a Bank Deposit * Are Not a
Condition to Any Banking Service or Activity * Are Not Insured by Any
Federal Government Agency. Attachments that are part of this
E-communication may have additional important disclosures and
disclaimers, which you should read. This message is subject to terms
available at the following link:
http://www.ml.com/e-communications_terms/. By messaging with Merrill
Lynch you consent to the foregoing.
------------------------------------------------------------------------
--



More information about the R-help mailing list