[R] Application of "merge" and "within"

Santosh santosh2005 at gmail.com
Sat Jun 4 20:44:50 CEST 2016


Thanks so much.. I will try that.. and keep you posted..

on a different note.. using the above examples.. after adding a new column
is blank.. summarizing a null column causes errors.. (like min(NULL) or
min(NA) etc.. to avoid that I was trying the following code..

within(q, as.data.frame(as.matrix(apply($DATA,2,function(x)
{require(gtools);x1 <- ifelse(invalid(x),0,x);return(x1)}))))

For some reason, the above code is not working.. any ideas on converting
NAs/blanks to 0 using apply in within? or any other similar method?

Thanks so much.. I appreciate your help!!

Regards,
Santosh


On Wed, Jun 1, 2016 at 1:45 AM, peter dalgaard <pdalgd at gmail.com> wrote:

> Notice that within-group processing is intended. I'd try
>
> > first <- function(x)x[1]
> > s  <- within(q, {bl <- ave(b, paste(G,a), FUN=first); db <- b - bl})
>
> Or perhaps
>
> q <- within(q, Ga <- paste(G,a))
> tbl <- with(q, tapply(b, Ga, first))
> s <- within(q, {bl <- tbl[Ga]; db <- b - bl})
>
> -pd
>
>
> On 28 May 2016, at 22:53 , Duncan Murdoch <murdoch.duncan at gmail.com>
> wrote:
>
> > On 27/05/2016 7:00 PM, Santosh wrote:
> >> Dear Rxperts!
> >>
> >> Is there a way to compute relative values.. using within().. function?
> >>
> >> Any assistance/suggestions are highly welcome!!
> >> Thanks again,
> >> Santosh...
> >> ___________________________________________________________________
> >> A sample dataset and the computation "outside" within()  function is
> shown..
> >>
> >> q <- data.frame(GL = rep(paste("G",1:3,sep = ""),each = 50),
> >>                G  = rep(1:3,each = 50),
> >>                D = rep(paste("D",1:5,sep = ""),each = 30),
> >>                a = rep(1:15,each = 10),
> >>                t = rep(seq(10),15),
> >>                b = round(runif(150,10,20)))
> >> r <- subset(q,!duplicated(paste(G,a)),sel=c(G,a,b))
> >> names(r)[3] <- "bl"
> >> s <- merge(q,r)
> >> s$db <- s$b-s$bl
> >>
> >>> head(s,5)
> >>    G  a GL  D  t  b bl db
> >> 1   1  1 G1 D1  1 13 13  0
> >> 2   1  1 G1 D1  2 16 13  3
> >> 3   1  1 G1 D1  3 19 13  6
> >> 4   1  1 G1 D1  4 12 13 -1
> >> 5   1  1 G1 D1  5 19 13  6
> >
> > Just use
> >
> > s <- within(s, db <- b - bl)
> >
> > Duncan Murdoch
> >
> > ______________________________________________
> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Office: A 4.23
> Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com
>
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list