[R] weight cases?

Gabor Grothendieck ggrothendieck at gmail.com
Sat Oct 14 15:00:48 CEST 2006


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.
>



More information about the R-help mailing list