Recode continuous variable_summary

116 Views Asked by At

enter image description here

After recode from continuous variables into categorical. The summary for the new categorical variable doens't show how it summarize for categorical in levels we often see.

Please help!

3

There are 3 best solutions below

0
ekoam On

Try this

A4 <- within(A4, resize <- factor(Size > 1000, c("S", "L")))
0
Rui Barradas On

cut does precisely this.

A4$resize <- cut(A4$Size, breaks = c(-Inf, 1000, Inf), labels = c("S", "L"))
0
akrun On

We can use findInterval

A4$resize <- with(A4, c('S', 'L')[findInterval(Size, 1000)])

Or using case_when

library(dplyr)
A4 %>%
   mutate(resize = case_when(Size > 1000 ~ "S", TRUE ~ "L"))