简体   繁体   中英

R: how to create a new column in a dataframe where is cardinally counted how many times an observation has the same value for a variable

I have a R dataframe of more than 15,000 rows like the following one:

+------------------------------------+-------+
|  Authors                           | IDs   |
+------------------------------------+-------+
|  Abad J., Cabrera H.R., Medina A.  | 16400 |
|  Abad J., Cabrera H.R., Medina A.  | 70058 |
|  Abad J., Cabrera H.R., Medina A.  | 71030 |
|  A Banuls V., Salmeron J.L.        | 57196 |
|  A Banuls V., Salmeron J.L.        | 56372 |
+------------------------------------+-------+

What i want to obtain is the following new column:

+------------------------------------+-------+-------+
|  Authors                           | IDs   |Order  |
+------------------------------------+-------+-------+
|  Abad J., Cabrera H.R., Medina A.  | 16400 |   1   |
|  Abad J., Cabrera H.R., Medina A.  | 70058 |   2   |
|  Abad J., Cabrera H.R., Medina A.  | 71030 |   3   |
|  A Banuls V., Salmeron J.L.        | 57196 |   1   | 
|  A Banuls V., Salmeron J.L.        | 56372 |   2   |
+------------------------------------+-------+-------+

Basically i want a new column where is counted the number of observations that have the same Authors.

Any guess ?

We can do a group by 'Authors' and get the row_number()

library(dplyr)
df1 %>%
  group_by(Authors)%>%
  mutate(order = row_number())

Or with ave

df1$order <- with(df1, ave(seq_along(Authors), Authors, FUN = seq_along))

Or if the 'Authors' are arranged alphabetically

df1$order <- sequence(table(df1$Authors))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM