user451151 - 4 months ago 33

R Question

Let's say I have the following:

`myseq <- seq(0, 1, by = 0.1)`

scores <- sample(seq(0, 1, by = 0.01), 10)

var1 <- sample(c(0,1), 10, replace = T)

var2 <- sample(c(0,1), 10, replace = T)

mydf <- data.frame(scores = scores, var1 = var1, var2 = var2)

myseq

[1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0

mydf

scores var1 var2

1 0.10 1 0

2 0.06 1 0

3 0.74 0 0

4 0.15 1 0

5 0.40 1 1

6 0.96 0 0

7 0.04 1 0

8 0.71 0 1

9 0.94 1 1

10 0.38 0 0

For each value in

`myseq`

`var1`

`var2`

`scores`

`myseq`

So, for example:

The first value in

`myseq`

`0.0`

`scores`

`0.0`

`var1`

`6`

`var2`

`3`

The second value in

`myseq`

`0.1`

`scores`

`0.1`

`var1`

`3`

`var2`

`3`

...so on and so forth...

In the end, I'd like to the final output to be a 11(r) x 2(c) matrix (or data frame or list) containing the sums for each var.

`var1 var2`

6 3

3 3

...

...

Note: 11(r) is because the length of

`myseq`

`var1`

`var2`

Answer

Something like this ?

```
res<-t(sapply(myseq,function(x){apply(mydf[scores>x,2:3],2,sum)}))
```