val
val

Reputation: 1709

How to count these transitions - in R

Given a table of values, where A = state of system, B = length of state, and C = cumulative length of states:

A     B      C
1   1.16    1.16
0   0.51    1.67
1   1.16    2.84
0   0.26    3.10
1   0.59    3.69
0   0.39    4.08
1   0.78    4.85
0   0.90    5.75
1   0.78    6.53
0   0.26    6.79
1   0.12    6.91
0   0.51    7.42
1   0.26    7.69
0   0.51    8.20
1   0.39    8.59
0   0.51    9.10
1   1.16    10.26
0   1.10    11.36
1   0.59    11.95
0   0.51    12.46

How would I use R to calculate the number of transitions (where A gives the state) per constant interval length - where the intervals are consecutive and could be any arbitrary number (I chose a value of 2 in my image example)? For example, using the table values or the image included we count 2 transitions from 0-2, 3 transitions from greater than 2-4, 3 transitions from >4-6, etc.enter image description here

Upvotes: 2

Views: 204

Answers (1)

gung - Reinstate Monica
gung - Reinstate Monica

Reputation: 11903

This is straightforward in R. All you need is column C and ?cut. Consider:

d <- read.table(text="A     B      C
1   1.16    1.16
0   0.51    1.67
1   1.16    2.84
0   0.26    3.10
1   0.59    3.69
0   0.39    4.08
1   0.78    4.85
0   0.90    5.75
1   0.78    6.53
0   0.26    6.79
1   0.12    6.91
0   0.51    7.42
1   0.26    7.69
0   0.51    8.20
1   0.39    8.59
0   0.51    9.10
1   1.16    10.26
0   1.10    11.36
1   0.59    11.95
0   0.51    12.46", header=TRUE)

fi <- cut(d$C, breaks=seq(from=0, to=14, by=2))
table(fi)
# fi
#   (0,2]   (2,4]   (4,6]   (6,8]  (8,10] (10,12] (12,14] 
#      2       3       3       5       3       3       1 

Upvotes: 3

Related Questions