简体   繁体   中英

Create index column for n unique intervals in R

I have a column that looks like the following:

pscore  pscoreblocks
0.18    (.177, 0.187)
0.24    (0.237, 0.246)
0.07    (0.069, 0.079)

I created the pscoreblocks column by dividing the pscore column into 100 evenly spaced intervals.

dfc$pscoreblocks <- cut_interval(dfc$pscore, n=100)

How can I create a new column with a unique number for each of the 100 intervals?

pscore  pscoreblocks    block_number
0.18    (.177, 0.187)   3
0.24    (0.237, 0.246)  5
0.07    (0.069, 0.079)  1

Convert pscoreblocks to ineteger.

dfc$block_number <- as.integer(dfc$pscoreblocks)

You can also use match and unique

dfc$block_number <- match(dfc$pscoreblocks, unique(dfc$pscoreblocks))

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