简体   繁体   中英

Split dataframe into X groups from 0 to 1 based on column values

I am fairly new to R and can't find a concise way to a problem.

I have a dataframe in R called df that looks as such. It contain a column called values that contains values from 0 to 1 ordered numerically and a binary column called flag that contains either 0 or 1.

df
value     flag
0.033     0
0.139     0
0.452     1
0.532     0
0.687     1
0.993     1

I wish to split this dataframe into X amount of groups from 0 to 1 binning the values column. For example if I wished a 4 split grouping, the data would be split from 0-0.25, 0.25-0.5, 0.5-0.75, 0.75-1. This data would also contain the corresponding flag to that point.

THE ANSWER SHOULD ONLY USE DATAFRAME FORMAT AS INPUT, ONLY THE COLUMNS STATED IN THIS QUESTION AND ONLY PACKAGES FROM TIDYVERSE, CARET OR DATA.TABLE.

I want to solution to be scalable so if I wished to split it into more group then I can.

Does anyone have a solution for this? Thanks

I cant see how the answers you got earlier are not scaleable, they use native R with no packages... if n is number of partitions you want:

n = 4
L = seq(1,n)/n

GroupedList = lapply(L,function(x){
                 df[(df$value < x) & (df$value > (x-(1/n))),]
               })

Perhaps the cut() function can help you? This divides the range of a numeric column into a custom amount of intervals:

n <- 4
breaks <- seq(0, 1, by = 1/n)

df$group <- cut(df$value, breaks = breaks)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM