Skip to main content

Questions tagged [r]

R is a free, open source programming language and software environment for statistical computing and graphics.

Filter by
Sorted by
Tagged with
3 votes
1 answer
33 views

Mean by groups Rcpp

Here is an attempt to translate a R function to Rcpp. The goal is to group every n observations together (of a numeric vector) and calculate the mean. On big'ish vectors the R function is almost two ...
snoram's user avatar
  • 435
2 votes
1 answer
127 views

R Script to forecast volatility of monthly crude oil prices using GARCH

This is my first time writing a script for volatility forecasting and I am fairly new to econometrics. I chose to analyze monthly WTI crude oil prices of past 20 years and determine volatility as well ...
dovexz12323's user avatar
1 vote
0 answers
91 views

`summarize()` the Power Set of Grouping Variables in R

Background On a whim, I wrote a function called summarize_pow(). It performs dplyr::summarize() on ...
Greg's user avatar
  • 423
2 votes
2 answers
54 views

Correctness of newborn feeding data visualization

Just wanted some code review for correctness here. This is a little app for plotting newborn feeding data (important to keep track of in the first 48 hours of so) that I hope to provide to a clinic, ...
Sam Weisenthal's user avatar
0 votes
0 answers
76 views

Combine and Propagate "Vectors" in Power Query M

Background In an earlier post, I prototyped an M equivalent of tidyselect in R: in particular, its general syntax and selection helpers. In this post, I prototype ...
Greg's user avatar
  • 423
1 vote
0 answers
110 views

"Tidy Selection" in Power Query M

Update I have posted a sequel (here) with my prototype for the "missing piece": the combination and propagation of variable names, especially within nested ...
Greg's user avatar
  • 423
2 votes
1 answer
145 views

Acceleration of Hidden Markov Modell likelihood calculation in Rcpp

I wrote the following code in Rcpp, i.e. it is C++ code that is compiled in R for making faster calculations. The code in Rcpp is intended to calculate in a recursive way the likelihood function for ...
Jonathan1234's user avatar
2 votes
2 answers
70 views

Comparing univariable regression results between complete-case and multiple imput data sets

Dear Code Review community, I try to develop a reproducible code to pool univariable regression estimates across multiple imputed data sets in R. I therefore prepared the following code that is ...
HuMaN90's user avatar
  • 21
0 votes
0 answers
59 views

Forecast the turnover with a prophet model in R

I am a student who is interested in data analytics and currently figuring out how to write forecast models for several scenarios in R. I'm working on a fictitious project in which I want to forecast ...
LearnHoney's user avatar
1 vote
0 answers
159 views

Filtering spatial data based on a distance matrix from large datasets

I am using a R function to spatially filter location points. There are five steps in the function: 1) calculate the distance between all points, 2) for each point, define the number of neighboring ...
Marine Régis's user avatar
0 votes
1 answer
42 views

Transform variables based on a constant in another object

My problem I am working with two objects, structured as follows: ...
Giulio Centorame's user avatar
1 vote
0 answers
25 views

LASSO kernel for nonparametric models

I am a novice in nonparametric modeling and have recently begun exploring the world of varying coefficient models. Essentially, these models involve a non-linear least squares problem that relies on ...
MOHAMMED's user avatar
  • 111
1 vote
1 answer
64 views

Optimize an algorithm for preparing a dataset for machine learning

I'm learning how to use R coming from a python background. I'm following Andrej Karpathy's zero-to-hero course, reimplementing it in R. We start with a list of 32033 names. These names have to be ...
plaffont's user avatar
2 votes
0 answers
1k views

R function to sort columns of gtsummary::tbl_summary objects

I work with gtsummary package a lot and wanted to try creating a function to sort the columns of table output from tbl_summary() ...
nightstand's user avatar
5 votes
0 answers
57 views

Exploring how densities can be approximated by histograms in R

...
user21582991's user avatar
1 vote
1 answer
95 views

Group uncorrelated variables into subsets using correlation matrix

I am trying to group uncorrelated variables into subsets. So, using the correlation matrix, I check each variable to see the correlation. If correlation is more then a threshold I will create a new ...
Nmgh's user avatar
  • 13
