[R] weight cases?

Gabor Grothendieck ggrothendieck at gmail.com
Sat Oct 14 16:07:58 CEST 2006


I missed your second question.  See ?cov.wt

On 10/14/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> Try this (and round the result to make to it comparable to your calculation):
>
> xtabs(weight ~ var1 + var2, my.data)
>
> On 10/14/06, Adrian Dusa <dusa.adrian at gmail.com> wrote:
> > Thanks for this Gabor,
> >
> > Sometimes weights can take various values, like 0.9
> > > rep(letters[1:3], c(1, 0.9, 1.6))
> > [1] "a" "c"
> >
> > What if the weight variable would be:
> >
> > my.data$weight <- c(0.4, 2, 1.3, 0.9, 1)
> >
> > The way I found the solution was to compute the unweighted table, then find
> > the weight for each unique combination and multiply that with the
> > corresponding row-column entry in the table. The solution though is not very
> > satisfactory:
> >
> > my.data$var1 <- as.factor(my.data$var1)
> > my.data$var2 <- as.factor(my.data$var2)
> > total <- expand.grid(levels(my.data$var1), levels(my.data$var2))
> > rowsmy.data <- apply(unique(my.data[,1:2]), 1, paste, collapse="")
> > rowstotal <- apply(total, 1, paste, collapse="")
> > total$weight <- 0
> > total$weight[sapply(rowsmy.data, function(x) which(rowstotal == x))] <-
> > unique(my.data)[,3]
> >
> > (unweighted <- table(my.data$var1, my.data$var2))
> > round(unweighted*total$weight, 0)
> >
> >
> > Yet another question: how would the weight variable be applied to correlate
> > two numerical variables?
> >
> > Best,
> > Adrian
> >
> > On Saturday 14 October 2006 16:00, Gabor Grothendieck wrote:
> > > Try this:
> > >
> > >     table(lapply(my.data, rep, my.data$weight)[1:2])
> > >
> > > On 10/14/06, Adrian Dusa <dusa.adrian at gmail.com> wrote:
> > > > Dear all,
> > > >
> > > > This is probably a stupid question for which I have a solution, which
> > > > unfortunately is not as straighforward as I'd like. I wonder if there's a
> > > > simple way to apply a weighting variable for the cases of a dataframe
> > > > (well I'm sure there is, I just cannot find it).
> > > >
> > > > My toy example:
> > > > > my.data <- data.frame(var1=c("c", "e", "a", "d", "b"),
> > > >
> > > >                        var2=c("E", "B", "A", "C", "D"),
> > > >                        weight=c(1, 2, 1, 1, 1))
> > > >
> > > > > table(my.data$var1, my.data$var2)
> > > >
> > > >    A B C D E
> > > >  a 1 0 0 0 0
> > > >  b 0 0 0 1 0
> > > >  c 0 0 0 0 1
> > > >  d 0 0 1 0 0
> > > >  e 0 1 0 0 0
> > > >
> > > > Applying the weight variable, the table should yield a value of 2 for the
> > > > "eB"
> > > >
> > > > combination:
> > > > > table(my.data$var1, my.data$var2)
> > > >
> > > >    A B C D E
> > > >  a 1 0 0 0 0
> > > >  b 0 0 0 1 0
> > > >  c 0 0 0 0 1
> > > >  d 0 0 1 0 0
> > > >  e 0 2 0 0 0
> > > >
> > > >
> > > > Thanks in advance,
> > > > Adrian
> > > >
> > > > --
> > > > Adrian Dusa
> > > > Romanian Social Data Archive
> > > > 1, Schitu Magureanu Bd
> > > > 050025 Bucharest sector 5
> > > > Romania
> > > > Tel./Fax: +40 21 3126618 \
> > > >          +40 21 3120210 / int.101
> > > >
> > > > ______________________________________________
> > > > R-help at stat.math.ethz.ch 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.
> >
> > --
> > Adrian Dusa
> > Romanian Social Data Archive
> > 1, Schitu Magureanu Bd
> > 050025 Bucharest sector 5
> > Romania
> > Tel./Fax: +40 21 3126618 \
> >          +40 21 3120210 / int.101
> >
>



More information about the R-help mailing list