C W
2017-Apr-29 00:57 UTC
[R] How create columns for squared values from previous columns?
Dear R list, I am am a little unsure what is the best way to approach this. I suppose I have> dat <- matrix(rnorm(30), ncol = 5) > dat <- data.frame(dat) > datX1 X2 X3 X4 X5 1 -1.13999917 -0.87868106 -0.33000492 1.5241765 -0.92483388 2 -0.56168006 -0.08837883 1.96237792 -0.5335615 0.02880586 3 0.82800071 -1.89965562 -0.05438815 -0.9162857 -0.57470053 4 -0.03218412 -0.23119263 -1.10671765 -0.2885518 -0.30953951 5 1.70525779 -0.93854817 -1.05932636 -0.2983139 -0.21980145 6 1.19047531 0.38301678 -0.20830015 -0.6668266 0.82578534 Suppose I want to add columns X6, X7, X8, where X6 = X1^2 X7 = X2^2 X8 = X3^2 I am thinking of using apply(), but df asks for column names, what's a quick way to generate names on the fly? Thank you very much! [[alternative HTML version deleted]]
C W
2017-Apr-29 01:21 UTC
[R] How create columns for squared values from previous columns?
I came up with this solution,> cbind(dat, dat[, 1:3]^2)X1 X2 X3 X4 X5 X1 X2 X3 1 0.72776481 -1.1332612 -1.9857503 0.46189400 -0.09016379 0.529641625 1.28428102 3.9432044 2 0.05126592 0.2858707 0.9075806 1.27582713 -0.49438507 0.002628194 0.08172203 0.8237026 3 -0.40430146 0.5457195 -1.1924042 0.15025594 1.99710475 0.163459669 0.29780978 1.4218277 4 1.40746971 -1.2279416 0.3296075 0.84411774 -0.52371619 1.980970990 1.50784058 0.1086411 5 -0.53841150 0.4750082 -0.4705148 0.05591914 -0.31503500 0.289886944 0.22563275 0.2213842 6 0.90691210 0.7247171 0.8244184 0.73328097 -1.05284737 0.822489552 0.52521494 0.6796657 But, you would NOT ONLY get undesired variable names, BUT ALSO duplicated names. I suppose I can use paste() to solve that? Any better ideas? On Fri, Apr 28, 2017 at 8:57 PM, C W <tmrsg11 at gmail.com> wrote:> Dear R list, > > I am am a little unsure what is the best way to approach this. I suppose I > have > > > dat <- matrix(rnorm(30), ncol = 5) > > dat <- data.frame(dat) > > dat > X1 X2 X3 X4 X5 > 1 -1.13999917 -0.87868106 -0.33000492 1.5241765 -0.92483388 > 2 -0.56168006 -0.08837883 1.96237792 -0.5335615 0.02880586 > 3 0.82800071 -1.89965562 -0.05438815 -0.9162857 -0.57470053 > 4 -0.03218412 -0.23119263 -1.10671765 -0.2885518 -0.30953951 > 5 1.70525779 -0.93854817 -1.05932636 -0.2983139 -0.21980145 > 6 1.19047531 0.38301678 -0.20830015 -0.6668266 0.82578534 > > Suppose I want to add columns X6, X7, X8, where > X6 = X1^2 > X7 = X2^2 > X8 = X3^2 > > I am thinking of using apply(), but df asks for column names, what's a > quick way to generate names on the fly? > > Thank you very much! > >[[alternative HTML version deleted]]
Rolf Turner
2017-Apr-29 02:48 UTC
[R] [FORGED] Re: How create columns for squared values from previous columns?
On 29/04/17 13:21, C W wrote:> I came up with this solution, > >> cbind(dat, dat[, 1:3]^2) > X1 X2 X3 X4 X5 X1 > X2 X3 > 1 0.72776481 -1.1332612 -1.9857503 0.46189400 -0.09016379 0.529641625 > 1.28428102 3.9432044 > 2 0.05126592 0.2858707 0.9075806 1.27582713 -0.49438507 0.002628194 > 0.08172203 0.8237026 > 3 -0.40430146 0.5457195 -1.1924042 0.15025594 1.99710475 0.163459669 > 0.29780978 1.4218277 > 4 1.40746971 -1.2279416 0.3296075 0.84411774 -0.52371619 1.980970990 > 1.50784058 0.1086411 > 5 -0.53841150 0.4750082 -0.4705148 0.05591914 -0.31503500 0.289886944 > 0.22563275 0.2213842 > 6 0.90691210 0.7247171 0.8244184 0.73328097 -1.05284737 0.822489552 > 0.52521494 0.6796657 > > But, you would NOT ONLY get undesired variable names, BUT ALSO duplicated > names. I suppose I can use paste() to solve that? > > Any better ideas?Well, if the names bizzo is your only worry, you could hit the result with data.frame() *after* cbinding on the squared terms: dat <- matrix(rnorm(30),ncol=5) dat <- cbind(dat,dat[,1:3]^2) dat <- data.frame(dat) names(dat) And as you indicate, the names of a data frame are easily adjusted. I wouldn't lose sleep over it. cheers, Rolf Turner P.S. You could also do names(dat) <- make.unique(names(dat)) to your original idea, to get rid of the lack of uniqueness. The result is probably "undesirable" but. R. T. -- Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276
PIKAL Petr
2017-May-02 11:56 UTC
[R] How create columns for squared values from previous columns?
Hi you can use data.frame data.frame(dat, dat[,1:3]^2) and you can set names afterwards by names function. Cheers Petr> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of C W > Sent: Saturday, April 29, 2017 3:22 AM > To: r-help <r-help at r-project.org> > Subject: Re: [R] How create columns for squared values from previous > columns? > > I came up with this solution, > > > cbind(dat, dat[, 1:3]^2) > X1 X2 X3 X4 X5 X1 > X2 X3 > 1 0.72776481 -1.1332612 -1.9857503 0.46189400 -0.09016379 0.529641625 > 1.28428102 3.9432044 > 2 0.05126592 0.2858707 0.9075806 1.27582713 -0.49438507 0.002628194 > 0.08172203 0.8237026 > 3 -0.40430146 0.5457195 -1.1924042 0.15025594 1.99710475 0.163459669 > 0.29780978 1.4218277 > 4 1.40746971 -1.2279416 0.3296075 0.84411774 -0.52371619 1.980970990 > 1.50784058 0.1086411 > 5 -0.53841150 0.4750082 -0.4705148 0.05591914 -0.31503500 0.289886944 > 0.22563275 0.2213842 > 6 0.90691210 0.7247171 0.8244184 0.73328097 -1.05284737 0.822489552 > 0.52521494 0.6796657 > > But, you would NOT ONLY get undesired variable names, BUT ALSO > duplicated names. I suppose I can use paste() to solve that? > > Any better ideas? > > > On Fri, Apr 28, 2017 at 8:57 PM, C W <tmrsg11 at gmail.com> wrote: > > > Dear R list, > > > > I am am a little unsure what is the best way to approach this. I > > suppose I have > > > > > dat <- matrix(rnorm(30), ncol = 5) > > > dat <- data.frame(dat) > > > dat > > X1 X2 X3 X4 X5 > > 1 -1.13999917 -0.87868106 -0.33000492 1.5241765 -0.92483388 > > 2 -0.56168006 -0.08837883 1.96237792 -0.5335615 0.02880586 > > 3 0.82800071 -1.89965562 -0.05438815 -0.9162857 -0.57470053 > > 4 -0.03218412 -0.23119263 -1.10671765 -0.2885518 -0.30953951 > > 5 1.70525779 -0.93854817 -1.05932636 -0.2983139 -0.21980145 > > 6 1.19047531 0.38301678 -0.20830015 -0.6668266 0.82578534 > > > > Suppose I want to add columns X6, X7, X8, where > > X6 = X1^2 > > X7 = X2^2 > > X8 = X3^2 > > > > I am thinking of using apply(), but df asks for column names, what's a > > quick way to generate names on the fly? > > > > Thank you very much! > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.________________________________ Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou ur?eny pouze jeho adres?t?m. Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? neprodlen? jeho odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie vyma?te ze sv?ho syst?mu. Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat. Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi ?i zpo?d?n?m p?enosu e-mailu. V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?: - vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? smlouvy, a to z jak?hokoliv d?vodu i bez uveden? d?vodu. - a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout; Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany p??jemce s dodatkem ?i odchylkou. - trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve v?slovn?m dosa?en?m shody na v?ech jej?ch n?le?itostech. - odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za spole?nost ??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n nebo p?semn? pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto emailu p??padn? osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich existence je adres?tovi ?i osob? j?m zastoupen? zn?m?. This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email. In case that this e-mail forms part of business dealings: - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning. - if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation. - the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects. - the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.