I'm getting an "invalid first argument" error for the following. However, con is an actual connection and is set up properly. So what does this error actually refer to?
library(dplyr)
con <- RSQLServer::src_sqlserver("***", database = "***")
myData <- con %>%
tbl("table") %>%
group_by( work_dt, campaign, ad_group, matchtype, keyword ) %>%
select( work_dt, campaign, ad_group, matchtype, keyword, impressions, clicks, cost ) %>%
filter(site_id %in% c(6932,6946,6948,6949,6951,6952,6953,6954,
6955,6964,6978,6979,7061,7260,7272,7329,
7791,7794,7850,7858,7983)) %>%
filter(work_dt >= as.Date("2014-10-01 00:00:00") & work_dt < as.Date("2014-10-02 00:00:00")) %>%
summarise(
sum_impressions = sum(impressions),
sum_clicks = sum(clicks),
sum_cost = sum(cost),
) %>%
collect()
This code produces:
Error in exists(name, env) : invalid first argument
exists("con")
> exists(con)
Error in exists(con) : invalid first argument
> exists("con")
[1] TRUE
Related
I need to create a line ID column within a dataframe for further pre-processing steps. The code worked fine up until yesterday. Today, however I am facing the error message:
"Error in mutate():
ℹ In argument: line_id = (function (x, y) ....
Caused by error:
! Can't convert y to match type of x ."
Here is my code - the dataframe consists of two character columns:
split_text <- raw_text %>%
mutate(text = enframe(strsplit(text, split = "\n", ))) %>%
unnest(cols = c(text)) %>%
unnest(cols = c(value)) %>%
rename(text_raw = value) %>%
select(-name) %>%
mutate(doc_id = str_remove(doc_id, ".txt")) %>%
# removing empty rows + add line_id
mutate(line_id = row_number())
Besides row_number(), I also tried rowid_to_column, and even c(1:1000) - the length of the dataframe. The error message stays the same.
Try explicitly specifying the data type of the "line_id" column as an integer using the as.integer() function, like this:
mutate(line_id = as.integer(row_number()))
This code works but is not fully satisfying, since I have to break the pipe:
split_text$line_id <- as.integer(c(1:nrow(split_text)))
How can I convert this query into R.
Select Location, MAX(cast(Total_deaths as int)) as TotalDeathCount
From PortfolioProject..CovidDeaths
Where continent is not null
Group by Location
order by TotalDeathCount desc
I tried this but keeping getting an error.
library(dplyr)
library(tidyr)
covid_death %>%
drop_na() %>%
select(total_deaths) %>%
summarise(TotalDeathCount = max(total_deaths)) %>%
filter(continent != null)
Error in filter(., continent != null) :
Caused by error in `mask$eval_all_filter()`:
! object 'null' not found
The following should do:
covid_death %>%
drop_na() %>%
group_by(location) %>%
summarise(TotalDeathCount = max(total_deaths)) %>%
arrange(desc(TotalDeathCount))
I am using the R programming language. I am trying to follow the R tutorial over here on neural networks (lstm) and time series: https://blogs.rstudio.com/ai/posts/2018-06-25-sunspots-lstm/
I decided to create my own time series data ("y.mon") for this tutorial (the same format and the same variable names) :
library(tidyverse)
library(glue)
library(forcats)
library(timetk)
library(tidyquant)
library(tibbletime)
library(cowplot)
library(recipes)
library(rsample)
library(yardstick)
library(keras)
library(tfruns)
library(dplyr)
library(lubridate)
library(tibbletime)
library(timetk)
index = seq(as.Date("1749/1/1"), as.Date("2016/1/1"),by="day")
index <- format(as.Date(index), "%Y/%m/%d")
value <- rnorm(97520,27,2.1)
final_data <- data.frame(index, value)
y.mon<-aggregate(value~format(as.Date(index),
format="%Y/%m"),data=final_data, FUN=sum)
y.mon$index = y.mon$`format(as.Date(index), format = "%Y/%m")`
y.mon$`format(as.Date(index), format = "%Y/%m")` = NULL
y.mon %>%
mutate(index = paste0(index, '/01')) %>%
tk_tbl() %>%
mutate(index = as_date(index)) %>%
as_tbl_time(index = index) -> y.mon
From here on, I follow the instructions in the tutorial (replacing the "sun_spots data" with "y.mon". Everything works fine until this point (I posted a question yesterday that got closed for being too detailed https://stackoverflow.com/questions/65527230/r-error-in-is-symbolx-object-not-found-keras - the code can be followed from the rstudio tutorial) :
#ERROR
coln <- colnames(compare_train)[4:ncol(compare_train)]
cols <- map(coln, quo(sym(.)))
rsme_train <-
map_dbl(cols, function(col)
rmse(
compare_train,
truth = value,
estimate = !!col,
na.rm = TRUE
)) %>% mean()
rsme_train
Error in is_symbol(x) : object '.' not found
I found another stackoverflow post which deals with a similar problem:Getting error message while calculating rmse in a time series analysis
According to this stackoverflow post, this first error can be resolved like this:
coln <- colnames(compare_train)[4:ncol(compare_train)]
rsme_train <-
map_df(coln, function(col)
rmse(
compare_train,
truth = value,
estimate = !!col,
na.rm = TRUE
)) %>%
pull(.estimate) %>%
mean()
rsme_train
However, the following section of the tutorial has a similar section in which the same error persists even after applying the corrections:
compare_test %>% write_csv(str_replace(model_path, ".hdf5", ".test.csv"))
compare_test[FLAGS$n_timesteps:(FLAGS$n_timesteps + 10), c(2, 4:8)] %>% print()
cols <- map(coln, quo(sym(.)))
rsme_test <-
map_dbl(cols, function(col)
rmse(
compare_test,
truth = value,
estimate = !!col,
na.rm = TRUE
)) %>% mean()
rsme_test
#errors:
Error in stri_replace_first_regex(string, pattern, fix_replacement(replacement), :
object 'model_path' not found
Error in is_symbol(x) : object '.' not found
These errors are preventing me from finishing the rest of the tutorial.
Can someone please show me how to fix these?
Thanks
Try using coln in map_dbl :
rsme_test <- map_dbl(coln, function(col)
rmse(
compare_test,
truth = value,
estimate = !!col,
na.rm = TRUE
)) %>% mean()
Im using the below statement for converting rownames to column
library(tidyverse)
names(res) <- names(dt)[]
final<- imap(res, ~ .x %>%
as.data.frame %>%
select(!! .y := `Point Forecast`) %>%
rownames_to_column("Month_year")) %>%
reduce(inner_join, by = "Month_year")
and when i try to write the output to a db,
with
dbWriteTable(mycon, value = final , Database= 'mydb' ,name = "Rpredict", append = TRUE )
i receive an error as below:
Error in result_insert_dataframe(rs#ptr, values) :
nanodbc/nanodbc.cpp:1587: 42S22: [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid column name 'Month_year'
How do i fix this?
I am trying to scrape IMDB data, and for one variable I keep getting an error.
Error: Result 28 is not a length 1 atomic vector
library(rvest)
library(purrr)
library(tidyr)
topmovies <- read_html("http://www.imdb.com/chart/top")
links <- topmovies %>%
html_nodes(".titleColumn") %>%
html_nodes("a") %>%
html_attr("href") %>%
xml2::url_absolute("http://imdb.com")
pages <- links %>% map(read_html)
budget <- try(pages %>%
map_chr(. %>%
html_nodes("#titleDetails .txt-block:nth-child(11)") %>%
html_text() %>%
#gsub("\\D", "", .) %>%
extract_numeric()),silent=TRUE)
When I do it with try (as in the code), I get
budget
[1] "Error : cannot convert object to a data frame\n" attr(,"class")
[1] "try-error" attr(,"condition") <Rcpp::exception: cannot convert
object to a data frame>
It would be great if anyone could tell me what is going wrong / why try isn't just skipping that result?
This can happen when you encounter NULL. Try:
map_if(is_empty, ~ NA_character_)
in the pipe chain and see if it works.