You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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.Widthiris$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 👍
The text was updated successfully, but these errors were encountered:
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:
knossoscemeteries/R/Samples.R
Lines 52 to 63 in bdfb456
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:Sorry for crashing in here with my unsolicited advice. I just thought this might be interesting to you 👍
The text was updated successfully, but these errors were encountered: