R function for obtaining a reference to a variable
In Advanced R, environments are advertised as a useful way to get pass-by-reference semantics in R: instead of passing a list, which gets copied, I can pass an environment, which is not. This is useful...
View ArticleWhy is the computation is faster with a loop?
Update:As pointed out by @Dave2e, moving the start_time statement (in code 2) out of the for loop will make the running time comparable to code 1. Namely:start_time <- Sys.time() for (j in 1:1) { n...
View ArticleCreate a kml/kmz file in R with embedded photos
I am using plotKML to generate a kml file with images to overlay inside Google Earth. The following code comes from the help file of spPhoto().library(plotKML) #> plotKML version 0.6-0 (2019-11-10)...
View ArticleVlookup and Count String Occurrences in Separate Table R to new Column
I have two data frames. Below are samples but should be easily reproducible for illustration.df1 <- data.frame(School = c("Omaha South", "Omaha Central", "Grand Island"), Enrollment = c(2166, 2051,...
View ArticleConverting the values in all columns to a certain value in tibble/dplyr
I have a tibble object and I want to replace all of the columns except certain ones with a value (say NA or 0). I can able to do it without pipes %>% but how can I do it with %>%?library(tibble)...
View ArticleRow Index where all Columns meet one Criterion
I got this dataframe:df <- data.frame( a = c("1", "2", "", "") , b = c("1", "", "3", "") ) a b 1 1 1 2 2 3 3 4 I want to indentify (and then actually remove) the rows where all columns fulfill one...
View ArticleGRanges as column in base::data.frame
I would like to store a GenomicRanges::GRanges object from Bioconductor as a single column in a base R data.frame. The reason I'd like to have it in a base R data.frame is because I'd like to write...
View ArticleFinding all terms near a given phrase
Is there a way to find all words that are associated with a given phrase?For example, say I want to find all of the words next to the word "illness" in a string. The string has the word "illness" quite...
View ArticleIdentify user plan behavior switches for subscription transaction in dplyr
I have a df identifying users and their recurring transactions in a subscription-based business model, where transactions can happen on a monthly, 3 months or 6 months cycle (every 30, 90, 180 days...
View ArticleFunction to automatically coerce integers to 64-bit integers in R?
I am running into an issue where integer calculations within an R-based model I am using are leaving R's 32-bit range. I have tried using the trace() function to manually coerce some of the values of...
View ArticleAdd next closest date from a second table matched on id in R
Say I have a datatable with a list of id numbers and date-times. Each id may appear more than once with a different date-time (example data in table 1) I want to add a column to table 1 with a date...
View ArticlePass .sql file into DBI::dbGetQuery with comments as the first line into R /...
I am trying to pass a .sql file into DBI::dbGetQuery, but I run into problems whenever there is comment in the first line. For example:runs_fine.sqlSELECT * FROM mytable does_not_work.sql-- Some...
View ArticlePassing Column Names to ggplot() in R
I've written a function in R to produce a graph from a given data frame:my_graph <- function (df, scale_fac) { row_count = nrow(df) df %>% mutate(star_sign = as.numeric(factor(star_sign, levels =...
View ArticleHow to export static plots from Plotly in R using Orca?
I have successfully installed Orca using Method 4 on the github page. However, I am unsure how best to proceed to export a static plot I have made in Plotly.I have tried calling Orca in my R script...
View Articleincorrect output for the statar::xtile function when using with dplyr
I ran into some problem when trying to use the statar::xtile function together with dplyr. I have a dataset called "resp_rate" as follows: userid group_id group_resp_rate 63775658 514 0.0315789...
View Articlenloptr error in getting the function evaluated
I am having an issue in running an optimization program in R. I have attached the codes here. This code is for non-linear programming having 8 decision variables(X[j]). I am trying to maximize the...
View ArticleLast observation of the previous group
I would like to know, if I have data that I can group by a variable, how can I get the last observation of the previous group?I have the following data:dt <-...
View ArticleHow to escape characters in SQL code in an R Markdown chunk?
``` {r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) library(tidyverse) library(odbc) library(DBI) library(dbplyr) ``` ```{r SQL, connection=con, output.var="df"} SELECT DB_Fruit.Pear,...
View ArticleAdding a list of extra dependencies in YAML using hyphens in RMD file?
I have the following YAML given to me as an RMD template for use in a course:--- title: "Chapter 1" author: output: pdf_document: extra_dependencies: - geometry - multicol - multirow html_document:...
View ArticleGSL numerical integration for a function with no extra parameters
I have a problem where I need to numerically integrate a univariate function with multiple extra inputs other than the variable that's being integrated over. The integration is from zero to infinity.I...
View Article