order matrix based on another matrix - r

If i have
ex1 <-
structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 1L, 1L,
1L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 0L,
0L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L,
1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L), .Dim = c(10L, 12L
), .Dimnames = list(c("q1", "q2", "q3", "q4", "q5", "q6", "q7",
"q8", "q9", "q10"), c("q1", "q2", "q3", "q4", "q5", "q6", "q7",
"q8", "q9", "q10", "q11", "q12")))
and
ex2 <-
structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 1L, 1L,
1L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 0L,
0L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L,
1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L), .Dim = c(10L, 12L
), .Dimnames = list(c("q4", "q7", "q10", "q9", "q2", "q1", "q6",
"q3", "q5", "q8"), c("q12", "q9", "q10", "q6", "q5", "q7", "q4",
"q1", "q11", "q2", "q3", "q8")))
How can I make the row and column order of ex2 match ex1 and vice versa .
I tried methods in this post but to no avail.

Just use the rownames and colnames to subset. [ is pretty flexible:
ex2[rownames(ex1),colnames(ex1)]
q1 q2 q3 q4 q5 q6 q7 q8 q9 q10 q11 q12
q1 1 0 0 1 1 1 1 0 1 1 1 1
q2 0 1 0 1 1 1 1 0 1 1 0 1
q3 1 1 1 1 1 1 1 0 1 1 0 1
q4 0 0 0 1 1 0 0 0 0 0 0 1
q5 1 1 1 1 1 1 1 0 1 1 1 1
q6 1 0 0 1 1 1 1 0 1 1 1 1
q7 1 0 0 0 0 0 1 0 1 1 0 1
q8 1 1 1 1 1 1 1 1 1 1 1 1
q9 0 0 1 0 1 1 1 0 1 1 1 1
q10 1 0 0 0 0 1 0 1 1 0 1 1
ex1[rownames(ex2),colnames(ex2)]
q12 q9 q10 q6 q5 q7 q4 q1 q11 q2 q3 q8
q4 0 1 0 1 1 0 1 1 1 1 1 0
q7 0 1 0 1 1 1 1 1 0 1 1 1
q10 1 1 1 1 1 1 1 1 1 1 1 1
q9 0 1 1 1 1 1 1 1 1 1 1 1
q2 0 0 0 1 0 0 0 1 0 1 1 1
q1 0 0 0 0 1 1 0 1 0 0 0 0
q6 0 1 0 1 1 1 1 1 0 1 1 1
q3 1 1 0 0 0 0 1 1 0 1 0 1
q5 0 0 1 1 1 1 1 1 0 1 1 0
q8 0 0 1 1 1 1 1 1 1 1 1 1

Related

Calculating co-voting within and between groups

I have a square matrix with information on the co-voting behavior between individuals (15x15 in toy example below). The rows and columns of the matrix are arranged according to the groups the individuals belong to (A, B or C). The entries indicate whether or not two individuals voted the same way 50% of the time (possible entries: 1, 0, NaN).
I need to calculate the rate/fraction of co-voting within and between groups. The resulting matrix in the toy example should be a 3x3 matrix with A, B, C on the rows and columns and values ranging from 0 to 1. How can I do this using for loops?
A A A A A B B B B B C C C C C
A 1 1 1 1 1 1 0 0 0 0 0 1 1 1 0
A 1 1 1 1 1 1 0 0 0 0 0 1 1 1 0
A 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0
A 1 1 1 1 1 1 0 0 0 0 0 1 1 1 0
A 1 1 1 1 1 1 0 0 0 0 0 1 1 1 0
B 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1
B 0 0 0 0 0 1 1 1 1 1 1 0 0 1 1
B 0 0 0 0 0 1 1 1 1 1 1 1 0 1 1
B 0 0 0 0 0 0 1 1 1 1 1 0 0 0 1
B 0 0 0 0 0 1 1 1 1 1 1 0 0 1 1
C 0 0 1 0 0 1 1 1 1 1 1 1 0 0 1
C 1 1 1 1 1 1 0 1 0 0 1 1 1 0 1
C 1 1 1 1 1 1 0 0 0 0 0 1 1 1 0
C 1 1 1 1 1 1 1 1 0 1 0 0 1 1 0
C 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1
If your matrix is called m, you could do
groups <- unique(colnames(m))
res <- matrix(0, 3, 3, dimnames = list(groups, groups))
for(i in groups) {
for(j in groups) {
mat <- m[rownames(m) %in% i, colnames(m) %in% j]
res[rownames(res) %in% i, colnames(res) %in% j] <- sum(mat) / length(mat)
}
}
res
#> A B C
#> A 1.00 0.20 0.64
#> B 0.20 0.92 0.68
#> C 0.64 0.68 0.60
Created on 2022-06-02 by the reprex package (v2.0.1)
Data taken from question in reproducible format
m <- structure(c(1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L,
1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L,
0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L,
1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 1L,
1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L,
0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L,
0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L,
0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L,
1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L,
0L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L,
0L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L,
0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 0L, 0L, 1L), dim = c(15L, 15L), dimnames = list(c("A", "A",
"A", "A", "A", "B", "B", "B", "B", "B", "C", "C", "C", "C", "C"
), c("A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "C", "C",
"C", "C", "C")))

How do I convert this adjacency matrix into a graph object?

I have a matrix that represents social interaction data on a CSV, which looks like below:
`0` `1` `2` `3` `4` `5` `6` `7` `8` `9`
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
0 0 29 1 0 1 9 3 0 1 4
1 1 0 0 1 3 1 0 1 1 1
2 1 1 0 13 4 0 1 1 15 0
3 3 0 1 0 1 1 7 1 1 1
4 1 0 1 98 0 1 1 1 1 2
5 2 5 1 1 3 0 2 0 1 5
6 1 1 0 0 12 1 0 2 1 1
7 1 1 0 1 0 1 9 0 1 2
8 1 1 17 13 145 1 39 1 0 1
9 88 23 1 5 1 2 1 7 1 0
I am new to social network analysis, so I am not sure of my terminology, but this seems like a weighted adjacency matrix to me, as we can say from this that student 1 has had 29 interactions with student 0 in the last year. I had this object stored as a data-frame in my RStudio, but when I ran the following code, I received the below error:
> fn <- graph_from_adjacency_matrix(output, weighted = T)
Error in mde(x) : 'list' object cannot be coerced to type 'double'
I've tried converting it to matrix, but that does not seem to work either. Any help concerning this would be really appreciated.
You need to convert your data.frame to matrix first and then apply graph_from_adjacency_matrix, e.g.,
g <- graph_from_adjacency_matrix(as.matrix(df),weighted = TRUE)
and plot(g) gives
Data
> dput(df)
structure(list(``0`` = c(0L, 1L, 1L, 3L, 1L, 2L, 1L, 1L, 1L,
88L), ``1`` = c(29L, 0L, 1L, 0L, 0L, 5L, 1L, 1L, 1L, 23L), ``2`` = c(1L,
0L, 0L, 1L, 1L, 1L, 0L, 0L, 17L, 1L), ``3`` = c(0L, 1L, 13L,
0L, 98L, 1L, 0L, 1L, 13L, 5L), ``4`` = c(1L, 3L, 4L, 1L, 0L,
3L, 12L, 0L, 145L, 1L), ``5`` = c(9L, 1L, 0L, 1L, 1L, 0L, 1L,
1L, 1L, 2L), ``6`` = c(3L, 0L, 1L, 7L, 1L, 2L, 0L, 9L, 39L, 1L
), ``7`` = c(0L, 1L, 1L, 1L, 1L, 0L, 2L, 0L, 1L, 7L), ``8`` = c(1L,
1L, 15L, 1L, 1L, 1L, 1L, 1L, 0L, 1L), ``9`` = c(4L, 1L, 0L, 1L,
2L, 5L, 1L, 2L, 1L, 0L)), class = "data.frame", row.names = c("0",
"1", "2", "3", "4", "5", "6", "7", "8", "9"))

How to compute on columns pairwise, from two groups with dplyr

I have a dataset of this shape.
group a1 a2 ... a9 b1 b2 ... b7
1 1 0 ... 1 0 1 ... 1
1 1 1 ... 1 0 0 ... 1
1 0 0 ... 0 1 0 ... 1
1 1 1 ... 0 1 1 ... 0
2 1 0 ... 1 0 1 ... 1
2 1 1 ... 1 0 0 ... 1
2 0 0 ... 0 1 0 ... 1
2 1 1 ... 0 1 1 ... 0
...
and what I'd like to do is apply a two-argument summary function to all pairs of columns, maintaining the grouped nature of the data.
So, for example
f = function(a, b) { mean(a) + mean(b) + mean(a & b) }
would return something like (I'm not actually going to compute the value of the function, I'll just put "x" to indicate where the stat would go, but of course it would be different for each group-a-b combination).
group a_col b_col stat
1 a1 b1 x
1 a1 b2 x
1 a1 b3 x
...
1 a9 b7 x
2 a1 b1 x
...
A commenter asked for some sample data. Here it is:
structure(list(group = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L,
7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 10L, 10L), a1 = c(0L,
1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L,
1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L,
1L, 0L, 0L, 0L), a2 = c(0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 1L,
0L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 1L,
0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 1L), a3 = c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 0L,
1L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 1L,
0L, 0L), a4 = c(0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 1L,
1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L,
0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L), a5 = c(1L, 0L, 0L, 0L, 0L,
0L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 0L,
0L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 0L
), b1 = c(1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L,
0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 0L,
0L, 1L, 0L, 1L, 1L, 0L, 0L, 0L), b2 = c(0L, 0L, 1L, 0L, 0L, 0L,
1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L,
1L, 0L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 0L),
b3 = c(0L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 0L,
1L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 1L,
1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), class = "data.frame", row.names = c(NA,
-37L))
A solution using tidyverse. We can gather the columns based on the starting letters twice and then conduct the operation. Assuming your data is called dat, dat2 is the final output.
library(tidyverse)
dat2 <- dat %>%
gather(column_a, value_a, starts_with("a")) %>%
gather(column_b, value_b, starts_with("b")) %>%
group_by(group, column_a, column_b) %>%
summarise(stat = mean(value_a) + mean(value_b) + mean(value_a + value_b)) %>%
ungroup()
dat2
# # A tibble: 150 x 4
# group column_a column_b stat
# <int> <chr> <chr> <dbl>
# 1 1 a1 b1 3
# 2 1 a1 b2 2
# 3 1 a1 b3 2
# 4 1 a2 b1 2
# 5 1 a2 b2 1
# 6 1 a2 b3 1
# 7 1 a3 b1 3.5
# 8 1 a3 b2 2.5
# 9 1 a3 b3 2.5
# 10 1 a4 b1 2
# # ... with 140 more rows

