Get categories from numeric vector

前端 未结 2 1668
再見小時候
再見小時候 2020-12-21 08:56

I have this numeric vector:

vec <- 1:7

How can I transform it into 3 categories using these logical rules:

if(vec >=          


        
2条回答
  •  旧巷少年郎
    2020-12-21 09:32

    I'd recommend cut and its labels variable

    cut(vec, c(1, 3, 5, 8), include.lowest = TRUE, labels = paste0("category", 1:3))
    # [1] category1 category1 category1 category2 category2 category3 category3
    # Levels: category1 category2 category3
    

提交回复
热议问题