val
val

Reputation: 1699

R extract or split an interval into vectors

What is this operation called and how do I achieve this? (I can't find an example.)

Given

temp1
               Var1 Freq
1          (0,0.78]    0
2       (0.78,0.99]    0
3       (0.99,1.07]    0
4      (1.07,1.201]    1
5     (1.201,1.211]    0
6      (1.211,1.77]    2

How do I split the intervals in Var1 into two vectors for start and end?

Like this

df2
  start   end Freq
1 0.000 0.780    0
2 0.780 0.990    0
3 0.990 1.070    0
4 1.070 1.201    1
5 1.201 1.211    0
6 1.211 1.770    2

Upvotes: 0

Views: 174

Answers (1)

thelatemail
thelatemail

Reputation: 93813

This is an XY problem. You shouldn't need to have that format to fix in the first place. E.g.:

x <- 1:10
brks <- c(0,5,10)
data.frame(table(cut(x,brks)))

#    Var1 Freq
#1  (0,5]    5
#2 (5,10]    5

data.frame(start=head(brks,-1), end=tail(brks,-1), Freq=tabulate(cut(x,brks)))

#  start end Freq
#1     0   5    5
#2     5  10    5

Upvotes: 4

Related Questions