Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Betty888
Helper II
Helper II

R in Power suery : How to extend my code that works by group to all data

Dear all,
I'm trying to analyse the effect of "Factor1" and "Factor2" on the response variable "perf" by "location", and obtaining at the end my original dataset associated to the lsmeans and tukey groups letters. Here below the code I'm using that gives me exactly what I need but only by location, my need is : how can I extend it in a way my output combines results of all locations, i.e, output of lsmeans and CLD by location :

library(multcomp)
library(lsmeans)
library(dplyr)
library(broom)

output <- as.data.frame(dataset)
output$perf <- as.numeric(output$perf)
output$factor1 <- as.factor(output$factor1)
output$factor2 <- as.factor(output$factor2)
output <- output %>% 
           group_by(location) %>% 
           mutate(row=row_number()) 

model <- lm(perf ~ factor1 + factor2, data = output)
lsmeans <- lsmeans(model, ~ factor1)
CLD = cld(lsmeans,
            alpha   = 0.05,
            Letters = letters,
            adjust  = "tukey")         

output <- left_join(output, CLD, by = "factor1")
Thank you in advance for your help !
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Betty888 

You can consider to create a new column instead of group_by, you can refer to the following link, it may help you.

Concatenating strings with multiple separators using paste() in R - Stack Overflow

 

Best Regards!

Yolo Zhu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi @Betty888 

You can consider to create a new column instead of group_by, you can refer to the following link, it may help you.

Concatenating strings with multiple separators using paste() in R - Stack Overflow

 

Best Regards!

Yolo Zhu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Many thanks ! 

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.