[R] Grouping by 3 variable and renaming groups

Jeff Reichman reichm@nj @ending from @bcglob@l@net
Sat May 26 15:03:20 CEST 2018


ALCON

 

I'm trying to figure out how to rename groups in a data frame after groups
by selected variabels.  I am using the dplyr library to group my data by 3
variables as follows

 

# group by lat (StoreX)/long (StoreY)

priceStore <- LapTopSales[,c(4,5,15,16)]

priceStore <- priceStore[complete.cases(priceStore), ]  # keep only non NA
records

priceStore_Grps <- priceStore %>%

  group_by(StorePC, StoreX, StoreY) %>%

  summarize(meanPrice=(mean(RetailPrice)))

 

which results in .

 

> priceStore_Grps

# A tibble: 15 x 4

# Groups:   StorePC, StoreX [?]

   StorePC  StoreX StoreY meanPrice

   <fct>     <int>  <int>     <dbl>

1 CR7 8LE  532714 168302      472.

2 E2 0RY   535652 182961      520.

3 E7 8NW   541428 184515      467.

4 KT2 5AU  517917 170243      522.

5 N17 6QA  533788 189994      523.

 

Which is fine, but I then want to give each group (e.g. CR7 8LE  532714
168302) a unique identifier (say) Store 1, 2, 3 or some other unique
identifier.

 

   StorePC  StoreX StoreY meanPrice

   <fct>     <int>  <int>     <dbl>

1 CR7 8LE  532714 168302      472.   Store 1

2 E2 0RY   535652 182961      520.   Store 2

3 E7 8NW   541428 184515      467.   Store 3

4 KT2 5AU  517917 170243      522.   Store 4

5 N17 6QA  533788 189994      523.   Store 5

 


	[[alternative HTML version deleted]]



More information about the R-help mailing list