I have two data.frames:
df <- data.frame(Cats = c(1,2,3), Dogs = c(1,2,3), Bears = c(1,2,3))
df_lower <- structure(list(cats = c(1, 2, 3), n = c(1, 1, 1), percent = c(0.333333333333333,
0.333333333333333, 0.333333333333333)), class = "data.frame", row.names = c(NA,
-3L), core = structure(list(cats = c(1, 2, 3), n = c(1, 1, 1),
percent = c(0.333333333333333, 0.333333333333333, 0.333333333333333
)), class = "data.frame", row.names = c(NA, -3L)), tabyl_type = "one_way")
df:
Cats Dogs Bears
1 1 1 1
2 2 2 2
3 3 3 3
df_lower:
cats n percent
1 1 1 0.3333333
2 2 1 0.3333333
3 3 1 0.3333333
How can I rename the columns in df_lower by detecting the names that are present in df, and then matching them?
Expected output:
Cats n percent
1 1 1 0.3333333
2 2 1 0.3333333
3 3 1 0.3333333