Skip to content

grouping function

4 messages · Geoffrey Smith, Sarah Goslee, arun

#
Hi,
On Tue, May 8, 2012 at 2:17 PM, Geoffrey Smith <gps at asu.edu> wrote:
Thanks for providing reproducible data. Two minor points: you don't
need ; at the end of lines, and calling your data frame df is
confusing because there's a df() function.
This isn't a clear specification:
1990, 1994 for instance fits into all three groups. Do you want to
extend this to more start years, or are you only interested in those
three? Assuming end is always >= start, you don't even need to
consider the end years in your grouping.

Here are two methods, one that "looks like" your pseudocode, and one
that is more R-ish. They give different results because of different
handling of cases that fit all three groups. Rearranging the
statements in makegroup1() from broadest to most restrictive would
make it give the same result as makegroup2().


makegroup1 <- function(x,y) {
 group <- numeric(length(x))
 group[x <= 1990 & y > 1990] <- 1
 group[x <= 1991 & y > 1991] <- 2
 group[x <= 1992 & y > 1992] <- 3
 group
}

makegroup2 <- function(x, y) {
   ifelse(x <= 1990 & y > 1990, 1,
      ifelse(x <= 1991 & y > 1991, 2,
   	   ifelse(x <= 1992 & y > 1992, 3, 0)))
}
[1] 3 3 3 0 0 3 3 0 0 0 3 0 0 0 0
[1]  1  2  3 NA NA  2  3 NA NA NA  3 NA NA NA NA
But really, it's a better idea to develop an unambiguous statement of
your desired output.

Sarah
#
HI Sarah,

I run the same code from your reply email.? For the makegroup2, the results are 0 in places of NA.
+ group <- numeric(length(x))
+ group[x <= 1990 & y > 1990] <- 1
+ group[x <= 1991 & y > 1991] <- 2
+ group[x <= 1992 & y > 1992] <- 3
+ group
+ }
+?? ifelse(x <= 1990 & y > 1990, 1,
+?????? ifelse(x <= 1991 & y > 1991, 2,
+???????? ifelse(x <= 1992 & y > 1992, 3, 0)))
+ }
?[1] 3 3 3 0 0 3 3 0 0 0 3 0 0 0 0
?[1] 1 2 3 0 0 2 3 0 0 0 3 0 0 0 0


A. K.




----- Original Message -----
From: Sarah Goslee <sarah.goslee at gmail.com>
To: gps at asu.edu
Cc: "r-help at r-project.org" <r-help at r-project.org>
Sent: Tuesday, May 8, 2012 2:33 PM
Subject: Re: [R] grouping function

Hi,
On Tue, May 8, 2012 at 2:17 PM, Geoffrey Smith <gps at asu.edu> wrote:
Thanks for providing reproducible data. Two minor points: you don't
need ; at the end of lines, and calling your data frame df is
confusing because there's a df() function.
This isn't a clear specification:
1990, 1994 for instance fits into all three groups. Do you want to
extend this to more start years, or are you only interested in those
three? Assuming end is always >= start, you don't even need to
consider the end years in your grouping.

Here are two methods, one that "looks like" your pseudocode, and one
that is more R-ish. They give different results because of different
handling of cases that fit all three groups. Rearranging the
statements in makegroup1() from broadest to most restrictive would
make it give the same result as makegroup2().


makegroup1 <- function(x,y) {
group <- numeric(length(x))
group[x <= 1990 & y > 1990] <- 1
group[x <= 1991 & y > 1991] <- 2
group[x <= 1992 & y > 1992] <- 3
group
}

makegroup2 <- function(x, y) {
???ifelse(x <= 1990 & y > 1990, 1,
? ? ? ifelse(x <= 1991 & y > 1991, 2,
?????? ???ifelse(x <= 1992 & y > 1992, 3, 0)))
}
[1] 3 3 3 0 0 3 3 0 0 0 3 0 0 0 0
[1]? 1? 2? 3 NA NA? 2? 3 NA NA NA? 3 NA NA NA NA
But really, it's a better idea to develop an unambiguous statement of
your desired output.

Sarah
#
Sorry, yes: I changed it before posting it to more closely match what
the default value in the pseudocode. That's a very minor issue: the
very last value in the nested ifelse() statements is what's used by
default.

Sarah
On Tue, May 8, 2012 at 2:46 PM, arun <smartpink111 at yahoo.com> wrote: