Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
372 views
in Technique[技术] by (71.8m points)

r - How do I use plyr to number rows?

Basically I want an autoincremented id column based on my cohorts - in this case .(kmer, cvCut)

    > myDataFrame
       size kmer cvCut   cumsum
1      8132   23    10     8132
10000   778   23    10 13789274
30000   324   23    10 23658740
50000   182   23    10 28534840
100000   65   23    10 33943283
200000   25   23    10 37954383
250000  584   23    12 16546507
300000  110   23    12 29435303
400000   28   23    12 34697860
600000  127   23     2 47124443
600001  127   23     2 47124570

I want a column added that has new row names based on the kmer/cvCut group

    > myDataFrame
       size kmer cvCut   cumsum  newID
1      8132   23    10     8132      1
10000   778   23    10 13789274      2
30000   324   23    10 23658740      3
50000   182   23    10 28534840      4
100000   65   23    10 33943283      5 
200000   25   23    10 37954383      6
250000  584   23    12 16546507      1
300000  110   23    12 29435303      2
400000   28   23    12 34697860      3
600000  127   23     2 47124443      1
600001  127   23     2 47124570      2
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'd do it like this:

library(plyr)
ddply(df, c("kmer", "cvCut"), transform, newID = seq_along(kmer))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...