[R] Establishing groups using something other than ifelse()
Sam Albers
tonightsthenight at gmail.com
Fri Jan 20 00:45:25 CET 2012
On Thu, Jan 19, 2012 at 3:34 PM, Justin Haynes <jtor14 at gmail.com> wrote:
> how bout
>
> levels(df$z)[grep('A',levels(df$z))] <- 'A'
> levels(df$z)[grep('B',levels(df$z))] <- 'B'
> levels(df$z)[grep('C',levels(df$z))] <- 'C'
>
> does that do what you're wanting?
Shoot. Might have made my example confusing, sorry. First of all I
want to retain the information in the sub.group (z) here but more
importantly, I used A1 and A2 to illustrate the grouping under the
larger group A but the pattern of the group names is irrelevant for my
purposes. So to modify the example I wanted to achieve this without
pattern matching like the above:
df <- data.frame(x=runif(36, 0, 120),
y=runif(36, 0, 120),
z=factor(c("G1","G1","G2","G2","H1","H1","H2","H2","I1","I1","I2","I2"))
)
df$Big.Group <- with(df, ifelse(df$z=="G1","A", ifelse(df$z=="G2","A",
ifelse(df$z=="H1", "B", ifelse(df$z=="H2", "B", "C")))))
Thanks for the response!
Sam
>
>
> On Thu, Jan 19, 2012 at 3:05 PM, Sam Albers <tonightsthenight at gmail.com>
> wrote:
>>
>> Hello all,
>>
>> This is one of those "Is there a better way to do this questions". Say
>> I have a dataframe (df) with a grouping variable (z). This is my base
>> data. Now I know that there is a higher order level of grouping that
>> exist for my group variable. So what I want to do is create a new
>> column that express that higher order level of grouping based on
>> values in the sub-group (z in this case). In the past I have used
>> ifelse() but this tends to get fairly redundant and messy with a large
>> amount of sub-groupings (z). I've created a sample dataset below. Can
>> anyone recommend a better way of achieving what I am currently
>> achieving with ifelse()? A long series of ifelse statements makes me
>> think that there is something better for this.
>>
>> ## Dataframe creation
>> df <- data.frame(x=runif(36, 0, 120),
>> y=runif(36, 0, 120),
>>
>> z=factor(c("A1","A1","A2","A2","B1","B1","B2","B2","C1","C","C2","C2"))
>> )
>>
>> ## Current method is grouping
>> df$Big.Group <- with(df, ifelse(df$z=="A1","A", ifelse(df$z=="A2","A",
>> ifelse(df$z=="B1", "B", ifelse(df$z=="B2", "B", "C")))))
>>
>>
>> So any suggestions? Thanks in advance!
>>
>> Sam
>>
>> ______________________________________________
>> 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