Grouping by 3 variable and renaming groups
Rui
Your first code worked just fine.
Jeff
-----Original Message-----
From: Rui Barradas <ruipbarradas at sapo.pt>
Sent: Saturday, May 26, 2018 8:30 AM
To: reichmanj at sbcglobal.net; 'R-help' <r-help at r-project.org>
Subject: Re: [R] Grouping by 3 variable and renaming groups
Hello,
Sorry, but I think my first answer is wrong.
You probably want something along the lines of
sp <- split(priceStore_Grps, priceStore_Grps$StorePC) res <- lapply(seq_along(sp), function(i){
sp[[i]]$StoreID <- paste("Store", i, sep = "_")
sp[[i]]
})
res <- do.call(rbind, res)
row.names(res) <- NULL
Hope this helps,
Rui Barradas
On 5/26/2018 2:22 PM, Rui Barradas wrote:
Hello,
See if this is it:
priceStore_Grps$StoreID <- paste("Store",
seq_len(nrow(priceStore_Grps)), sep = "_")
Hope this helps,
Rui Barradas
On 5/26/2018 2:03 PM, Jeff Reichman wrote:
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]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.