Hi, folks, Please first look at the codes: plan_a=c('apple','orange','apple','apple','pear','bread') plan_b=c('bread','bread','orange','bread','bread','yogurt') value=1:6 data=data.frame(plan_a,plan_b,value) library(plyr) library(reshape) mm=melt(data, id=c('plan_a','plan_b')) sum_plan_a=cast(mm,plan_a~variable,sum) ### I would like to add a new column to the data.frame named 'data', with the same sum of value for the same type of plan_a ### The result should come up like this: plan_a plan_b value sum_plan_a 1 apple bread 1 8 2 orange bread 2 2 3 apple orange 3 8 4 apple bread 4 8 5 pear bread 5 5 6 bread yogurt 6 6 Any tips? Thank you. [[alternative HTML version deleted]]
Joris Meys
2010-Jun-26 00:43 UTC
[R] All a column to a data frame with a specific condition
merge(sum_plan_a,data,by="plan_a") Cheers Joris On Sat, Jun 26, 2010 at 2:27 AM, Yi <liuyi.feier at gmail.com> wrote:> Hi, folks, > > Please first look at the codes: > > plan_a=c('apple','orange','apple','apple','pear','bread') > plan_b=c('bread','bread','orange','bread','bread','yogurt') > value=1:6 > data=data.frame(plan_a,plan_b,value) > library(plyr) > library(reshape) > mm=melt(data, id=c('plan_a','plan_b')) > sum_plan_a=cast(mm,plan_a~variable,sum) > > ### I would like to add a new column to the data.frame named 'data', ?with > the same sum of value for the same type of plan_a > ### The result should come up like this: > > ? plan_a ?plan_b ?value ?sum_plan_a > 1 ?apple ?bread ? ? ?1 ? ? ? ?8 > 2 orange ?bread ? ? 2 ? ? ? ?2 > 3 ?apple orange ? ? 3 ? ? ? ?8 > 4 ?apple ?bread ? ? ?4 ? ? ? ?8 > 5 ? pear ?bread ? ? ?5 ? ? ? ? 5 > 6 ?bread yogurt ? ? 6 ? ? ? ? 6 > > Any tips? > > Thank you. > > ? ? ? ?[[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. >-- Joris Meys Statistical consultant Ghent University Faculty of Bioscience Engineering Department of Applied mathematics, biometrics and process control tel : +32 9 264 59 87 Joris.Meys at Ugent.be ------------------------------- Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php
Possibly Parallel Threads
- initial value in 'vmmin' is not finite
- initial gradient and vmmin not finite
- count data with a specific range
- How to predict the mean and variance of the dependent variable after regression
- Help on calculating spearman rank correlation for a data frame with conditions