[R] ifthen() question

Matthew Dowle mdowle at mdowle.plus.com
Fri Mar 5 11:33:05 CET 2010


This post breaks the posting guide in multiple ways.  Please read it again 
(and then again) - in particular the first 3 paragraphs.  You will help 
yourself by following it.

The solution is right there in the help page for ?data.frame and other 
places including Introduction to R.  I think its more helpful *not* to tell 
you what it is, so that you discover it for yourself, learn how to learn, 
and google.   I hope that you appreciate it that I've been helpful just 
simply (and quickly) telling you the answer *is* there.

Having said that, you don't appear to be aware of many of the packages 
around that does this task - you appear to be re-inventing the wheel.  I 
suggest you briefly investigate each and every one of the top 30 packages 
ranked by crantastic, before writing any more R code.  A little time 
invested doing that will pay you dividends in the long run. That is not a 
complaint of you though, as that advice is not in the posting guide.

Matthew


"AC Del Re" <delre at wisc.edu> wrote in message 
news:85cf8f8d1003040735k2b076142jc99b7ec34da879af at mail.gmail.com...
> Hi All,
>
> I am using a specialized aggregation function to reduce a dataset with
> multiple rows per id down to 1 row per id. My function work perfect when
> there are >1 id but alters the 'var.g' in undesirable ways when this
> condition is not met, Therefore, I have been trying ifthen() statements to
> keep the original value when length of unique id == 1 but I cannot get it 
> to
> work. e.g.:
>
> #function to aggregate effect sizes:
> aggs <- function(g, n.1, n.2, cor = .50) {
>  n.1 <- mean(n.1)
>  n.2 <- mean(n.2)
>  N_ES <- length(g)
>  corr.mat <- matrix (rep(cor, N_ES^2), nrow=N_ES)
>  diag(corr.mat) <- 1
>  g1g2 <- cbind(g) %*% g
>  PSI <- (8*corr.mat + g1g2*corr.mat^2)/(2*(n.1+n.2))
>  PSI.inv <- solve(PSI)
>  a <- rowSums(PSI.inv)/sum(PSI.inv)
>  var.g <- 1/sum(PSI.inv)
>  g <- sum(g*a)
>  out<-cbind(g,var.g, n.1, n.2)
>  return(out)
>  }
>
>
> # automating this procedure for all rows of df. This format works perfect
> when there is > 1 id per row only:
>
> agg_g <- function(id, g, n.1, n.2, cor = .50) {
>  st <- unique(id)
>  out <- data.frame(id=rep(NA,length(st)))
>  for(i in 1:length(st))   {
>    out$id[i] <- st[i]
>    out$g[i] <- aggs(g=g[id==st[i]], n.1= n.1[id==st[i]],
>                       n.2 = n.2[id==st[i]], cor)[1]
>    out$var.g[i] <- aggs(g=g[id==st[i]], n.1= n.1[id==st[i]],
>                          n.2 = n.2[id==st[i]], cor)[2]
>    out$n.1[i] <- round(mean(n.1[id==st[i]]),0)
>    out$n.2[i] <- round(mean(n.2[id==st[i]]),0)
>  }
>  return(out)
> }
>
>
> # The attempted solution using ifthen() and minor changes to function but
> it's not working properly:
> agg_g <- function(df,var.g, id, g, n.1, n.2, cor = .50) {
>  df$var.g <- var.g
>  st <- unique(id)
>  out <- data.frame(id=rep(NA,length(st)))
>  for(i in 1:length(st))   {
>    out$id[i] <- st[i]
>    out$g[i] <- aggs(g=g[id==st[i]], n.1= n.1[id==st[i]],
>                       n.2 = n.2[id==st[i]], cor)[1]
>    out$var.g[i]<-ifelse(length(st[i])==1, df$var.g[id==st[i]],
>                     aggs(g=g[id==st[i]],
>                          n.1= n.1[id==st[i]],
>                          n.2 = n.2[id==st[i]], cor)[2])
>    out$n.1[i] <- round(mean(n.1[id==st[i]]),0)
>    out$n.2[i] <- round(mean(n.2[id==st[i]]),0)
>  }
>  return(out)
> }
>
> # sample data:
> id<-c(1, rep(1:19))
> n.1<-c(10,20,13,22,28,12,12,36,19,12,36,75,33,121,37,14,40,16,14,20)
> n.2 <- c(11,22,10,20,25,12,12,36,19,11,34,75,33,120,37,14,40,16,10,21)
> g <- c(.68,.56,.23,.64,.49,-.04,1.49,1.33,.58,1.18,-.11,1.27,.26,.40,.49,
> .51,.40,.34,.42,1.16)
> var.g <-
> c(.08,.06,.03,.04,.09,.04,.009,.033,.0058,.018,.011,.027,.026,.0040,
> .049,.0051,.040,.034,.0042,.016)
> df<-data.frame(id, n.1,n.2, g, var.g)
>
> Any help is much appreciated,
>
> AC
>
> [[alternative HTML version deleted]]
>



More information about the R-help mailing list