3 votes
2 answers
93 views

Convert dataset from wide to a longer format for two different set of columns

I have a dataset that is a bit too wide for the EDA plots I would like to make. The data can be found here: https://www.massey.ac.nz/~kgovinda/data/Bill_Colour.RData But looks something like this: ...
James's user avatar
  • 55
8 votes
3 answers
616 views

Python+R Project: Fantasy Football Power Rankings Graphic Generator

I am in a fantasy football league and each week I send out a power rankings poll through Microsoft Forms. People will send in their rankings poll and I will create a graphic based on responses. I was ...
jordanar189's user avatar
2 votes
1 answer
65 views

Calculate number of max and min outliers from data.frame

I am working on a problem where I need to get specific information for min and max outliers. This is in relation to the ggplot library. I will be getting all my information from ggplot_build object. ...
James's user avatar
  • 55
0 votes
1 answer
65 views

How can I can do better with my R code to analyze word counts

I am new to R and to learn I start to code to do things that interest me. I have a biological background and I am very interested in bioinformatics. I know some python, but I want to use R as tool to ...
Paulo Sergio Schlogl's user avatar
1 vote
1 answer
216 views

Find Reddit comments with a particular keyword

I am writing a loop (in R) to webscrape Reddit posts - using Reddit's API ("Pushshift"). Essentially, I would like to get every comment that contains the word "Trump" between now ...
antonoyaro8's user avatar
2 votes
0 answers
64 views

S_n covariance calculation function

I am trying to understand the estimator proposed in ["Multidimensional outlier detection and robust estimation using Sn covariance"][1]. I wrote the code in a basic line-by-line form which ...
user263375's user avatar
2 votes
0 answers
50 views

Plot ellipse hypothesis plots with confidence intervals for multiple models

I have developed a function to graphically plot between two predictors of interest their confidence interval regions on an ellipse. It states whether the null hypotheses have been accepted for the one-...
Working dollar's user avatar
2 votes
0 answers
124 views

All permutations of a linear model given all predictors

I have developed a fairly large function that can do the following: From a dataset, select the response variable and the independent variables. Get all the number of permutations for the formula ...
Working dollar's user avatar
2 votes
1 answer
42 views

R: Function to return the category that has the maximum in another variable [closed]

Is there a nice simple function to return which diet has the highest average? I can calculate the means from each diet using: ...
Taffy's user avatar
  • 21
1 vote
0 answers
62 views

Extract, transform, and load Excel files

I am curious to know if there is a cleaner/better way to write this. Any suggestions would be greatly appreciated! I do ask some questions in comments within the code specific to certain functions/...
Dre Day's user avatar
  • 111
1 vote
1 answer
67 views

R code to calculate coef

Can you help me think of some way to reduce the computational time of a code that generates a certain value, which in this case I call coef, which will depend on <...
JVieira's user avatar
  • 121
1 vote
0 answers
37 views

Another solution approach to find if trailing zeros exist for each row id

The code below removes trailing zeros exist for each row id. This question has been resolved here https://stackoverflow.com/questions/69452882/delete-column-depending-on-the-date-and-code-you-choose. ...
JVieira's user avatar
  • 121
0 votes
1 answer
56 views

Mapping variables in two data frames using a third data frame

I have two data frames, df1 and df2 of identical structure. The first three columns, id, <...
jpsmith's user avatar
  • 154
2 votes
1 answer
671 views

Create a folder with subfolders and files from a R dataframe

I have such a dataframe: ...
Stéphane Laurent's user avatar
0 votes
1 answer
81 views

A function that modifies the column types of a `data.frame`, given a character vector of "format codes"

Problem A business process requires me to read two different text files (raw and processed) into R as data.frames, with each column being read as a character type. ...
Christian Million's user avatar
1 vote
1 answer
67 views

How can I simplify the code using just the tidyverse?

So I have the data frame below, and I'm trying to creat a new Data frame which show me the name of the department which have the highest and the lowest employee turnover. The code that I write is ...
Mattia Valsangiacomo's user avatar
0 votes
1 answer
57 views

Faster way to print Number of Descents in a permutation in R

