Skip to content

Instantly share code, notes, and snippets.

@madilk
Last active March 1, 2021 20:55
Show Gist options
  • Save madilk/8c9621eeba92ebecd7e193797dcffe55 to your computer and use it in GitHub Desktop.
Save madilk/8c9621eeba92ebecd7e193797dcffe55 to your computer and use it in GitHub Desktop.
Scale function example in R - K means clustering
mean(data_strings_removed$br)
sd(data_strings_removed$br)
data_br_scaled <- data_br%>%
mutate(br_scaled=((br-br_mean)/br_sd))
#wrangled data used in k means clustering example
#https://analyticslog.com/blog/2021/2/20/google-analytics-content-segmentation-via-k-means-clustering-in-r-programming
#has been uploaded to google sheets. Below example uses it:
#https://docs.google.com/spreadsheets/d/1W8tzRMV0hHpa1xpVgRA0iqwKEI9-JPEuSWUCen1Ehqg/edit#gid=0
#Readxl. Read XL file from PC
data <- data_for_k_means
#load tidyverse
library(tidyverse)
#SO solution to convert string to # row, required for scaling
data_strings_removed <- data %>% remove_rownames %>%
column_to_rownames(var="postType")
data_scaled<- scale(data_strings_removed)
View(data_scaled)
?scale()
mean(data_strings_removed$br)
sd(data_strings_removed$br)
data_br<- data_strings_removed%>%
select(starts_with("br"))%>%
mutate(br_mean = mean(data_strings_removed$br)) %>%
mutate (br_sd = sd(data_strings_removed$br))
data_br
View(data_br)
data_br_scaled <- data_br%>%
mutate(br_scaled=((br-br_mean)/br_sd))
data_br_scaled
View(data_br_scaled)
#https://stackoverflow.com/questions/47859440/viewing-single-column-of-data-frame-in-r
View(data_scaled[,1, drop=FALSE])
data_scaled[,1,drop=FALSE]
#wrangled data used in k means clustering example
#https://analyticslog.com/blog/2021/2/20/google-analytics-content-segmentation-via-k-means-clustering-in-r-programming
#has been uploaded to google sheets. Below example uses it:
#https://docs.google.com/spreadsheets/d/1W8tzRMV0hHpa1xpVgRA0iqwKEI9-JPEuSWUCen1Ehqg/edit#gid=0
#Readxl. Read XL file from PC
data <- data_for_k_means
#load tidyverse
library(tidyverse)
#SO solution to convert string to # row, required for scaling
data_strings_removed <- data %>% remove_rownames %>%
column_to_rownames(var="postType")
data_scaled<- scale(data_strings_removed)
View(data_scaled)
data_br<- data_strings_removed%>%
select(starts_with("br"))%>%
mutate(br_mean = mean(data_strings_removed$br)) %>%
mutate (br_sd = sd(data_strings_removed$br))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment