【发布时间】:2022-12-02 01:41:38
【问题描述】:
I have a data frame with 4 columns and for each row, I want to extract 2 of the 4 columns (but for each row, it's going to be different columns).
repro = structure(list(c1 = c(0L, 0L, 1L, 1L, 0L, 1L), c2 = c(1L, 1L,
0L, 0L, 1L, 1L), c1 = c(0L, 1L, 1L, 0L, 1L, 0L), c2 = c(0L, 1L,
1L, 1L, 1L, 0L)), row.names = c(86L, 59L, 58L, 79L, 70L, 83L),
class = "data.frame")
head(repro)
c1 c2 c1 c2
86 0 1 0 0
59 0 1 1 1
58 1 0 1 1
79 1 0 0 1
70 0 1 1 1
83 1 1 0 0
Vectors of columns to select in the repro data frame
col.sel1 = c(2, 1, 2, 2, 2, 2)
col.sel2 = c(4, 3, 3, 4, 3, 3)
For loop to select the columns (it works, but for my original data, it takes for ever as there are thousands of lines...).
# Make offspring table
offspring = NULL
for (i in 1:nrow(repro)) {
offs = cbind(c3 = repro[i,col.sel1[i]],
c4 = repro[i,col.sel2[i]])
offspring = rbind(offspring,offs)
}
head(offspring)
Giving
c3 c4
[1,] 1 0
[2,] 0 1
[3,] 0 1
[4,] 0 1
[5,] 1 1
[6,] 1 0
Is there a faster way to select different columns for each rows based on the 2 vectors
col.sel1 and col.sel2?
I've tried:
rp[1:6, cs1]
lapply(cs1, function(x) rp[,x])
But both don't give this expected result.
【问题讨论】: