Укажите уровни факторов для интервалов

У меня есть непрерывная переменная, которую я хотел бы превратить в фактор.

Данные:

DCA<-c(0.14,0.14,0.16,0.16,0.27,0.27,0.07,0.07,0.41,0.41,0.00,0.00,0.33,0.33,0.11,0.11,0.64,0.64,0.28,0.28,0.02,0.02,0.43,0.43,0.24,0.24,0.08,0.08,0.00,0.00,0.64,0.64,0.07,0.07,0.16,0.16,0.24,0.24,0.26,0.26,0.64,0.64,0.22,0.22,0.03,0.03,0.03,0.03,0.35,0.35,0.35,0.35,0.37,0.37,0.37,0.37,0.22,0.22,0.00,0.00,0.33,0.33,0.19,0.19,0.33,0.33,0.33,0.33,0.02,0.02,0.36,0.36)

Я знаю, что вы можете сделать это быстро с помощью

DCA.f<-factor(DCA)

но это создает факторы для каждого уникального значения. Я хочу указать уровни факторов, чтобы уровни

0, меньше или равно 0,10, больше 0,10 и меньше или равно 0,20 и т.д. И т.д., Пока не будет больше 0,50.

Я думаю, что это должно быть указано в "уровнях" в фактор-функции, но я не знаю, как указать, что я описал выше.

1 ответ

Посмотрите в cut:

cut(DCA, c(-Inf, seq(0, .5, .1), Inf))
# [1] (0.1,0.2] (0.1,0.2] (0.1,0.2] (0.1,0.2] (0.2,0.3] (0.2,0.3] (0,0.1] 
# [8] (0,0.1] (0.4,0.5] (0.4,0.5] (-Inf,0] (-Inf,0] (0.3,0.4] (0.3,0.4] 
# [15] (0.1,0.2] (0.1,0.2] (0.5, Inf] (0.5, Inf] (0.2,0.3] (0.2,0.3] (0,0.1] 
# [22] (0,0.1] (0.4,0.5] (0.4,0.5] (0.2,0.3] (0.2,0.3] (0,0.1] (0,0.1] 
# [29] (-Inf,0] (-Inf,0] (0.5, Inf] (0.5, Inf] (0,0.1] (0,0.1] (0.1,0.2] 
# [36] (0.1,0.2] (0.2,0.3] (0.2,0.3] (0.2,0.3] (0.2,0.3] (0.5, Inf] (0.5, Inf]
# [43] (0.2,0.3] (0.2,0.3] (0,0.1] (0,0.1] (0,0.1] (0,0.1] (0.3,0.4] 
# [50] (0.3,0.4] (0.3,0.4] (0.3,0.4] (0.3,0.4] (0.3,0.4] (0.3,0.4] (0.3,0.4] 
# [57] (0.2,0.3] (0.2,0.3] (-Inf,0] (-Inf,0] (0.3,0.4] (0.3,0.4] (0.1,0.2] 
# [64] (0.1,0.2] (0.3,0.4] (0.3,0.4] (0.3,0.4] (0.3,0.4] (0,0.1] (0,0.1] 
# [71] (0.3,0.4] (0.3,0.4] 
# 7 Levels: (-Inf,0] (0,0.1] (0.1,0.2] (0.2,0.3] (0.3,0.4] ... (0.5, Inf]

Вы можете настроить второй аргумент (" breaks ") для представления перерывов, которые вы действительно ищете, а также изучить некоторые другие аргументы, которые могут быть переданы функции cut.

licensed under cc by-sa 3.0 with attribution.