gdemin / expss

expss: Tables and Labels in R
https://cran.r-project.org/web/packages/expss/
84 stars 16 forks source link

Help with val_lab() and var_lab() #103

Closed robertogilsaura closed 1 year ago

robertogilsaura commented 1 year ago

Hello @gdemin. Hope you are well. Long time no contact.

I hope that You can help me, beacuse I need a function or script that I suppose for you is very simple, but for me it is doing it very complicated. I have a JSON data source with three tables, the data in a JSON node (variable, value), text of variables in another JSON node (variable, text) and other with the values of variables (variable, value , text). I have read the information and it is stored in three dataframes. It would be wonderful if I finish import data with this function and data with var_lab() and val_lab()

I would like to be able to impute to each variable its var_lab() and its val_lab() automatically in a function if possible, but I don't see the best way to do it. I read, num_lab, set_val_lab() etc... but I don't find the solution ...

A short case is this ...

library(expss)
data <- data.frame(gender=c(1,1,1,2), age=c(1,2,5,4))
vars <- data.frame(nomvar=c('gender', 'age'), 
                    txtext=c('DC: Gender', 'DC: Age'))
codes <- data.frame(nomvar=c('gender','gender','age','age','age','age'),
        cod=c(1,2,1,2,3,4), 
        txt=c('male','female', '25-35', '36-45', '46-55', '56-65'))

How can I get var_lab() and respective val_lab() for each variable in data? Any suggestion for doing?

Thanks a lot in advance ...

robertogilsaura commented 1 year ago

Hi @gdemin ; I got a solution. I had a misinterpretation of how make_labels() worked. I don't know if it's the best solution, but it produces a good ouput for me.

library(jsonlite)
library(httr)
library(expss)

URLRECORD <- paste("https://online2.tesintegra.net/html/integra_web_service/integra.php?apikey=U3hlSnRGWENrL0Jid29kZ3B5ZzlLQT09",
                   "&op=getSurveyData",
                   "&pid=TS23000021&",
                   "rawdata=1",
                   sep = '')
headers=c()
res <- VERB("GET", 
    url = URLRECORD , 
    add_headers(headers)
    )
res <- content(res, 'text')
json <- fromJSON(res, simplifyDataFrame = TRUE)
data <- json$data
vars <- json$structure
cods <- json$cods
remove(URLRECORD)
remove(headers)
remove(res)
remove(json)

coding = function(variable) {
    tmpcod <- cods %>% rows(nomvar==variable) %>% columns(2:3)
    write.table(tmpcod, file='tmpcod', row.names = F, quote=F, col.names=F)
    tmpcod1 <- readLines(file('tmpcod'))
    close(con=file('tmpcod'))
    labs <- make_labels(tmpcod1)
    val_lab(data[[variable]]) <<- labs
    remove(tmpcod)
    remove(tmpcod1)
    remove(labs)
}

Sharing code ...

Thanks a lot for the package and your help!!!

gdemin commented 1 year ago

Hi, @robertogilsaura

apply_labels recently became to support list argument with all meta. You can use this feature:

library(expss)
data <- data.frame(gender=c(1,1,1,2), age=c(1,2,5,4))
vars <- data.frame(nomvar=c('gender', 'age'), 
                   txtext=c('DC: Gender', 'DC: Age'))
codes <- data.frame(nomvar=c('gender','gender','age','age','age','age'),
                    cod=c(1,2,1,2,3,4), 
                    txt=c('male','female', '25-35', '36-45', '46-55', '56-65'))

# make named list - names are variable names, values are variable labels
var_list = as.list(vars$txtext) %>% 
    setNames(vars$nomvar)

# make named list with value labels
val_list = codes %>% 
    split(codes$nomvar) %>% 
    lapply(function(x) setNames(x$cod, x$txt))

# apply all labels
data = data %>% 
    apply_labels(
        c(var_list, val_list)
    )
robertogilsaura commented 1 year ago

Great !!!

I'll apply your solution. Code cleaner and simplier than mine.

Thanks a lot @gdemin.

El vie, 10 feb 2023 a las 22:57, Gregory Demin @.***>) escribió:

Hi, @robertogilsaura https://github.com/robertogilsaura

apply_labels recently became to support list argument with all meta. You can use this feature:

library(expss)data <- data.frame(gender=c(1,1,1,2), age=c(1,2,5,4))vars <- data.frame(nomvar=c('gender', 'age'), txtext=c('DC: Gender', 'DC: Age'))codes <- data.frame(nomvar=c('gender','gender','age','age','age','age'), cod=c(1,2,1,2,3,4), txt=c('male','female', '25-35', '36-45', '46-55', '56-65'))

make named list - names are variable names, values are variable labelsvar_list = as.list(vars$txtext) %>%

setNames(vars$nomvar)

make named list with value labelsval_list = codes %>%

split(codes$nomvar) %>%
lapply(function(x) setNames(x$cod, x$txt))

apply all labelsdata = data %>%

apply_labels(
    c(var_list, val_list)
)

— Reply to this email directly, view it on GitHub https://github.com/gdemin/expss/issues/103#issuecomment-1426393176, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACWGQPZP2VK5G7ZX4AEJYFTWW22WLANCNFSM6AAAAAAUWWU6TQ . You are receiving this because you were mentioned.Message ID: @.***>