My dataframe looks like this
library(dplyr)
N = 10
a <- c("a", "b", "h", "d", "e", "a", "c", "b", "d", "f")
b <- c("a", "b", "h", "d", "e", "z", "d", "g", "h", "q")
z <- rnorm(N)
df1 <- data.frame(a, b, z)
df2 <- data.frame(a, b, z)
df1 <- df1 %>% mutate(year = 2012)
df2 <- df2 %>% mutate(year = 2013)
I'm trying to create unique ID for each observation using this code:
dfs <- c("df1", "df2")
for (i in dfs){
assign(i, get(i) %>%
group_by(a, b, z) %>%
mutate(id = cur_group_id()))
}
However, I find that even though the the values for a and b are the same across df1 and df2, they have a different id. Ideally, the id for the first 5 observations for df1 and df2 should be the same. Is there a way to make sure that observations with the values for a and b have the same id across the dataframes?