Error in as.data.frame.default(y) : cannot coerce class ‘"function"’ to a data.frame - dataframe

# Use x in the left_join() function
combined_tb <-
x %>%
left_join(presentation.tree, by = "toElementId", copy = TRUE) %>%
left_join(labels, by = c("toElementId"="elementId"), copy = TRUE) %>%
select(roleId, fromElementId, toElementId, order, fromHref,
toHref, concept, period, unit, value, decimals, labelString)
I do get this error message:
Error in as.data.frame.default(y) :
cannot coerce class ‘"function"’ to a data.frame
I don't know what to do.
I tried to solve the issue but nothing works

Related

tidymodels: "following required column is missing from `new_data` in step..."

I'm creating and fitting a workflow for a lasso regression model in {tidymodels}. The model fits fine, but when I go to predict the test set I get an error saying "the following required column is missing from `new_data`". Tha column ("price") is in both the train and test sets. Is this a bug? What am I missing?
Any help would be greatly appreciated.
# split the data (target variable in house_sales_df is "price")
split <- initial_split(house_sales_df, prop = 0.8)
train <- split %>% training()
test <- split %>% testing()
# create and fit workflow
lasso_prep_recipe <-
recipe(price ~ ., data = train) %>%
step_zv(all_predictors()) %>%
step_normalize(all_numeric())
lasso_model <-
linear_reg(penalty = 0.1, mixture = 1) %>%
set_engine("glmnet")
lasso_workflow <- workflow() %>%
add_recipe(lasso_prep_recipe) %>%
add_model(lasso_model)
lasso_fit <- lasso_workflow %>%
fit(data = train)
# predict test set
predict(lasso_fit, new_data = test)
predict() results in this error:
Error in `step_normalize()`:
! The following required column is missing from `new_data` in step 'normalize_MXQEf': price.
Backtrace:
1. stats::predict(lasso_fit, new_data = test, type = "numeric")
2. workflows:::predict.workflow(lasso_fit, new_data = test, type = "numeric")
3. workflows:::forge_predictors(new_data, workflow)
5. hardhat:::forge.data.frame(new_data, blueprint = mold$blueprint)
7. hardhat:::run_forge.default_recipe_blueprint(...)
8. hardhat:::forge_recipe_default_process(...)
10. recipes:::bake.recipe(object = rec, new_data = new_data)
12. recipes:::bake.step_normalize(step, new_data = new_data)
13. recipes::check_new_data(names(object$means), object, new_data)
14. cli::cli_abort(...)
You are getting the error because all_numeric() in step_normalize() selects the outcome price which isn't avaliable at predict time. Use all_numeric_predictors() and you should be good
# split the data (target variable in house_sales_df is "price")
split <- initial_split(house_sales_df, prop = 0.8)
train <- split %>% training()
test <- split %>% testing()
# create and fit workflow
lasso_prep_recipe <-
recipe(price ~ ., data = train) %>%
step_zv(all_predictors()) %>%
step_normalize(all_numeric_predictors())
lasso_model <-
linear_reg(penalty = 0.1, mixture = 1) %>%
set_engine("glmnet")
lasso_workflow <- workflow() %>%
add_recipe(lasso_prep_recipe) %>%
add_model(lasso_model)
lasso_fit <- lasso_workflow %>%
fit(data = train)
# predict test set
predict(lasso_fit, new_data = test)

tidyverse across where(!is.factor)?

I would like to create factor variables for all non-factor columns. I tried:
dat %>%
mutate(across(where(!is.factor), as.factor, .names = "{.col}_factor"))
But get error message:
Error in `mutate()`:
! Problem while computing `..1 = across(where(!is.factor), as.factor, .names = "{.col}_factor")`.
Caused by error in `across()`:
! invalid argument type
Run `rlang::last_error()` to see where the error occurred.
the where() function needs to be written as a formula, which in tidyverse shorthand is:
dat %>%
mutate(across(where(~!is.factor(.x)), as.factor, .names = "{.col}_factor"))

