Home > Software engineering >  error (cannot coerce class ‘c("labelled", "call")’ to a data.frame)
error (cannot coerce class ‘c("labelled", "call")’ to a data.frame)

Time:03-04

I want to add mean, median, max, min to my expss table ,but I am unable to do so. Do we have any solution to add mean median mode, min, max to my table?

data<-data.frame(
  gender = c(1,2,1,2,1,2,1,2,2,2,2,1,1,2,2,2,2,1,1,1,1,1,2,1,2,1,2,2,2,1,2,1,2,1,2,1,2,2,2),
  sector = c(3,3,1,2,5,4,4,4,4,3,3,4,3,4,2,1,4,2,3,4,4,4,3,1,2,1,5,5,4,3,1,4,5,2,3,4,5,1,4),
  col1=c(1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,0,1,1,0,0,0,0,1,0,0,0,0,1,0,1),
  col2=c(1,1,1,1,1,0,0,0,0,1,1,1,1,1,0,0,0,1,1,1,0,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,0,0,0),
  col3=c(1,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,1,1,0,0,0,1,0,0,1,1,1,1,1,0,0,1),
  col4=c(1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
  col5=c(1,2,1,1,1,2,1,2,2,1,2,1,1,1,2,2,2,1,1,1,2,1,2,1,1,1,2,2,2,1,1,2,2,1,1,1,2,2,2)
)

data$gender<-factor(data$gender, levels=c(1,2), labels=c("Male","female"))
data$sector<-factor(data$sector, levels=c(1,2,3,4,5), labels=c("TX","CA","NY","LA","WA"))
data$col1<-factor(data$col1, levels=1, labels="Sales")
data$col2<-factor(data$col2, levels=1, labels="OPS")
data$col3<-factor(data$col3, levels=1, labels="Management")
data$col4<-factor(data$col4, levels=1, labels="HR")
data$col5<-factor(data$col5, levels=c(1,2), labels=c("National","Overseas"))



data$gender1 <- ifelse(data$gender == "Male",1,0)
data$total <- ifelse(data$col5 == "National",1,0)

val_lab(data$gender1)<-c("GENDER"=1)
val_lab(data$total)<-c("All Market"=1)
lst <- list(data$gender1,data$total)


fun1 <- function(dataset,var_list,banner1){
  
  dataset<-dataset[var_list] %>% as.data.frame() 
  first_col_param <- head(var_list,1)
  second_col_param <- tail(var_list,1)
  var_lab(colnames(dataset)[ncol(dataset)]) <- ""
  
  t1<- cross_fun(dataset, rlang::parse_expr(paste0("mrset(",
                                                   first_col_param ," %to% ",second_col_param,")")),
                 col_vars = banner1,
                 fun = combine_functions("Mean" = Mean, 
                                         "Median" = Median,
                                         "Max"= Max,
                                         "Min"=Min,
                                         "25th Perc" = perc_25,
                                         "75th Perc" = perc_75,
                                         "Valid N" = valid_n
                 ))
  
  t1
}


debug(fun1)
fun1(dataset=data,c("col1","col2","col3"),banner1=lst)

While trying this function, I am getting error:

Error in as.data.frame.default(x, optional = FALSE, check.names = FALSE, : cannot coerce class ‘c("labelled", "call")’ to a data.frame

CodePudding user response:

There are several things going on here. The main error is actually from the internal function test_and_make_data_frame() where the function finds that the second argument is an unevaluated labelled call. That can be fixed, but another problem is that the functions you're using to summarize the data require (except for valid_n) require numeric data and you're handing it factors, so even after fixing the function, there remains errors. Here is a working example using the mtcars data:

library(expss)
#> Loading required package: maditr
#> 
#> To select columns from data: columns(mtcars, mpg, vs:carb)
#> 
#> Use 'expss_output_viewer()' to display tables in the RStudio Viewer.
#>  To return to the console output, use 'expss_output_default()'.
data(mtcars)
lst <- list(as.factor(mtcars$cyl),as.factor(mtcars$am))


fun1 <- function(dataset,var_list,banner1){
  perc_25 <- function(x, ...){unname(quantile(x, .25, na.rm=TRUE))}
  perc_75 <- function(x, ...){unname(quantile(x, .75, na.rm=TRUE))}
  
  dataset<-dataset[var_list] %>% as.data.frame() 
  first_col_param <- head(var_list,1)
  second_col_param <- tail(var_list,1)
  var_lab(colnames(dataset)[ncol(dataset)]) <- ""
  mr <- parse(text=paste0("mrset(",
               first_col_param ," %to% ",second_col_param,")"))
  t1<- cross_fun(dataset, 
                 eval(mr),
                 col_vars = banner1,
                 fun = combine_functions("Mean" = mean, 
                                         "Median" = median,
                                         "Max"= max,
                                         "Min"=min,
                                         "25th Perc" = perc_25,
                                         "75th Perc" = perc_75,
                                         "Valid N" = valid_n
                 ))
  
  t1
}
fun1(dataset=mtcars,c("hp","mpg","qsec"),banner1=lst)

enter image description here

Created on 2022-03-03 by the reprex package (v2.0.1)

  •  Tags:  
  • r
  • Related