我有一个数字向量,我想转换为五个数字级别。 我可以使用cut来获得五个级别
I have a numeric vector that I want to convert to five numeric levels. I can get the five levels using cut
dx <- data.frame(x=1:100) dx$cut <- cut(dx$x,5)但是我是现在在提取关卡的上下边界时遇到问题。 例如,在 dx $ min 中,(0.901,20.8]为0.901,在 dx $ max中为20.8 code>。
But I am now having problems extracting the lower and upper boundaries of the levels. So for example (0.901,20.8] would be 0.901 in dx$min and 20.8 in dx$max.
我尝试过:
dx$min <- pmin(dx$cut) dx$max <- pmax(dx$cut) dx但是这不起作用。
推荐答案您可以尝试分割标签(转换为预先字符,并进行了修改以抑制标点符号,但,和除外。),然后根据逗号创建两列:
you can try splitting the labels (converted to character beforehand and modified to suppress the punctuation except , and .) according to the comma and then create 2 columns:
min_max <- unlist(strsplit(gsub("(?![,.])[[:punct:]]", "", as.character(dx$cut), perl=TRUE), ",")) # here, the regex ask to replace every punctuation mark except a . or a , by an empty string dx$min <- min_max[seq(1, length(min_max), by=2)] dx$max <- min_max[seq(2, length(min_max), by=2)] head(dx) # x cut min max #1 1 (0.901,20.8] 0.901 20.8 #2 2 (0.901,20.8] 0.901 20.8 #3 3 (0.901,20.8] 0.901 20.8 #4 4 (0.901,20.8] 0.901 20.8 #5 5 (0.901,20.8] 0.901 20.8 #6 6 (0.901,20.8] 0.901 20.8