Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个这样的data.frame:
a b 1 1 2 2 1 3 3 2 3 4 2 5
它是排序的a,我需要不同的索引a。现在我正在使用 for 循环,但它并不优雅。
a
尝试以下操作:
# this will give you the row indices lapply(unique(dat$a), function(a) which(dat$a==a))
如果要命名结果,请使用:
U <- unique(dat$a) names(U) <- U lapply(U, function(a) which(dat$a==a)) # Produces: # $`1` # [1] 1 2 # # $`2` # [1] 3 4