counting occurrences in column and create variable in R

后端 未结 3 782
执念已碎
执念已碎 2020-12-28 10:20

I am new on R and I have a data.frame , called \"CT\", containing a column called \"ID\" containing several hundreds of different identification numbers (these are patients)

3条回答
  •  爱一瞬间的悲伤
    2020-12-28 11:00

    data.table usually provides the quickest way

    set.seed(3)
    library(data.table)
    ct <- data.table(id=sample(1:10,15,replace=TRUE),item=round(rnorm(15),3))
    st <- ct[,countid:=.N,by=id]
     id   item countid
     1:  2  0.953       2
     2:  9  0.535       2
     3:  4 -0.584       2
     4:  4 -2.161       2
     5:  7 -1.320       3
     6:  7  0.810       3
     7:  2  1.342       2
     8:  3  0.693       1
     9:  6 -0.323       5
    10:  7 -0.117       3
    11:  6 -0.423       5
    12:  6 -0.835       5
    13:  6 -0.815       5
    14:  6  0.794       5
    15:  9  0.178       2
    

提交回复
热议问题