Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Classification #1

Open
nevrome opened this issue May 22, 2021 · 0 comments
Open

Classification #1

nevrome opened this issue May 22, 2021 · 0 comments

Comments

@nevrome
Copy link

nevrome commented May 22, 2021

I just came across this interesting research compendium and scrolled a bit through the code. I believe a construct like this could be replaced with less verbose code:

aorist_melt$period<-ifelse(aorist_melt$variable<=1100 & aorist_melt$variable>=980,"SM",
ifelse(aorist_melt$variable<=970 & aorist_melt$variable>=930,"EPG",
ifelse(aorist_melt$variable<=920 & aorist_melt$variable>=880,"MPG",
ifelse(aorist_melt$variable<=870 & aorist_melt$variable>=850,"LPG",
ifelse(aorist_melt$variable<=840 & aorist_melt$variable>=820,"PGB",
ifelse(aorist_melt$variable<=810 & aorist_melt$variable>=800,"EG",
ifelse(aorist_melt$variable<=790 & aorist_melt$variable>=750,"MG",
ifelse(aorist_melt$variable<=740 & aorist_melt$variable>=720,"LG",
ifelse(aorist_melt$variable<=710 & aorist_melt$variable>=680,"EO",
ifelse(aorist_melt$variable<=670 & aorist_melt$variable>=640,"LO",
ifelse(aorist_melt$variable<=630 & aorist_melt$variable>=600,"A",""
)))))))))))

For this specific numeric-to-label transformation one can use cut in base R or - more flexible - dplyr::case_when. Cut returns an ordered factor, which could actually be useful here. For example:

x <- iris$Petal.Width

iris$PW_class1 <- base::cut(
  x,
  breaks = c(0, 1, 2, 3),
  labels = c("small", "medium", "large")
)

iris$PW_class2 <- dplyr::case_when(
  x > 0 & x <= 1 ~ "small",
  x > 1 & x <= 2 ~ "medium",
  x > 2 & x <= 3 ~ "large",
  TRUE ~ "" # if you really need to cover this case
)

knitr::kable(tail(iris))
Sepal.Length Sepal.Width Petal.Length Petal.Width Species PW_class1 PW_class2
145 6.7 3.3 5.7 2.5 virginica large large
146 6.7 3.0 5.2 2.3 virginica large large
147 6.3 2.5 5.0 1.9 virginica medium medium
148 6.5 3.0 5.2 2.0 virginica medium medium
149 6.2 3.4 5.4 2.3 virginica large large
150 5.9 3.0 5.1 1.8 virginica medium medium

Sorry for crashing in here with my unsolicited advice. I just thought this might be interesting to you 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant