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