Hi, A fairly basic problem I think, here although searching the inetrnet doesn't seem to reveal a solution. I have a dataset with two columns of real numbers. It is read in via read.table. I simply need to square the data in the second column and then plot it. I have tried extracting the second column (b <- z[2]) and then squaring it and merging it to a variable which is the first column extracted. Firstly thsi seems very convoluted, secondly it gives a seg fault (I'm guessing because memory hasn't been allocated for the second column on the creation of teh first variable). So any ideas about the easy way of doing the matsh so I can then just use plot(z, .....) as before? Cheers in advance, Kenny -- View this message in context: http://www.nabble.com/Squaring-one-column-of-data-tp23997104p23997104.html Sent from the R help mailing list archive at Nabble.com.
Have sorted it, it was simple, for anyone else who needs it: z[2] <- z[2]*z[2] squares the second column of a data table and writes it back to the second column Kenny Kenny Larsen wrote:> > Hi, > > A fairly basic problem I think, here although searching the inetrnet > doesn't seem to reveal a solution. I have a dataset with two columns of > real numbers. It is read in via read.table. I simply need to square the > data in the second column and then plot it. I have tried extracting the > second column (b <- z[2]) and then squaring it and merging it to a > variable which is the first column extracted. Firstly thsi seems very > convoluted, secondly it gives a seg fault (I'm guessing because memory > hasn't been allocated for the second column on the creation of teh first > variable). > > So any ideas about the easy way of doing the matsh so I can then just use > plot(z, .....) as before? > > Cheers in advance, > > Kenny >-- View this message in context: http://www.nabble.com/Squaring-one-column-of-data-tp23997104p23997558.html Sent from the R help mailing list archive at Nabble.com.
Kenny Larsen wrote:> Hi, > > A fairly basic problem I think, here although searching the inetrnet doesn't > seem to reveal a solution. I have a dataset with two columns of real > numbers. It is read in via read.table. I simply need to square the data in > the second column and then plot it. I have tried extracting the second > column (b <- z[2]) and then squaring it and merging it to a variable which > is the first column extracted. Firstly thsi seems very convoluted, secondly > it gives a seg fault (I'm guessing because memory hasn't been allocated for > the second column on the creation of teh first variable). > > So any ideas about the easy way of doing the matsh so I can then just use > plot(z, .....) as before? > > Cheers in advance, > > Kenny >Hi, A few alternatives, d <- data.frame(x = seq(1, 10), y=seq(1, 10)) d2 <- d # copy # variants that make a new column d$z <- d$y^2 d <- within(d, {z1 <- y^2}) # destructive transformation d3 <- transform(d2, y=y^2) ize <- # this may be useful if you want to apply a function to several columns (orig. idea from vQ) function(d, columns=names(d), izer=`^`, ...) { d[columns] = sapply(d[columns], izer, ...) d } d4 <- ize(d2, "y",`^`, 2) HTH, baptiste