Count unique values in R and display in column

房东的猫 提交于 2019-12-11 07:36:04

问题


I have this dataframe, and I would like to count the unique values in column A and display them in Column D

So the if else function should look at column A and ad 1 for each new unique user

> DF_Have <- data.frame(A=c(1,2,2,3,3), B=1:5*10, C=1:5*100)
> DF_Have
   A  B   C
1: 1 10 100
2: 2 20 200
3: 2 30 300
4: 3 40 400
5: 3 50 500


> DF_Want
   A  B   C   D
1: 1 10 100   1
2: 2 20 200   2
3: 2 30 300   2
4: 3 40 400   3
5: 3 50 500   3

回答1:


library(data.table)
DF_Have$D <- rleid(DF_Have$A)
DF_Have
#  A  B   C D
#1 1 10 100 1
#2 2 20 200 2
#3 2 30 300 2
#4 3 40 400 3
#5 3 50 500 3

another option without need of any external package is( provided DF_Have$A is ordered)

DF_Have$D <- cumsum(!duplicated(DF_Have$A))



回答2:


Perhaps this helps

library(data.table)
setDT(DF_Have)[, D:= rleid(A)]
DF_Have
#   A  B   C D
#1: 1 10 100 1
#2: 2 20 200 2
#3: 2 30 300 2
#4: 3 40 400 3
#5: 3 50 500 3

Or using dplyr

library(dplyr)
DF_Have %>%
     mutate(D = cumsum(c(TRUE, A[-1]!= A[-length(A)])))



回答3:


We can use base R match

DF_Have$D <- match(DF_Have$A, unique(DF_Have$A))
DF_Have
#  A  B   C D
#1 1 10 100 1
#2 2 20 200 2
#3 2 30 300 2
#4 3 40 400 3
#5 3 50 500 3


来源:https://stackoverflow.com/questions/41850966/count-unique-values-in-r-and-display-in-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!