[R] looking for a cleaner way to do something

Gabor Grothendieck ggrothendieck at gmail.com
Tue Oct 17 22:02:59 CEST 2006


Try this:

X <- structure(11:15, .Names = letters[1:5])
Y <- structure(21:25, .Names = letters[1:5])

rbind(group1 = X, group2 = Y)
tab <- rbind(group1 = X, group2 = Y)
tab[,-1] / tab[,1]

On 10/17/06, Leeds, Mark (IED) <Mark.Leeds at morganstanley.com> wrote:
> I have two numeric vectors each of length 17 and each is named the exact
> same way.
>
> so
>                obsnum ppppp  ppppm pppmp . dot dot dot......
> temp1    is    1417    52         63       85
>
>                 obsnum ppppp  ppppm pppmp . dot dot dot......
> temp2     is   1213     41        50       97
>
> what i want to have is a resultant matrix with 2 rows and 16 columns
> where the 16 columns are the 2:17 columns divided by the respective
> first element in each vector.
> ( so 52, 63 and 85 should all get divided by 1417  and 41, 50 and 97
> should all be divided by 1213 ).
>
> it doesn't have to retain the column names because i can just assign
> them again when i am assigning the rownames.
>
> below is my  code :
>
> resultmatrix<-rbind(temp1,temp2)
> resultmatrix<-resultmatrix[,2:17]/resultmatrix[,1]
>
> colnames(resultmatrix)<-colnames(temp1)
> rownames(resultmatrix)<-c("group1","group2")
>
> I'm pretty sure above will work but it seemed kind of ugly and I
> wondering if there is a better way because i am trying to improve in R.
> if there isn't, that's fine. thanks.
> --------------------------------------------------------
>
> This is not an offer (or solicitation of an offer) to buy/sell the securities/instruments mentioned or an official confirmation.  Morgan Stanley may deal as principal in or own or act as market maker for securities/instruments mentioned or may advise the issuers.  This is not research and is not from MS Research but it may refer to a research analyst/research report.  Unless indicated, these views are the author's and may differ from those of Morgan Stanley research or others in the Firm.  We do not represent this is accurate or complete and we may not update this.  Past performance is not indicative of future returns.  For additional information, research reports and important disclosures, contact me or see https://secure.ms.com/servlet/cls.  You should not use e-mail to request, authorize or effect the purchase or sale of any security or instrument, to send transfer instructions, or to effect any other transactions.  We cannot guarantee that any such requests received via !
>  e-mail will be processed in a timely manner.  This communication is solely for the addressee(s) and may contain confidential information.  We do not waive confidentiality by mistransmission.  Contact me if you do not wish to receive these communications.  In the UK, this communication is directed in the UK to those persons who are market counterparties or intermediate customers (as defined in the UK Financial Services Authority's rules).
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>



More information about the R-help mailing list