gdemin / expss

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

var_lab in net #95

Closed robertogilsaura closed 2 years ago

robertogilsaura commented 2 years ago

Hi @gdemin. I have a new question about coding with net (or subtotal).

In order to repeat by week the same structure in a monadic product test, I have several (a lot of) tables like this ...

library(expss)
x1 <- c(1,2,3,4,5,6,7,8,9)
x2 <- c(1,2,3,9,5,9,7,8,9)
x3 <- c(8,8,8,8,8,8,7,8,9)

df <- data.frame(x1,x2,x3)

var_lab(df$x1) <- 'text 1'
var_lab(df$x2) <- 'text 2'
var_lab(df$x3) <- 'text 3'

df %>% 
    tab_cols(total()) %>% 
    tab_cells(
         '|'=unvr(net(x1, var_lab(df$x1)=8:9, 'Rest'=1:7))
        ,'|'=unvr(net(x2, var_lab(df$x2)=8:9, 'Rest'=1:7))
        ,'|'=unvr(net(x3, var_lab(df$x3)=8:9, 'Rest'=1:7))
    ) %>% 
    tab_stat_cpct() %>% 
    tab_pivot() %>% 
        rows(!grepl("Rest", row_labels)) %>% 
        as.etable()

Variable label changes weekly, and I would remain the same code.. If I run this code, there is an error

Error in check_class_for_stat(data) : argument "data" is missing, with no default

Error is in the assignment var_lab. If I substitute var_lab for 'text', table runs properly.

Is there anyway to do the var_lab() assignment dynamically?

Thanks in advance.

Regards.

gdemin commented 2 years ago

Hi, @robertogilsaura First of all, you can't use a vector as a variable label. 8:9 are two values and a variable label should be only one value. As for your main question, you can use set_var_lab(x1, "label") in any place.

robertogilsaura commented 2 years ago

Thank you @gdemin. I have understood the answer and why the error occurs.

I thought that doing a var_lab() assignment, I was like assigning text to value, but it's true that 8:9 are two seq values.

I will work the etable object to get what I need (str_replace())

Thanks Gregory; as always, quick response...

PD: Thanks for maditr updating with respect etable class!