1. Query
我正在使用sas
个数据集(SAS数据集包含可以读取的labelled::get_variable_labels(df)
个标签),并try 创建gt
个表,其中column name和labels都可以位于数据的顶部.但我不确定如何做到这一点,也无法在网上找到任何相关的东西.
默认情况下,gt
使用lables
作为数据头.
数据
df %>% head()
# A tibble: 6 × 357
SPOUSARR SPOUSE NRESPOUS HHSPOUSE SPOUSEFW SPOUSENF HHKID
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 NA 0 NA 0 NA NA 0
2 NA 0 NA 0 NA NA 0
3 NA 1 3 0 NA NA 0
4 NA 0 NA 0 NA NA 6
5 NA 1 0 1 NA NA 2
6 NA 1 3 0 NA NA 4
below code gives me the label names in the 数据set:
labelled::get_variable_labels(df)
$SPOUSARR
[1] "YEAR SPOUSE ARRIVAL IN US"
$SPOUSE
[1] "SPOUSE ON FAMILY GRID"
$NRESPOUS
[1] "NON HH RESIDENT SPOUSE"
$HHSPOUSE
[1] "SPOUSE IN HH"
2. Desired output: Structure of table that I am trying to achieve is like below:
3. Sample 数据 Source used in this post is downloaded from this 数据 link government source https://www.dol.gov/agencies/eta/national-agricultural-workers-survey/数据/files-sas.
4. packages & code used:个
library(tidyverse)
library(haven)
library(gt)
library(labelled)
# after download & unzipping 15mb file
df <- haven::read_sas("naws_all.sas7bdat")
# to view table
df %>% head() %>% gt()
# to view lables
labelled::get_variable_labels(df)
如果能帮上忙我会感激不尽的.