Caret featurePlot giving "object cannot be coerced" error - r

Code that use to run:
library(caret)
library(tidyverse)
data_set <- diamonds %>%
filter(row_number() <= 1000) %>%
select(carat, depth)
featurePlot(x = data_set[,-1], y = data_set[,1])
now fails with
Error in diff(as.numeric(y[ord])) :
(list) object cannot be coerced to type 'double'
Below also fails:
featurePlot(x = as.data.frame(data_set[,-1]), y = as.data.frame(data_set[,1]))
But below works:
featurePlot(x = data_set$depth, y = data_set$carat)
Seeking a way to use sub setting by column number.

I think there is something going on with the tibble class---I've encountered something similar. Try the following. It works for me:
class(data_set) <- "data.frame"
featurePlot(x = data_set[,-1], y = data_set[,1])
You might want to file an issue on GitHub, because it is indeed strange that your second approach (which is very sensible) does not work. I can confirm that both approaches that you tried also generate the same errors for me.

Related

What is generating the error 'Can't subset `.data` outside of a data mask context' with 'dplyr'?

I have a huge shiny app which uses a huge package. I'm not the author of any of them and I'm a bit lost. A function (fermentationPlot) throws the error: Can't subset .data outside of a data mask context:
Warning: Error in fermentationPlot: Can't subset `.data` outside of a data mask context.
185: <Anonymous>
173: dplyr::arrange
172: dplyr::mutate
171: as.data.frame
What could be the cause of this error? What does it mean? Below is the code block which generates it. I googled this error message and I found that it can be fixed by downgrading 'dplyr'. I tried 1.0.10, 1.0.5 and 1.0.0, and the error always occurs.
plotInfo <- dplyr::left_join(
x = dplyr::select(
plotDefaults, -c(.data$templateName, .data$minValue, .data$maxValue)
),
y = plotSettings,
by = .data$dataName
) %>%
dplyr::arrange(!is.na(.data$order), -.data$order) %>%
dplyr::mutate(
color = replace(.data$color, .data$color == "Blue", "Dark blue"),
minValue = as.numeric(.data$minValue),
maxValue = as.numeric(.data$maxValue)
) %>%
as.data.frame()
The by argument of left_join must be a character vector of column names. Probably the author wanted to do
by = "dataName"
and not
by = .data$dataName

Error: 'list' object cannot be coerced to type 'double' in R

I'm new to R. I'm trying to get the SD of weight in lbs. First I'm getting the weight in lbs from a dataset with weight in kg. When I get type of() for the result, it's a list. But in the console, its a 'list' of 'dbl'. I've tried 'as.numeric()' and 'as.integer()' in the pipe but both give the same error. How can I get the SD?
I have other questions that have similar issues (data type being a list when they should be numeric) so if you can explain why that's happening that would be great!
weight_lbs <- brfss %>%
clean_names(., "lower_camel") %>%
select(havarth3, wtkg3)%>%
filter(havarth3 == "1")%>%
na.omit()%>%
mutate(weight_lbs=(round(wtkg3*2.20462)/100),2)%>%
select(weight_lbs)%>%
as.numeric()
weight_lbs
sd_weight <- sd(weight_lbs, na.rm=TRUE)
Try this code:
I think as.numeric() alone won't work. wrap it into a mutate:
weight_lbs <- brfss %>%
clean_names(., "lower_camel") %>%
select(havarth3, wtkg3)%>%
filter(havarth3 == "1")%>%
na.omit()%>%
mutate(weight_lbs=(round(wtkg3*2.20462)/100),2)%>%
select(weight_lbs)%>%
mutate(weight_lbs = as.numeric(weight_lbs)) %>%
mutate(sd_weight_lbs = sd(weight_lbs))

Filter data with !is.na() . Alternative to !is na?

