Category "r"

How does ggplot2 split groups using cut_number if you have a small number of data points?

I am wondering about the "behind-the-scenes functionality of ggplot2 and cut_number. I have some pretty complex data that has many subsets of data with a relati

Transform code of two loops ("For") with lapply or sapply

I am learning how to code in R and I have written a piece of code that calculate the Net Present Value (NPV) of various assets at different points in time, give

Strahler stream order using igraph or sfnetwork in R

I cannot fathom how to derive strahler order in R. Here's an example in postgres and neo4j. An attempt in R There are three rules (from the GRASS 7.8 Manual): i

Is there a way to add the bin range label into the tooltip for a histogram using ggplotly in R?

library(tidyverse) library(ggplot2) library(plotly) data(mpg) ggplotly( mpg %>% ggplot(aes(x=hwy)) + geom_histogram(), tooltip = ("all")) When you

separate_columns for tidyr

Let's say I had a survey question that read: What did you eat? [ ] apple [ ] pear [x] banana [x] grapes Now, I have the endorsed options as comma-separated st

Why am I not getting high-resolution with ggsave()?

I'm trying to get 300 dpi images with ggsave, and am only getting the default 72 dpi. Here's an example of the code I'm using: library(tidyverse) x <- rnorm(

Multiply Columns of two different dataframes

How to efficiently multiply columns of two different data frames with same number of rows but different number of columns. I have two data sets Volumes and pri

Calculate Stock

Is it possible calculated stock using R? The formula is stock+purchase-sold. In this case first stock (row1) is 0, rg first result stockB1= 12 - 3 = 9 the secon

grouped data frame to list

I've got a data frame that contains names that are grouped, like so: df <- data.frame(group = rep(letters[1:2], each=2), name = LETTERS[1:4

How to reduce a categorical variable in a logistic regression model in R

I've created a logistic regression formula regarding mpg for various makes and models of cars. One variable "origin" was integer with : 1=American, 2=German, 3=

Looping a function/analysis in R by unique column values

I am trying to sort people in my dataset into three body-size categories (Small, Medium, Large). Consider the toy example below: library(ggplot2) library(dplyr)

How to reorder a list by the size of the data frame in each list element?

I have a list in which each element contains a data frame with n nrows. For example, dfA <- data.frame(A=c("a","a","a")) dfB <- data.frame(B=c("b","b")) d

finding peaks and peak interval for non-uniform waves

Big Update I found an inefficient, but working solution for finding peaks and peak interval, although it doesn't work for multiple groups(in the code below I us

R neuralnet package: Can't train neural network

I'm trying to use the neuralnet package to train a model on this data set. However, I'm getting the following error which I can't understand: Error: the err

Create loyalty variable in R by subtracting 2 date variables. Problem with getting the total amount of years. Football data

Im currently working for a Dutch football club and run into some problems with getting the length of relationship of certain fans. In the image below a short ov

trelliscopejs R package only one trelliscope figure visible in html file

I am having a problem rendering more than one trelliscopejs displays in an html file created with Rmarkdown. I'm using self_contained=TRUE in order to render di

Mermaid diagram line break

I cannot find how to insert a line break in long titles inside nodes. For example: library(DiagrammeR) mermaid(" graph TB A[GE Solution]-->C{ } B[

Second to last Wednesday of month in R

In R, how can I produce a list of dates of all 2nd to last Wednesdays of the month in a specified date range? I've tried a few things but have gotten inconsiste

Adding a leaflet in a precompile vignette

I'm maintaining R code in my company that is use by multiple colleagues. I decided to convert my code into a package to make it simpler to share, maintain and

Downloading file from a private repo into R

I have a zip file containing data in a private Github repo. I am writing some R code, and I wish to download the data into R. When I run the following code I ge