Adam - 1 month ago 7x

R Question

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.

Answer

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))
```

Source (Stackoverflow)

Comments