I am trying to find a more efficient way to print the number of descents in a permutation using R as my programming language. This code does it for the numbers 3 to 10: ...
code writer 3000's user avatar
3 votes
0 answers
48 views

R language optimize converting mpfr to gmp

I wrote a function to convert, without loss of precision, a mpfr - class value to a gmp bigq value. (This particular operation ...
Carl Witthoft's user avatar
1 vote
1 answer
143 views

Conditionally drop rows in grouped data.frame using dplyr in R

I have a data frame with columns for three predictors that try to predict the target variable 'change' with values 'yes' and 'no'. The data frame is grouped by id, and for every id, I have 4 years. ...
saQuist's user avatar
  • 113
2 votes
2 answers
268 views

Assign 0 or 1 with different probabilities conditional on another column in R

I am trying to assign a 0 or 1 with some stochasticity based on another column in a data frame (outcome). If outcome == 1, the ...
jpsmith's user avatar
  • 154
1 vote
0 answers
58 views

Estimating risk differences for marginal models

I need to estimate risk differences for marginal models. I have worked on the Probratio function to create an equivalent function for RDs that works. Please provide feedback on accuracy or comments to ...
Jacque's user avatar
  • 11
1 vote
0 answers
44 views

Combine several calculations on several variables

This is my very first time here, I am coming from stackoverflow.com thanks to @r2evans. So I apologize in advance if I don't follow all the rules. I wrote a code in ...
Discus23's user avatar
  • 111
0 votes
1 answer
58 views

Increase speed of lapply function identifying the sum of all existing combinations

I have a dataframe of cases that score 0-1 on a group of binary attributes. What I want to do is extract all possible ombinations of attribute triplets (e.g. A/B/C, A/B/D... out of A-E) and then sum ...
Fnguyen's user avatar
  • 163
2 votes
0 answers
30 views

Transform dataframe to a certain JSON format

I need to go from this dataframe: ...
Stéphane Laurent's user avatar
3 votes
1 answer
92 views

Simulating disease spread in a population

I am trying to build a model that allows me to estimate the spread of disease in the population. The setup is similar to the SIR model, with the difference that this model includes the stochastic ...
KLMK's user avatar
  • 31
0 votes
1 answer
376 views

An efficient way for removing rows with zero elements iteratively while constructing the matrix

Based on a previous post, Accelerating creation of matrices and finding ways for optimal scaling, we managed to accelerate the way that I construct a matrix in Rcpp (inputs for example are at the end ...
Jonathan1234's user avatar
0 votes
1 answer
57 views

R code for reading tabular data files and plotting light curves of modeled stars

Here is a minimal example of my task... I have four 2-column files. profile1.data ...
Woj's user avatar
  • 191
1 vote
1 answer
38 views

R code for reading tabular data files and plotting light curves of modeled stars

I am given a file named profile1.data that looks like this: ...
Woj's user avatar
  • 191
1 vote
1 answer
75 views

Accelerating creation of matrices and finding ways for optimal scaling

I have the following specific task, for which I have written code, in Rcpp, but it doesn't scale well and I would like to improve it. I'll describe first the steps of the procedure Step 1: We have ...
Jonathan1234's user avatar
1 vote
2 answers
67 views

R coding: creating a name variable to use with select

I am wondering if this is correct: ...
Elina Bochkova's user avatar
0 votes
1 answer
72 views

R code optimization

I have about 977 obs in top500Stocks which contains name of 977 stocks. ...
Stupid_Intern's user avatar
2 votes
1 answer
318 views

Find if a dataset of words contains any words containing all vowels by using exactly 5 calls to str_detect

Take the words dataset from stringr. An R4DS exercise challenges the reader to check if there are any words in that dataset that ...
J. Mini's user avatar
  • 127
1 vote
0 answers
40 views

using R probability

I am working on the subject of the French championship 2019, composed of 20 teams, and each team plays 38 times, if team A plays with team B, whatever the result, each team plays another time with ...
life success's user avatar
3 votes
0 answers
151 views

Rcpp sparse CSC matrix class

This is a sparse matrix (dgCMatrix) class that extends Rcpp. WHAT: This class includes Rcpp::NumericVector and ...
zdebruine's user avatar
  • 153

1
2 3 4 5
8