I'm trying to replicate a code so this is my following code:
statefips <- read.csv("https://raw.githubusercontent.com/kjhealy/fips-codes/master/state_fips_master.csv") %>%
select(state_name, state_abbr, region_name, division_name) %>% dplyr::rename(state = state_name)
That code works fine.
But there is an issue here:
uspop <- read.csv("https://raw.githubusercontent.com/JoseMontoya518/uspop2018/master/PEP_2018_PEPSR6H_with_ann.csv") %>%
janitor::clean_names()%>%
dplyr::filter(year_id == "est72018", !is.na(statefips))
I get this error message:
Error: Problem with `filter()` input `..2`.
x Input `..2` must be of size 5149 or 1, not size 50.
i Input `..2` is `!is.na(statefips)`.
So I try another way , instead of adding:
!is.na(statefips)
as an input in filter function. I use this:
%>% na.exclude
So this is the final code:
uspop <- read.csv("https://raw.githubusercontent.com/JoseMontoya518/uspop2018/master/PEP_2018_PEPSR6H_with_ann.csv") %>%
janitor::clean_names()%>%
dplyr::filter(year_id == "est72018")%>% na.exclude
That code works ,but I don't know if the purpose of the code is achieved.
!is.na(statefips)
was added as an input for a reason in the code which I'm replicating. When I remove %>% na.exclude, nothing change from the original data frame:
uspop <- read.csv("https://raw.githubusercontent.com/JoseMontoya518/uspop2018/master/PEP_2018_PEPSR6H_with_ann.csv") %>%
janitor::clean_names()%>%
dplyr::filter(year_id == "est72018")
So , Is there a way to filter the data with this input: !is.na(statefips) ?

How to fix RichnessGrid Error in split.default

I tried to use RichnessGrid to count species occurrence on the map. But I am constantly getting the error message
"Error in split.default(x = seq_len(nrow(x)), f = f, drop = drop, ...) :
group length is 0 but data length > 0".
By checking other posts, it seems that this is the error message for typos, which is not my case. Does anyone know how to trouble shot this problem?
My data look like this
I tried a few things: 1. change the resolution option or the type definition; 2. change header of my data; 3. look at the summary of my data and sample data. But nothing worked, and I still could not figure out where went wrong.
dput(head(clean))
#subset my df (clean) for RichnessGrid
dat<-clean %>% select(the.plant.list,longitude,latitude)
# tried to change header but still failed
dat <- dat %>% rename(species = the.plant.list)
head(dat)
RichnessGrid(dat, reso=60, type = "spnum")
#try sample data and code
data(lemurs)
e <- c(-125, -105, 30, 50)
RichnessGrid(lemurs, e, reso = 60, type = "spnum")
#compare sample data and my own
data(lemurs)
data(dat)
summary(lemurs)
summary(dat)

R dplyr filter for non-standard evaluation (NSE) ".dots is missing, with no default"

I am hoping someone can help determine why I receive an error message when using lazy evaluation as part of a dplyr filter_ verb. The end goal is to pass arguments by reference using a function but I have narrowed the problem down outside of a function.
library(dplyr)
library(lazyeval)
library(data.table)
data_raw_dt <- data.table(
R_dates = seq(from = as.Date("2015/8/31"), by = "1 day", length.out = 30),
grp_region = sample(letters[1:4], 30, replace = TRUE),
z_valuation = rnorm(30)
)
# Create some NAs
data_raw_dt$grp_region[data_raw_dt$grp_region == "d"] <- NA
dates = "R_dates"
group = "grp_region"
column = "z_valuation"
filter_criteria = interp(~(!is.na(var)), var = as.name(group))
data_raw_dt %>%
filter_(filter_criteria)
But this gives the following error message:
"Error in lazyeval::common_env(.dots) :
argument ".dots" is missing, with no default"
In this case, I am not sure how to specify .dots and when I do it asks for a list. I have checked here, here, and here and structured my code following these examples with no success.
Package version:
dplyr 0.4.2
lazyeval 0.1.10
data table 1.9.4
Does anyone have any ideas? Thank you so much in advance!
Just replace with
filter_(.dots = filter_criteria)

Resources