Error in as.data.frame.default(x[[i]], optional = TRUE): cannot coerce class ‘"function"’ to a data.frame

I am trying to make a c chart to analyse new diagnoses but I can't get past this stage. Please see code below:
Error in as.data.frame.default(x[[i]], optional = TRUE): cannot coerce class ‘"function"’ to a data.frame
Traceback:
qic(x = col_date, y = col_double(), data = measure1_data, chart = "c",
. freeze = ymd(20150201))
data.frame(x, y, n, notes, facets, cl, target)
as.data.frame(x[[i]], optional = TRUE)
as.data.frame.default(x[[i]], optional = TRUE)
stop(gettextf("cannot coerce class %s to a data.frame", sQuote(deparse(class(x))[1L])),
. domain = NA)

How to have partially italicized columns in pdf output?

This question is related to Creating a data frame that produces partially italicized cells with pkg:sjPlot functions
I'd like to have partially italicized cells in a kable. I have tried
library(tidyverse); library(kableExtra)
sum_dat_final2 <- list(Site = c("Hanauma Bay", "Hanauma Bay", "Hanauma Bay", "Waikiki", "Waikiki", "Waikiki"),
Coral_taxon = expression( italic(Montipora)~ spp.,
italic(Pocillopora)~spp.,
italic(Porites)~spp.,
italic(Montipora)~ spp.,
italic(Pocillopora)~spp.,
italic(Porites)~spp.))
sum_dat_final2 %>%
as.data.frame()%>%
kbl(longtable = F, "latex")
and got this error Error in as.data.frame.default(x[[i]], optional = TRUE) : cannot coerce class ‘"expression"’ to a data.frame
Many thanks in advance!!
You may italicize specific parts by adding $. In this sense, you need to set escape = F on your kbl function.
```{r}
library(tidyverse); library(kableExtra)
sum_dat_final2 <- list(Site = c("Hanauma Bay", "Hanauma Bay", "Hanauma Bay", "Waikiki", "Waikiki", "Waikiki"),
"Coral_taxon" = c("$Montipora$$~$ spp.",
"$Pocillopora$$~$spp.",
"$Porites$$~$spp.",
"$Montipora$$~$ spp.",
"$Pocillopora$$~$spp.",
"$Porites$$~$spp."))
sum_dat_final2 %>%
as.data.frame()%>%
kbl(longtable = F, "latex",
escape = F,
col.names = c("Site", "Coral taxonomie"))
```
--output

R dbWriteTable doesn't work with a column of all NA's and class not logical

I am trying to upload a table to SQL from R using dbWriteTable but I am having issues as some of my columns that have all NA's in them. I've learned that if the class is logical than it works but if it is anything else, it throws me an error.
Anybody have a solution? The columns which will have all NA's will be random so I can't just set a column to.logical() and I can't figure out a way to do it using lapply. I also do not want to get rid of these columns either.
Works
test <- data.frame(Name = c("Fred","Wilma","George"), Villians = c(2,4,3), Information = c(NA,NA,NA), stringsAsFactors = FALSE)
if (dbExistsTable(con, "test")) {dbRemoveTable(con, "test")}
dbWriteTable(con, name = "test", value = test, row.names = FALSE)
> sapply(test,class)
Name Villians Information
"character" "numeric" "logical"
Throws an error
test <- data.frame(Name = c("Fred","Wilma","George"), Villians = c(2,4,3), Information = c(NA,NA,NA), stringsAsFactors = FALSE)
test$Information <- as.character(test$Information)
if (dbExistsTable(con, "test")) {dbRemoveTable(con, "test")}
dbWriteTable(con, name = "test", value = test, row.names = FALSE)
Warning message:
In max(nchar(as.character(x)), na.rm = TRUE) :
no non-missing arguments to max; returning -Inf
> sapply(test,class)
Name Villians Information
"character" "numeric" "character"
I am using a company server so if there is any configuration that needs to be made on that end, I may or may not be able to get it done.