Dear list, I have a big data frame which looks like this: variable YEAR VAR EC01 2006 100 EC01 2007 200 EC02 2006 500 EC02 2007 450 PROD 2006 567 PROD 2007 543 What I would like to do is to divide each variables by PROD,namely: EC01(2006)/PROD(2006) EC01(2007)/PROD(2007) EC02(2006)/PROD(2006) EC02(2007)/PROD(2007) Anyone knows how to do it?? THANKS!!! [[alternative HTML version deleted]]
you can do this: a <- tapply(VAR, YEAR, prod) The use "merge" to create a new variable of the length of your original VAR, and just do VAR/prod.VAR ----------------Contact Details:------------------------------------------------------- Contact me: Tal.Galili@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) ---------------------------------------------------------------------------------------------- On Mon, Apr 26, 2010 at 6:07 PM, n.vialma@libero.it <n.vialma@libero.it>wrote:> PROD[[alternative HTML version deleted]]
Is this what you want:> x <- read.table(textConnection("variableYEAR VAR + EC01 2006 100 + + EC01 2007 200 + + EC02 2006 500 + + EC02 2007 450 + + PROD 2006 567 + + PROD 2007 543"), header=TRUE, as.is=TRUE)> closeAllConnections() > # split by year and then determine if there is a PROD in the year > result <- do.call(rbind, lapply(split(x, x$YEAR), function(.year){+ prod.indx <- which(.year$variable == "PROD") + if (length(prod.indx) != 1){ + cat('missing or incorrect number of PRODs for:', .year$YEAR[1L], '\n') + return(NULL) + } + prod.value <- .year$VAR[prod.indx] + # remove PROD from result + .year <- .year[-prod.indx,,drop=FALSE] + .year$VAR <- .year$VAR / prod.value + .year # return updated results + }))> resultvariable YEAR VAR 2006.1 EC01 2006 0.1763668 2006.3 EC02 2006 0.8818342 2007.2 EC01 2007 0.3683241 2007.4 EC02 2007 0.8287293>On Mon, Apr 26, 2010 at 11:07 AM, n.vialma@libero.it <n.vialma@libero.it>wrote:> > Dear list, > I have a big data frame which looks like this: > variable YEAR VAR > EC01 2006 100 > > EC01 2007 200 > > EC02 2006 500 > > EC02 2007 450 > > PROD 2006 567 > > PROD 2007 543 > > What I would like to do is to divide each variables by PROD,namely: > EC01(2006)/PROD(2006) > EC01(2007)/PROD(2007) > EC02(2006)/PROD(2006) > EC02(2007)/PROD(2007) > Anyone knows how to do it?? > THANKS!!! > > [[alternative HTML version deleted]] > > ______________________________________________ > 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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]
Try this: sweep(with(x, tapply(VAR, list(variable, YEAR), FUN = prod)), 2, with(x, tapply(VAR, YEAR, FUN = prod)), FUN = "/") On Mon, Apr 26, 2010 at 12:07 PM, n.vialma@libero.it <n.vialma@libero.it>wrote:> > Dear list, > I have a big data frame which looks like this: > variable YEAR VAR > EC01 2006 100 > > EC01 2007 200 > > EC02 2006 500 > > EC02 2007 450 > > PROD 2006 567 > > PROD 2007 543 > > What I would like to do is to divide each variables by PROD,namely: > EC01(2006)/PROD(2006) > EC01(2007)/PROD(2007) > EC02(2006)/PROD(2006) > EC02(2007)/PROD(2007) > Anyone knows how to do it?? > THANKS!!! > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Dear list, I have a big data frame which looks like this: variable YEAR VAR EC01 2006 100 EC01 2007 200 EC02 2006 500 EC02 2007 450 PROD 2006 567 PROD 2007 543 What I would like to do is to divide each variables by PROD,namely: EC01(2006)/PROD(2006) EC01(2007)/PROD(2007) EC02(2006)/PROD(2006) EC02(2007)/PROD(2007) Anyone knows how to do it?? THANKS!!! [[alternative HTML version deleted]]
Here is one way, using the reshape package: library(reshape) Dat <- read.table(textConnection("variable YEAR VAR EC01 2006 100 EC01 2007 200 EC02 2006 500 EC02 2007 450 PROD 2006 567 PROD 2007 543"), header=TRUE) closeAllConnections() c.Dat1 <- as.data.frame(cast(Dat, YEAR ~ variable, value = "VAR")) m.Dat <- melt(c.Dat1, measure.vars = c("EC01", "EC02")) m.Dat$value/m.Dat$PROD -Ista On Tue, Apr 27, 2010 at 8:04 AM, n.vialma at libero.it <n.vialma at libero.it> wrote:> > Dear list, > > I have a big data frame which looks like this: > variable ? ? ? ? ? ? ? ? YEAR ? ? ? ? ? ? ? ? ? ? ? ? ?VAR > EC01 ? ? ? ? ? ? ? ? ? ? ?2006 ? ? ? ? ? ? ? ? ? ? ? ? ? ? 100 > > EC01 ? ? ? ? ? ? ? ? ? ? ?2007 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?200 > > EC02 ? ? ? ? ? ? ? ? ? ? ? 2006 ? ? ? ? ? ? ? ? ? ? ? ? ? ? 500 > > EC02 ? ? ? ? ? ? ? ? ? ? ? 2007 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?450 > > PROD ? ? ? ? ? ? ? ? ? ? ? 2006 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?567 > > PROD ? ? ? ? ? ? ? ? ? ? ? 2007 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?543 > > What I would like to do is to divide each variables by PROD,namely: > EC01(2006)/PROD(2006) > EC01(2007)/PROD(2007) > EC02(2006)/PROD(2006) > EC02(2007)/PROD(2007) > Anyone knows how to do it?? > THANKS!!! > > > > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org