Nathan123
Nathan123

Reputation: 763

how to create a column in a dataframe based on the order of another column?

I have the following data.frame

casenum<-c('510-1','510-2','510-2','510-3','510-3','510-3','510-4')
chargecode<-c('AA','BB','DD','FF','GG','DD','ZZ')

df<-data.frame(casenum,chargecode)

  casenum chargecode
1   510-1         AA
2   510-2         BB
3   510-2         DD
4   510-3         FF
5   510-3         GG
6   510-3         DD
7   510-4         ZZ

Basically I want to create a third column called count that indicates if the charge code associated with a case is the first count, the second count or the third count. so that it looks like the following

  casenum chargecode count
1   510-1         AA   1
2   510-2         BB   1
3   510-2         DD   2
4   510-3         FF   1
5   510-3         GG   2
6   510-3         DD   3
7   510-4         ZZ   1

I used the following code

library(dplyr)
df%>%group_by(casenum,chargecode)%>%
mutate(count = as.integer(factor(chargecode, levels = unique(chargecode))))

how ever it doesn't provide the desired result.

Upvotes: 1

Views: 52

Answers (1)

dipetkov
dipetkov

Reputation: 3700

One way to do this using dplyr:

library("dplyr")
df %>% 
  group_by(casenum) %>% 
  # Add row number within a group, i.e., the order of occurrence
  mutate(count = row_number()) %>% 
  ungroup()

Upvotes: 1

Related Questions