user53020
user53020

Reputation: 889

R function for counting how often a value falls below a particular value

This is proving to be a monster for me with zero experience in R script. I have a data frame with 57 columns, 30 rows of data

Here is what I am trying to do:

1) Go to each column:

2) Count the number of times 2/3/4/5/6/7/8/9 consecutive values are less than -1

3) Print the result as a text file

4) Repeat step 2 and 3 for the second column and so on

I looked around and also on r stackoverflow

check number of times consecutive value appear based on a certain criteria

This is one column of my data:

data<-c(-0.996,-1.111,-0.638,0.047,0.694,1.901,2.863,2.611,2.56,2.016,0.929,-0.153,-0.617,-0.143
0.199,0.556,0.353,-0.638,0.347,0.045,-0.829,-0.882,-1.143,-0.869,0.619,0.923,-0.474,0.227
0.394,0.789,1.962,1.132,0.1,-0.278,-0.303,-0.606,-0.705,-0.858,-0.723,-0.081,1.206,2.329
1.863,2.1,1.547,2.026,0.015,-0.441,-0.371,-0.304,-0.668,-0.953,-1.256,-1.185,-0.891,-0.569
0.485,0.421,-0.004,0.024,-0.39,-0.58,-1.178,-1.101,-0.882,0.01,0.052,-0.166,-1.703,-1.048
-0.718,-0.036,-0.561,-0.08,0.272,-0.041,-0.811,-0.929,-0.853,-1.047,0.431,0.576,0.642,1.62
2.324,1.251,1.384,0.195,-0.081,-0.335,-0.176,1.089,-0.602,-1.134,-1.356,-1.203,-0.795,-0.752
-0.692,-0.813,-1.172,-0.387,-0.079,-0.374,-0.157,0.263,0.313,0.975,2.298,1.71,0.229,-0.313
-0.779,-1.12,-1.102,-1.01,-0.86,-1.118,-1.211,-1.081,-1.156,-0.972)

When I run the following code:

for (col in 1:ncol(data)) {
    runs <- rle(data[,col])
    print(runs$lengths[which(runs$values < -1)])
 }

It gives me this:

[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

It has counted the number of values <-1 but not runs. Is it something that I am during wrong here?

Upvotes: 0

Views: 130

Answers (1)

hrbrmstr
hrbrmstr

Reputation: 78822

(massive edit)

Fixed data vector (was missing commas):

data <- c(-0.996,-1.111,-0.638,0.047,0.694,1.901,2.863,2.611,2.56,2.016,0.929,-0.153,-0.617,-0.143,
          0.199,0.556,0.353,-0.638,0.347,0.045,-0.829,-0.882,-1.143,-0.869,0.619,0.923,-0.474,0.227,
          0.394,0.789,1.962,1.132,0.1,-0.278,-0.303,-0.606,-0.705,-0.858,-0.723,-0.081,1.206,2.329,
          1.863,2.1,1.547,2.026,0.015,-0.441,-0.371,-0.304,-0.668,-0.953,-1.256,-1.185,-0.891,-0.569,
          0.485,0.421,-0.004,0.024,-0.39,-0.58,-1.178,-1.101,-0.882,0.01,0.052,-0.166,-1.703,-1.048,
          -0.718,-0.036,-0.561,-0.08,0.272,-0.041,-0.811,-0.929,-0.853,-1.047,0.431,0.576,0.642,1.62,
          2.324,1.251,1.384,0.195,-0.081,-0.335,-0.176,1.089,-0.602,-1.134,-1.356,-1.203,-0.795,-0.752,
          -0.692,-0.813,-1.172,-0.387,-0.079,-0.374,-0.157,0.263,0.313,0.975,2.298,1.71,0.229,-0.313,
          -0.779,-1.12,-1.102,-1.01,-0.86,-1.118,-1.211,-1.081,-1.156,-0.972)

Doing data < -1 gives you a logical vector, and we can count runs of TRUE & FALSE:

runs <- rle(data < -1)

print(runs)

## Run Length Encoding
##   lengths: int [1:21] 1 1 20 1 29 2 8 2 4 2 ...
##   values : logi [1:21] FALSE TRUE FALSE TRUE FALSE TRUE ...

Then extract the length of only the TRUE runs:

print(runs$lengths[which(runs$values)])
##  [1] 1 1 2 2 2 1 3 1 3 4

and, iterate over columns of a data frame as previously shown:

# make a data frame from sampled versions of data

set.seed(1492) # repeatable

df <- data.frame(V1=data,
                 V2=sample(data, length(data), replace=TRUE),
                 V3=sample(data, length(data), replace=TRUE),
                 V4=sample(data, length(data), replace=TRUE))

# do the extraction

for (col in 1:ncol(df)) {
    runs <- rle(df[, col] < -1)
    print(runs$lengths[which(runs$values)])
}

##  [1] 1 1 2 2 2 1 3 1 3 4
##  [1] 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1
##  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1
##  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Upvotes: 1

Related Questions