convert dataset to longitudinal data structure in R

I have a datset that looks something like this:
> head(BurnData)
Treatment Gender Race Surface head buttock trunk up.leg low.leg resp.tract type ex.time excision antib.time antibiotic
1 0 0 0 15 0 0 1 1 0 0 2 12 0 12 0
2 0 0 1 20 0 0 1 0 0 0 4 9 0 9 0
3 0 0 1 15 0 0 0 1 1 0 2 13 0 13 0
4 0 0 0 20 1 0 1 0 0 0 2 11 1 29 0
5 0 0 1 70 1 1 1 1 0 0 2 28 1 31 0
6 0 0 1 20 1 0 1 0 0 0 4 11 0 11 0
inf.time infection
1 12 0
2 9 0
3 7 1
4 29 0
5 4 1
6 8 1
I want to run a Cox's Regression on variables Surface, ex.time and, antib.time and treatment. Treatment is an indicator variable. Surface denotes the % of body burned. ex.time and antib.time both record time to event in days.
I am aware that to run a time dependent Cox's Regression i need to convert the data in longitudinal structure, but how can i do it in R?
then i will use the forluma:
coxph(formula = Surv(tstart, tstop, infection) ~ covariate)
DATA
> dput(head(BurnData))
structure(list(Treatment = c(0L, 0L, 0L, 0L, 0L, 0L), Gender = c(0L,
0L, 0L, 0L, 0L, 0L), Race = c(0L, 1L, 1L, 0L, 1L, 1L), Surface = c(15L,
20L, 15L, 20L, 70L, 20L), head = c(0L, 0L, 0L, 1L, 1L, 1L), buttock = c(0L,
0L, 0L, 0L, 1L, 0L), trunk = c(1L, 1L, 0L, 1L, 1L, 1L), up.leg = c(1L,
0L, 1L, 0L, 1L, 0L), low.leg = c(0L, 0L, 1L, 0L, 0L, 0L), resp.tract = c(0L,
0L, 0L, 0L, 0L, 0L), type = c(2L, 4L, 2L, 2L, 2L, 4L), ex.time = c(12L,
9L, 13L, 11L, 28L, 11L), excision = c(0L, 0L, 0L, 1L, 1L, 0L),
antib.time = c(12L, 9L, 13L, 29L, 31L, 11L), antibiotic = c(0L,
0L, 0L, 0L, 0L, 0L), inf.time = c(12L, 9L, 7L, 29L, 4L, 8L
), infection = c(0L, 0L, 1L, 0L, 1L, 1L), Surface_discr = structure(c(1L,
1L, 1L, 1L, 2L, 1L), .Label = c("1", "2"), class = "factor"),
ex.time_discr = c(1L, 1L, 1L, 1L, 2L, 1L), antib.time_discr = c(1L,
1L, 1L, 2L, 2L, 1L)), .Names = c("Treatment", "Gender", "Race",
"Surface", "head", "buttock", "trunk", "up.leg", "low.leg", "resp.tract",
"type", "ex.time", "excision", "antib.time", "antibiotic", "inf.time",
"infection", "Surface_discr", "ex.time_discr", "antib.time_discr"
), row.names = c(NA, 6L), class = "data.frame")

