Work with Links Using Rank (R)

I am trying to create a dummy variable to see if the baby is born, and the other if the baby is born second. My data looks something like this.

ID   MID   CMOB   CYRB      
1    1     1      1991
2    1     7      1989
3    2     1      1985
4    2     11     1985
5    2     9      1994
6    3     4      1992
7    4     2      1992
8    4     10     1983

With ID = baby identifier, MID = mother identifier, CMOB = month of birth, and CYRB = year of birth.

For the first mannequin born, I tried using this:

Identifiers_age <- Identifiers_age %>% group_by(MPUBID) 
                          %>% mutate(first = as.numeric(rank(CYRB) == 1))

But there seems to be no way to break ties of the rank of another column (obviously, in this case the desired column is CMOB), whenever I try to use the "ties.method" argument, it tells me the input should be a character vector.

Did I miss something?

+4
source share
2 answers

order , ?order:

, , .

Identifiers_age <- Identifiers_age %>% group_by(MID) %>% 
                   mutate(first = as.numeric(order(CYRB, CMOB) == 1))
Identifiers_age

#Source: local data frame [8 x 5]
#Groups: MID [4]

#     ID   MID  CMOB  CYRB first
#  <int> <int> <int> <int> <dbl>
#1     1     1     1  1991     0
#2     2     1     7  1989     1
#3     3     2     1  1985     1
#4     4     2    11  1985     0
#5     5     2     9  1994     0
#6     6     3     4  1992     1
#7     7     4     2  1992     0
#8     8     4    10  1983     1
+4

rank, 'CYRB', 'CMOB' 'Date', rank

Identifiers_age %>%
         group_by(MID) %>% 
         mutate(first = as.integer(rank(as.Date(paste(CYRB, CMOB, 1,
                  sep="-"), "%Y-%m-%d"))==1))
#     ID   MID  CMOB  CYRB first
#  <int> <int> <int> <int> <int>
#1     1     1     1  1991     0
#2     2     1     7  1989     1
#3     3     2     1  1985     1
#4     4     2    11  1985     0
#5     5     2     9  1994     0
#6     6     3     4  1992     1
#7     7     4     2  1992     0
#8     8     4    10  1983     1

, rank

Identifiers_age %>% 
         group_by(MID) %>%
         mutate(first = as.integer(rank(CYRB + CMOB/12)==1))
#     ID   MID  CMOB  CYRB first
#   <int> <int> <int> <int> <int>
#1     1     1     1  1991     0
#2     2     1     7  1989     1
#3     3     2     1  1985     1
#4     4     2    11  1985     0
#5     5     2     9  1994     0
#6     6     3     4  1992     1
#7     7     4     2  1992     0
#8     8     4    10  1983     1
+1

Source: https://habr.com/ru/post/1650406/


All Articles