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

______________________________________________
R-help@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.

Reply via email to