[R] Help with function and survey data

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Mon Oct 31 17:21:31 CET 2022


1. This is a plain text list. Set your email to post in plain text, not
html, which often gets mangled (see below).

2. I did not run your example, but try:
my_funca(mk =names(dat)[1:9], y = dat$seg_2)

## seg_2 is a component of dat and is not in the environment of the call. I
did not see any data argument that would tell it to look elsewhere, but I
am not familiar with tidy_whatever's nonstandard evaluation conventions.

-- Bert

On Mon, Oct 31, 2022 at 8:39 AM Edjabou Vincent <maklawe using gmail.com> wrote:

> Dear R-Help
> I am working with complex survey data using the survey package.
> I would like to create a function for the generate multi crosstable. The
> problem is that I am getting error with the following message:
> "Error in eval(predvars, data, env) : object 'y' not found"
>
> Here is the example:
> library(tidyverse)
> library(plyr)
> library(survey)
>
> dat <- structure(list(
>    r3a_1 = structure(c(3L, 2L, 3L, 3L, 3L, 3L, 3L,3L, 3L, 3L, 3L, 2L, 2L,
> 3L, 3L, 3L, 3L, 3L, 3L, 3L),
>         .Label = c("Don't Know", "No", "Yes"), class = "factor"),
>   r3a_2 = structure(c(3L, 3L,3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
> 3L, 3L, 3L, 3L, 3L, 3L, 3L),
>                     .Label = c("Don't Know", "No", "Yes"), class =
> "factor"),
>   r3a_3 = structure(c(3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L,
> 2L, 2L, 2L, 2L, 2L, 3L, 3L),
>                     .Label = c("Don't Know","No", "Yes"), class =
> "factor"),
>   r3a_4 = structure(c(3L,2L, 2L, 2L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 3L, 2L,
> 3L, 2L, 2L,3L, 3L, 3L, 1L),
>                     .Label = c("Don't Know", "No", "Yes"), class =
> "factor"),
>   r3a_5 = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 3L, 2L,2L, 3L, 2L,
> 3L, 3L, 2L, 3L, 2L, 3L, 1L),
>                     .Label = c("Don't Know","No", "Yes"), class =
> "factor"),
>   r3a_6 = structure(c(3L,3L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 3L, 3L, 2L,
> 2L, 2L, 3L,2L, 3L, 3L, 3L),
>                     .Label = c("Don't Know", "No", "Yes"), class =
> "factor"),
>   r3a_7 = structure(c(1L, 2L, 2L, 2L, 3L, 2L, 2L, 3L, 3L, 2L,3L, 3L, 2L,
> 2L, 2L, 2L, 2L, 3L, 3L, 3L),
>                     .Label = c("Don't Know", "No", "Yes"), class =
> "factor"),
>   r3a_8 = structure(c(3L,2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 3L, 3L, 2L,
> 3L, 3L, 2L,  2L, 2L, 3L, 3L),
>                     .Label = c("Don't Know", "No", "Yes"), class =
> "factor"),
>   r3a_9 = structure(c(1L, 3L, 2L, 2L, 3L, 2L, 2L, 3L, 3L, 3L,3L, 3L, 2L,
> 2L, 2L, 3L, 2L, 2L, 3L, 3L),
>                     .Label = c("Don't Know","No", "Yes"), class =
> "factor"),
>   weight = c(0.34, 0.34, 0.34,0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34,
> 0.34, 0.34, 0.43, 0.43, 0.43, 0.34, 0.34, 0.34, 0.34, 0.34),
>   seg_2 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L,
> 1L, 1L, 1L,1L, 1L, 1L, 1L),
>                     .Label = c("1", "2"), class = "factor"),
>   seg_3 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L,
> 1L, 1L, 1L,1L, 2L, 2L, 2L),
>                     .Label = c("1", "2"), class = "factor"),
>   seg_4 = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L,
> 1L, 1L, 1L,1L, 1L, 1L, 1L),
>                     .Label = c("1", "2"), class = "factor")),
>   .Names = c("r3a_1", "r3a_2", "r3a_3", "r3a_4", "r3a_5", "r3a_6", "r3a_7",
> "r3a_8", "r3a_9", "weight", "seg_2","seg_3","seg_4"), row.names = c(NA,
> 20L), class = "data.frame")
>
>  dat_weight <- svydesign(ids = ~1, weights = ~weight, data = dat)
>
> my_funca <- function(mk,y){
>    my_re <-   ldply( mk, function(x)
>       svytable(bquote(~ y + .(as.name(x))), dat_weight) %>%
>         as_tibble() %>%
>         dplyr::group_by({{y}}) %>%
>         transmute(!!(as.name(x)), Procent = round(n/sum(n,na.rm =
> T)*100,2)) %>%
>         pivot_wider(names_from = (as.name(x)),
>                     values_from = Procent)
>    )
>    return(my_re)
> }
>
> my_funca(mk =names(dat)[1:9], y = seg_2)
>
>
> I will appreciate any help you will provide.
>
>
>
>
> Regards,
>
> Vincent Edjabou
> Mobile: +45 31 95 99 33
> linkedin.com/vincent
> <http://linkedin.com/in/vincent-maklawe-edjabou-9742a41b>
>
> Orcid: 0000-0003-2849-6151
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list