[R] Creating Enumerated Variables
John Kane
jrkrideau at yahoo.ca
Fri Jul 16 14:41:36 CEST 2010
I think this is simpler but still not all that clean.
===============================================================
xx <- structure(list(ID = 1:9, Age = c(10L, 10L, 10L, 11L, 11L, 11L,
10L, 10L, 11L), School = c(1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L
), Grade = c(98L, 97L, 92L, 90L, 80L, 70L, 80L, 79L, 70L)), .Names = c("ID",
"Age", "School", "Grade"), class = "data.frame", row.names = c(NA,
-9L))
library(reshape)
(rr <- cast(xx, Age + School ~ ., length))
mylist <- NULL
for(i in 1:length(rr[,3])) {
mylist[[i]] <- seq(rr[i,3])
}
ecount <- unlist(mylist)
cbind(xx[order(xx[,3]),], ecount)
================================================================
--- On Thu, 7/15/10, Dennis Murphy <djmuser at gmail.com> wrote:
> From: Dennis Murphy <djmuser at gmail.com>
> Subject: Re: [R] Creating Enumerated Variables
> To: "jdellava" <jdellava at vcuHI.edu>
> Cc: r-help at r-project.org
> Received: Thursday, July 15, 2010, 5:08 PM
> Hi:
>
> I sincerely hope there's an easier way, but one method to
> get this is as
> follows,
> with d as the data frame name of your test data:
>
> d <- d[order(with(d, Age, School, rev(Grade))), ]
> d$Count <- do.call(c, mapply(seq, 1, as.vector(t(with(d,
> table(Age,
> School))))))
> d
>
> > d
> ID Age School Grade Count
> 1 1 10 1
> 98 1
> 3 3 10 1
> 92 2
> 7 7 10 1
> 80 3
> 8 8 10 1
> 79 4
> 2 2 10 2
> 97 1
> 4 4 11 1
> 90 1
> 5 5 11 1
> 80 2
> 6 6 11 2
> 70 1
> 9 9 11 2
> 70 2
>
>
> The code to get the count is a little convoluted:
> - first, find the frequency table of Age and
> School, transpose it and
> then unlist into a vector
> - use mapply to generate a sequence for each
> group from 1 up to its
> group count; mapply() is necessary to use the counts
> as a vector argument.
> This returns a list of sequences.
> - do.call() applies a function (here, c) to
> an input list, yielding the
> vector of groupwise indices we wanted. Basically, it
> flattens the list. This
> is what we assign to d$Count.
>
> Side note: I didn't get the correct ordering the first
> time, but I did the
> second time (2.11.1 64bit, Windows 7).
>
> HTH,
> Dennis
>
> On Thu, Jul 15, 2010 at 7:45 AM, jdellava <jdellava at vcu.edu>
> wrote:
>
> >
> > Hi,
> >
> > I am trying to create a variable counting the number
> of individuals based
> > on
> > two variables. I am able to do it or one variable, but
> not two. In SAS I
> > was
> > able to sort by two variables and use a first.
> statement to create the
> > counts based on both. Here is an example:
> >
> > What I have
> > ID Age
> School
> Grade
> > 1 10
> 1
>
> 98
> > 2 10
> 2
>
> 97
> > 3 10
> 1
>
> 92
> > 4 11
> 1
>
> 90
> > 5 11
> 1
>
> 80
> > 6 11
> 2
>
> 70
> > 7 10
> 1
>
> 80
> > 8 10
> 1
>
> 79
> > 9 11
> 2
>
> 70
> >
> > What I need
> > ID Age
> School
> Grade School Count
> > 1 10
> 1
>
> 98
> 1
> > 3 10
> 1
>
> 92
> 2
> > 7 10
> 1
>
> 80
> 3
> > 8 10
> 1
>
> 79
> 4
> > 2 10
> 2
>
> 97
> 1
> > 4 11
> 1
>
> 90
> 1
> > 5 11
> 1
>
> 80
> 2
> > 6 11
> 2
>
> 70
> 1
> > 9 11
> 2
>
> 70
> 2
> >
> > I want to create counts of individuals age 10 in
> school 1 then age 10 in
> > school two (the what I need set)
> >
> > Anyway to do this?
> >
> > Thank you.
> >
> > --
> > View this message in context:
> > http://r.789695.n4.nabble.com/Creating-Enumerated-Variables-tp2290262p2290262.html
> > Sent from the R help mailing list archive at
> Nabble.com.
> >
> > ______________________________________________
> > R-help at r-project.org
> 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.
> >
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org
> 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