cut提取最小和最大切割级别作为data.frame中的列 [英] cut extracting the minimum and maximum of cut levels as columns in data.frame
本文介绍了cut提取最小和最大切割级别作为data.frame中的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数字向量,我想转换为五个数字级别。
我可以使用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
这篇关于cut提取最小和最大切割级别作为data.frame中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文