Reputation: 949
Columns A - F are identity columns - (1,0). Column G has the values "WLB0", "WLB2": "WLB10" and "WLB46", "WLB89".
I am trying to do the following for every permutation of A-F with Column G
I am looking for a function to call instead of doing it using this very awkward code that I wrote.
the test data is available for download at the bottom.
X1 <- {dd <- subset(TEST, TEST$A == 1 & TEST$G =="WLB10"); de <-transform(dd, RP = sum(dd$I)/sum(dd$H));mean(de$RP)}
X2 <- {dd <- subset(TEST, TEST$A == 1 & TEST$G =="WLB8"); de <-transform(dd, RP = sum(dd$I)/sum(dd$H));mean(de$RP)}
X3 <- {dd <- subset(TEST, TEST$B == 1 & TEST$G =="WLB10"); de <-transform(dd, RP = sum(dd$I)/sum(dd$H));mean(de$RP)}
TEST1$finalnumber <-ifelse(TEST1$A == 1 & TEST1$G == "WLB10", X1,
ifelse(TEST1$A == 1 & TEST1$G == "WLB8", X2,
ifelse(TEST1$B == 1 & TEST1$G == "WLB10", X3, 0)))
Test data
"https://s3.amazonaws.com/RProgramming/TEST.csv"
"https://s3.amazonaws.com/RProgramming/TEST1.csv"
Upvotes: 0
Views: 70
Reputation: 2414
I'm a bit confused about the purpose of setting RP to be constant across the rows of de
, but the below bit of code will get you some way along, I hope. ddply
and melt
are two great functions for this sort of thing
library(plyr)
library(reshape)
long <- melt(TEST, measure.vars=LETTERS[1:6])
#long <- subset(variable==1)
shorter <- ddply(long, .(G, variable, value), summarize, RP=sum(I)/sum(H))
You can uncomment the line to just get subtotals corresponding to 1, but I thought it was illustrative to show you how it works.
You can then do a similar melt on TEST1, and carry out a lookup for the relevant value:
long <- melt(TEST1, measure.vars=LETTERS[1:6])
ind <- match(paste0(long$G, long$variable), paste0(shorter$G, shorter$variable))
long$final <- shorter$RP[ind]
Upvotes: 1