简体   繁体   中英

How to create ranges in R and if the numbers in a column of my dataframe matches the interval, we can create a new column labeled by 1

Say I have a column of values in my data frame that consists of

[1.0,1.01,1.1,2.01,2.1]

I want to create an interval for example [1.0,1.1) inclusive,non-inclusive I also have a column vector filled with 0's that have the same number of numbers as my column of values.

If the col of values matches the interval, we change the interval number from 0 to 1.

What is the easiest way to do this in R? And also how can I automate this for a larger column of values?

(Creating multiple intervals such as [1.0,1.1) + [1.1,1.2) + [1.2,1.3) + etc)

We can use cut to create the bins by specifying the breaks and then split by the bins created to a list of vector s

grp <- cut(df1$col, breaks = c(-Inf, seq(1.0, 2.0, by = 0.1), Inf))
lst1 <- split(df1$col, grp)

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