我有一个看起来像这样的数据框
1
我想找到第一次和最后一次a
时每行的列索引
例如:对于行2,10
,它是void array_init(void)
{
arr_1[0] = add1;
arr_1[1] = sub1;
}
答案 0 :(得分:3)
你可以这样做:
x <- df==1
rbind(max.col(x, "first"), max.col(x, "last"))
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#[1,] 2 1 1 1 2 3 3 2 2 4
#[2,] 2 6 6 4 4 5 6 6 2 6
或apply
:
apply(df, 1, function(x) c(min(which(x==1)),max(which(x==1))))
数据强>
df <- structure(list(a = c(0, 1, 1, 1, 0, 0, 0, 0, 0, 0), b = c(1,
0, 0, 0, 1, 0, 0, 1, 1, 0), c = c(0, 1, 0, 1, 0, 1, 1, 1, 0,
0), d = c(0, 1, 0, 1, 1, 0, 1, 0, 0, 1), e = c(0, 0, 0, 0, 0,
1, 0, 1, 0, 1), f = c(0, 1, 1, 0, 0, 0, 1, 1, 0, 1)), .Names = c("a",
"b", "c", "d", "e", "f"), row.names = c(NA, -10L), class = "data.frame")