将数字矢量切成垃圾箱

时间:2020-11-11 15:35:50

标签: r dataframe

我想将值从范围更改为名称。我分为三个组,我想将这些值更改为这样的名称:

(9.94e+03,6.3e+04] -> high
(6.3e+04,1.16e+05] -> medium
(1.16e+05,1.69e+05] -> low

我需要在此代码中添加什么?

dm2 <- mutate(dm1,
              Levels.Salary = cut(dm1$Salary,3))

2 个答案:

答案 0 :(得分:1)

您可以在labels中使用cut

cut(0:9, 3, c("low", "medium", "high"))
# [1] low    low    low    low    medium medium medium high   high   high  
#Levels: low medium high

答案 1 :(得分:1)

扩展GKi的答案:

int set_text(std::string text) {
    u_int i, word_start, word_end;
    i = word_start = word_end = 0;

    while (i <= text.length()) {
        if (text[i] == ' ' || text[i] == '\0') {
            word_end = i;

            std::string substr = text.substr(word_start, word_end);
            // empty string argument to append_word has no effect
            if (append_word("", substr) < 0)
                return -1;
            i = word_start = ++word_end;
        } else {
            i++
        }
    }
    
    return 0;
}

但是请注意,您的标签从休息时间开始按降序排列。那是你想要的吗?