Cross tabs - need to aggregate several columns based on certain values in other columns

My data looks like this, all columns with binary presence/absence data:
POP1 POP2 POP3 T1 T2 T3 T4 T5 T6 T7 T8 T9
1 1 0 1 1 1 1 0 1 0 0 1
1 0 1 0 1 1 0 1 1 0 1 1
1 1 0 1 1 1 1 0 0 1 0 1
0 0 0 0 1 1 0 1 0 1 1 0
1 0 1 0 0 1 1 1 0 1 1 0
0 1 0 0 1 1 1 0 0 0 0 1
0 1 0 1 1 0 1 0 0 0 0 0
1 1 1 0 1 0 0 0 1 0 0 0
0 0 0 0 1 1 1 1 1 0 0 1
1 0 0 1 0 1 0 1 0 1 1 1
1 1 0 0 1 0 1 0 0 1 0 0
1 0 1 0 1 1 1 0 1 0 1 0
0 1 0 1 1 1 1 0 0 0 0 0
1 0 0 0 1 1 0 0 0 0 1 1
The POP1:POP3 are populations, and I need counts of all 1's for all T1:T9 for all POP1=1, POP2=1 and POP3=1. I need a table that crosstabulates my data like this:
T1 T2 T3 T4 T5 T6 T7 T8 T9
POP1=1 3 9 7 5 3 4 4 5 5
POP2=1 4 7 8 6 2 3 2 0 3
POP3=1 0 3 4 2 2 2 1 3 1
Don't bother checking the aggregated counts, they're not necessarily correct. I've have tried lots of synthaxes without getting what I want. Thankful for some guidance.
You need the matrix multiplication %*% here:
t(df[1:3]) %*% as.matrix(df[4:12])
T1 T2 T3 T4 T5 T6 T7 T8 T9
POP1 3 7 7 5 3 4 4 5 5
POP2 4 7 4 6 0 2 2 0 3
POP3 0 3 3 2 2 3 1 3 1
df = structure(list(POP1 = c(1L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 1L,
1L, 1L, 0L, 1L), POP2 = c(1L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 0L,
0L, 1L, 0L, 1L, 0L), POP3 = c(0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L,
0L, 0L, 0L, 1L, 0L, 0L), T1 = c(1L, 0L, 1L, 0L, 0L, 0L, 1L, 0L,
0L, 1L, 0L, 0L, 1L, 0L), T2 = c(1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L,
1L, 0L, 1L, 1L, 1L, 1L), T3 = c(1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L,
1L, 1L, 0L, 1L, 1L, 1L), T4 = c(1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L,
1L, 0L, 1L, 1L, 1L, 0L), T5 = c(0L, 1L, 0L, 1L, 1L, 0L, 0L, 0L,
1L, 1L, 0L, 0L, 0L, 0L), T6 = c(1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L,
1L, 0L, 0L, 1L, 0L, 0L), T7 = c(0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L,
0L, 1L, 1L, 0L, 0L, 0L), T8 = c(0L, 1L, 0L, 1L, 1L, 0L, 0L, 0L,
0L, 1L, 0L, 1L, 0L, 1L), T9 = c(1L, 1L, 1L, 0L, 0L, 1L, 0L, 0L,
1L, 1L, 0L, 0L, 0L, 1L)), .Names = c("POP1", "POP2", "POP3",
"T1", "T2", "T3", "T4", "T5", "T6", "T7", "T8", "T9"), class = "data.frame",
row.names = c(NA, -14L))
library(reshape2)
df = melt(df, id.vars = colnames(df)[-(1:3)] )
do.call(rbind, lapply(split(df, df$variable), function(x)
apply(x[x$value == 1,1:9], 2, function(y) sum(y))))
# T1 T2 T3 T4 T5 T6 T7 T8 T9
#POP1 3 7 7 5 3 4 4 5 5
#POP2 4 7 4 6 0 2 2 0 3
#POP3 0 3 3 2 2 3 1 3 1

Resources