I do not fully understand if you just want the label values as attributes inside the dataframe, or if you would like to pull the labels out to a new column (which has be answered by @DavoWW).
I would suggest to just load the data using haven::read_dta
and then use dplyr::mutate
(or collapse::ftransform
/collapse::fmutate
) to transform the relevant columns into a factor, using haven::as_factor
. Then, you would have ordinary factors to work with in your data.frame
and if you look at the columns, the labels are shown as entries.
Reprex:
data <- haven::read_dta('2021-PPI-Full.dta')
data |>
dplyr::mutate(country = haven::as_factor(country)) |>
head()
#> # A tibble: 6 × 59
#> ID IY country Region income IDA FCY FCM type stype
#> <dbl> <dbl> <fct> <dbl+lbl> <dbl+l> <dbl+l> <dbl> <dbl+lb> <dbl+l> <dbl+l>
#> 1 1334 1990 Argentina 4 [LAC] 3 [Upp… 0 [Non… 1990 9 [Sep… 1 [Bro… 5 [Reh…
#> 2 1337 1990 Argentina 4 [LAC] 3 [Upp… 0 [Non… 1990 9 [Sep… 1 [Bro… 5 [Reh…
#> 3 1330 1990 Argentina 4 [LAC] 3 [Upp… 0 [Non… 1990 9 [Sep… 1 [Bro… 5 [Reh…
#> 4 1346 1990 Mexico 4 [LAC] 3 [Upp… 0 [Non… 1990 3 [Mar… 3 [Gre… 8 [Bui…
#> 5 991 1990 Mexico 4 [LAC] 3 [Upp… 0 [Non… 1990 11 [Nov… 2 [Div… 6 [Ful…
#> 6 1462 1990 India 6 [SAR] 2 [Low… 0 [Non… 1990 6 [Jun… 1 [Bro… 3 [Bui…
#> # … with 49 more variables: status_n <dbl+lbl>, sector <dbl+lbl>,
#> # ssector <dbl+lbl>, Segment <dbl+lbl>, period <dbl>, GGC <dbl+lbl>,
#> # VDGS <dbl>, TIGS <chr>, VIGS <dbl>, private <dbl>, fees <dbl>,
#> # physical <dbl>, investment <dbl>, capacity <dbl+lbl>, pcapacity <dbl>,
#> # technol <dbl+lbl>, bid_crit <dbl+lbl>, CAM <dbl+lbl>, numberb <dbl>,
#> # CommentsDomIntl <chr>, PRS <dbl+lbl>, OSR <dbl+lbl>, Description <chr>,
#> # FundingYear <dbl>, debt <dbl>, equity <chr>, c_debt <dbl>, m_debt <dbl>, …
Created on 2022-08-18 by the reprex package (v2.0.1)
Maybe this helps.