Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of columns by a condition (>) for each row

Tags:

r

count

col

I am trying to work out for each row of a matrix how many columns have values greater than a specified value. I am sorry that I am asking this simple question but I wasn't able to figure it out.

I have extracted maximum temperature values from a raster stack, of multiple years of rasters, for some spatial points I am interested in. The data looks similar to:

data <- cbind('1990' = c(25, 22, 35, 42, 44), '1991' = c(23, 28, 33, 40, 45), '1992' = c(20, 20, 30, 41, 43))      1990   1991   1992 1     25     23     20 2     22     28     20 3     35     33     30 4     42     40     41 5     44     45     43 

I want to end up with the number of years that the temperature was above 30 for each location, eg.:

    yr.above    1          0 2          0 3          2 4          3 5          3 

I have tried a few things but they didn't work and were pretty illogical (e.g. trying length(data[1:length(data), which(blah blah doesn't make sense)), or apply(data, 1, length(data) > 30), I know these don't make sense but I am a bit stuck.

like image 308
Adam Avatar asked Sep 18 '13 00:09

Adam


People also ask

How do I count the number of elements in a row in R?

You can use the rowSums() function to do this. As the name suggests, this function sums the values of all elements in a row. Since TRUEs are equal to 1 and FALSEs are equal to 0, summing the number of TRUEs is the same as counting the number of NA's. The rowSums() function returns a numeric vector.

How do I count the number of rows in each column?

If you need a quick way to count rows that contain data, select all the cells in the first column of that data (it may not be column A). Just click the column header. The status bar, in the lower-right corner of your Excel window, will tell you the row count.

How do I count columns in R?

The ncol() function in R programming That is, ncol() function returns the total number of columns present in the object.

How do I count occurrences in R Dataframe?

To count occurrences between columns, simply use both names, and it provides the frequency between the values of each column. This process produces a dataset of all those comparisons that can be used for further processing.

How do you count a column in a table in Excel?

Using COUNTIF Function You can count one column based on criteria in another column by using the COUNTIF function. Suppose we want to count the number of salesmen who sell in Jacksonville. To find out the number, type the formula in an empty cell, Here, B6:B13 = Range of the dataset where the count takes place

How do you count in Excel with multiple criteria?

Using COUNTIFS Function The COUNIFS function is used when the count is made based on multiple criteria. You can count one column based on criteria in multiple columns using the COUNTIFS function. Suppose we want to count the number of salesmen who sell in Jacksonville and who sell cars. To find out the number, type the formula in an empty cell,

Why use dcount to conditional count across columns in Google Sheets?

It is because we have infinite rows in our database. This logical test helps us to return the result only in the rows where column A has values. That’s all about how to use DCOUNT to conditional count across columns and return row by row output in Google Sheets.

How to use countif across columns in Excel?

When you insert new rows, each time, you may require to copy-paste the formula. Hence, an array formula equivalent to Countif across columns row by row will be more preferable. As I have mentioned, we can use MMULT or DCOUNT for the same. This post explains the DCOUNT use.


1 Answers

This will give you the vector you are looking for:

rowSums(data > 30) 

It will work whether data is a matrix or a data.frame. Also, it uses vectorized functions, hence is a preferred approach over using apply which is little more than a (slow) for loop.

If data is a data.frame, you can add the result as a column by doing:

data$yr.above <- rowSums(data > 30) 

or if data is a matrix:

data <- cbind(data, yr.above = rowSums(data > 30)) 

You can also create a whole new data.frame:

data.frame(yr.above = rowSums(data > 30)) 

or a whole new matrix:

cbind(yr.above = rowSums(data > 30)) 
like image 88
flodel Avatar answered Sep 19 '22 19:09

flodel