I thought it's just deleting and reassigning those specific columns. I guess I don't know R as much as I think I do. :(
On Sat, Apr 29, 2017 at 4:06 AM, Göran Broström <goran.brost...@umu.se> wrote: > > > On 2017-04-29 06:45, Mike C wrote: > >> Thanks Rolf. I was just a bit frustrated that R wouldn't generate >> dummy variable names on the fly. >> >> Also, another question, if I want to put column 5 at column 3, >> >> dat[, 3:5] <- dat[, c(5,3,4)] >> >> It does not work, why? >> > > It "works", but you need to shuffle the names in the same way: > > names(dat)[3:5] <- names(dat)[c(5,3,4)] > > Better(?): > > perm <- c(1,2,5,3,4) > dat <- dat[perm] > > dat is a list. > > Göran > > >> ________________________________ From: Rolf Turner >> <r.tur...@auckland.ac.nz> Sent: Friday, April 28, 2017 10:48:42 PM >> To: C W Cc: r-help Subject: Re: [FORGED] Re: [R] 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 >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ R-help@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. >> >> > ______________________________________________ > R-help@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/posti > ng-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] ______________________________________________ R-help@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.