如何将值分组为数据集中的较小值集? [英] how group values into smaller sets of values in a data set?

查看:76
本文介绍了如何将值分组为数据集中的较小值集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有53个值的单列数据集.我要实现的目标是根据400点的差异将它们分为几组,范围从〜500到4500之间.如果需要,您可以含糊其词,并声明一个函数,其余的我可以解决

basically I have a single column data set of 53 values. what I am trying to achieve is binning them into sets based on a 400 point difference, ranging from ~500 to 4500. you can just be vague if needed and state a function for doing so, I can work out the rest

推荐答案

一个dplyr选项

library(dplyr)
df_test <- data.frame(x = runif(1000, 400, 5000),
                      y = rep("A", 1000))

df_test <- df_test %>% 
  mutate(bins = case_when(between(x, 400, 800) ~ "Set 1",
                          between(x, 801, 1600) ~ "Set 2",
                          between(x, 1601, 5000) ~ "Set 3"))
head(df_test)
              x y  bins
    1 1687.2854 A Set 3
    2 3454.1035 A Set 3
    3 4979.5434 A Set 3
    4  796.6475 A Set 1
    5 3665.7444 A Set 3
    6 3083.8969 A Set 3

您当然可以根据需要调整between范围.

You can of course adjust the between ranges as you see fit.

这篇关于如何将值分组为数据集中的较小值集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