I have a data dictionary called data_dict of this format with hundreds of rows:
| Field Name | Field Label |
|---|---|
| marital_status | What is your marital status? |
| birthplace | What country were you born? |
I have another dataframe called df in this format with hundreds of rows:
| record_id | marital_status | birthplace |
|---|---|---|
| 1 | 3 | 66 |
| 2 | 6 | 12 |
I am currently using df %>% map(~ table(.x, useNA = "ifany")) to summarize the results for all the columns in df. I want the Field Label column values from data_dict to appear instead of the column names from df. How could that be done without changing the column names in df?
CodePudding user response:
We may use rename
library(dplyr)
library(tibble)
df %>%
summarise(across(all_of(data_dict$"Field Name"),
~ list(table(.x, useNA = "ifany")))) %>%
rename(!!! deframe(data_dict[2:1]))
Or using map
library(purrr)
df %>%
rename(!!! deframe(data_dict[2:1])) %>%
map(~ table(.x, useNA = "ifany"))
-output
$record_id
.x
1 2
1 1
$`What is your marital status?`
.x
3 6
1 1
$`What country were you born?`
.x
12 66
1 1
data
df <- structure(list(record_id = 1:2, marital_status = c(3L, 6L), birthplace = c(66L,
12L)), class = "data.frame", row.names = c(NA, -2L))
data_dict <- structure(list(`Field Name` = c("marital_status", "birthplace"
), `Field Label` = c("What is your marital status?", "What country were you born?"
)), class = "data.frame", row.names = c(NA, -2L))
