最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

cut提取最小和最大切割级别作为data.frame中的列

SEO心得admin87浏览0评论
本文介绍了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
发布评论

评论列表(0)

  1. 暂无评论