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
2
votes
0answers
24 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 ...
0
votes
1answer
25 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 ...
0
votes
0answers
42 views

R code that creates stellar interior light curves

Here is a minimal example of my task... I have four 2-column files. profile1.data ...
0
votes
1answer
46 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 ...
1
vote
1answer
37 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: ...
0
votes
0answers
20 views

R Count rowwise missing values for specific columns - dplyr cleanest version

I want to count the missing values for each case in different sets of variables in a bigger dataset. Lets say in the mtcars between the variable disp and the ...
1
vote
1answer
57 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 ...
1
vote
2answers
46 views

R coding: creating a name variable to use with select

I am wondering if this is correct: ...
0
votes
1answer
46 views

R code optimization

I have about 977 obs in top500Stocks which contains name of 977 stocks. ...
2
votes
1answer
94 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 ...
1
vote
0answers
31 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 ...
2
votes
0answers
52 views

Rcpp sparse CSC matrix class

This is a sparse matrix (dgCMatrix) class that extends Rcpp. WHAT: This class includes Rcpp::NumericVector and ...
1
vote
1answer
38 views

R Container for Multiple data.frame with a Brief Description of the Content of the data.frame

For a project I have a large dataset with a multitude of variables from different questionnaires. Not all variables are required for all analyses. So I created a preprocessing script, in which subsets ...
0
votes
2answers
65 views

Algorithm that creates in a systematic form, matrix with zero-one elements

I'm working on Rcpp and I wrote the following code ...
0
votes
0answers
20 views

Is there a way for Rcpp's dpois function where I can send a vector `theta` as param?

In R, the function dpois (the poison distribution) can be used as dpois(x,theta) with both x and ...
1
vote
0answers
37 views

Reimplemented R function(s) is surprisingly slow

I reimplemented the functions https://rdrr.io/cran/fclust/man/RI.F.html, https://rdrr.io/cran/fclust/man/ARI.F.html and https://rdrr.io/cran/fclust/man/JACCARD.F.html of the R package https://rdrr.io/...
0
votes
1answer
48 views

Speed up a function that checks for a sequence

I created a function yes.seq that takes two arguments, a pattern pat and data dat, the ...
1
vote
0answers
49 views

Search rows in dataframe corresponding to a value in R - Performance & Scalability

Overview of the Question I would like to ask for performance/scalability feedback on an R function that I recently wrote. I have a large 4-column dataframe (the <...
2
votes
1answer
57 views

Calculating edges of a social network

I am working on an open Chess data-set (~15500 rows after cleaning), and I create nodes and edges. But the way I create the edges takes a bit of time. A sample of my nodes tibble: player 1 bougris ...
3
votes
0answers
85 views

Was this an idiomatic and prudent way to extend R's matrix multiplication syntax? Should I have used R's object system?

After reading one too many Lisp books, I decided to try extending R's syntax. My goal was to implement repeated matrix multiplication in a way such that I could write ...
1
vote
0answers
97 views

Dplyr friendly function for computing moving average

Background The function computes moving arrange and offers some other minor widgets like possibility to return data frame in original order or automatically derive variable name for the moving average....
2
votes
1answer
44 views

How can I strengthen this function? I want it to give me an estimated date of hearing back from grad school applications

I created a function to give me an estimated date of hearing back from grad school applications using the median length of days between each theGradCafe submission and January 1st. I separated ...
2
votes
0answers
49 views

How to make my code with for/if loop more efficient in R

My code is structured so that I create an ellipse through my first three points of data. Then I go through each next row, and if the point falls outside the ellipse, I record the minimum distance ...
2
votes
0answers
25 views

Implemented my own version of every() from {purrr}

I am new to programming / R and new to writing functions. I am working through the book ...
3
votes
1answer
48 views

Construct list from a dataframe and a formula

A dataframe and a right-sided formula are given: ...
2
votes
1answer
60 views

Unefficent data wrangling in R

so I've been working with R quite a while now, but due to very stressful deadlines in my last university module I had, I wrote code that I'm not very proud of. I would like to be more efficient in ...
2
votes
1answer
36 views

Writing an excel doc with a subset of my data in R. Currently the code is excessively long

I'm fairly new to R. I have used the 'r2excel' package to create an excel workbook from a subset of my R data. This should have 3 sheets for the three different subsets. What I have written works fine,...
1
vote
0answers
23 views

normal distribution line does not show up when plotting [closed]

I am plotting a distribution of 10,000 samples of sum of 100 poisson variables, and overlaying that distribution of the approximate normal distribution. However, the normal distribution line does not ...
3
votes
1answer
63 views

Does this R code look ok to expand out to other sites?

I'm after some constructive criticism on my code and maybe some ways to simplify before I expand it to include other sites. I am new to R and coding and have been working on this problem for weeks now....
1
vote
1answer
30 views

transform columns to binary encoded columns in R

I have some nominal variables encoded as integers (not ordinal), which I would like to encode as binary (not dummies nor one hot!). The following code is what I came up with (adapted from other code I ...
1
vote
0answers
20 views

Improvements on column renaming using a mapping table

I was wondering whether I could use more data.table functions rather than using for loops in the following piece of code: ...
1
vote
1answer
29 views

Cutting continuous variable into predefined blocks with custom labels

Background The function makes use of cut function offered in R's base package in order to "bin" a numeric vector into provided categories and apply, ...
2
votes
0answers
75 views

Using R to Dynamically Query Database Using SQL Template

Purpose of Code The purpose of the main function (get_player_penalties()) is to: Generate simple SQL statements from user input and a template Query a database ...
0
votes
0answers
39 views

Seeking ideas for better efficiently deleting an element in a vector

While experimenting with some algorithm I decided to simulate it in R. I have a number of chronological elements in an array, and I want to prevent unlimited growth as new samples are added. So I ...
2
votes
0answers
23 views

Package function to perform operation on different “sets” of data in same dataframe

The ubiquitous dplyr package has the function mutate_at (and many other *_at()'s), which ...
3
votes
0answers
41 views

Implementing a Hilbert-like space-filling-curve conversion in Rcpp

I have written a series of functions that are designed to create a space-filling curve (similar to a Hilbert Curve) that is intended for symmetric matrices (as opposed to a square region). I plan to ...
2
votes
0answers
41 views

Vectorize a function that, once per row, updates multiple rows in data.table

Requirements I have a table of the different diagnoses a patient has received, with the date the diagnosis was first recorded. It so happens that in medicine, diagnoses are recorded as codes from a ...
2
votes
0answers
68 views

Givens QR decomposition in R or C++: poor performance (or lack thereof) for rectangular matrices

My own Givens-based QR decomposition function in R (pseudocode from Politecnico of Turin's math department) is the following: ...
1
vote
1answer
34 views

Log Likelihood Calculations for Censored Data in R

I am calculating the log-likelihood of data that have some observations censored. For uncensored "a" clusters, the likelihood calculates P(Y=y). For censored "b" clusters, it calculates that Y is at ...
3
votes
1answer
46 views

Ensure that column names in output retain original variable names, instead of using arbitrary X1, X2, etc

Is there a more elegant way to ensure that colnames(output) of a output R dataframe retains the original column variable names ...
1
vote
0answers
62 views

Profile maximum likelihood programming in R

I am calculating a likelihood surface for a modified negative binomial distribution with two parameters: mean R and dispersion k ...
1
vote
0answers
25 views

Keeping groups based on the row count of a second column

I have the following code that aims to filter those pid (1 and 4) that have at least before_threshold (2) rows with a value of <...
1
vote
0answers
37 views

conditional simulation(path dependent simulation) in R

My codes are not good, I want to konw how to solve my problem efficicently and learn writing some elegant R codes.Thanks in advance! "r" is the starting value in ...
4
votes
0answers
36 views

purrr::partial() but keeping function signature

I quite like purrr::partial(), which allows us to easily create partial functions. However, the partial function loses its remaining arguments from its signature (...
4
votes
1answer
52 views

Function for calculating Archimedean density functions

I am not sure whether this is the right place to ask this, so please correct me if it's not. I am following this paper (e.g., Corollary 3.3) for some derived functional representations of the ...
2
votes
1answer
45 views

Better filereading and print formating in R

I am a beginner using R and I am trying to solve some simple statistics problems and print the output. The following code takes in two datasets, computes the mean, ...
5
votes
2answers
164 views

Speeding up string splitting in Rcpp/Cpp

I'm looking to speed up an Rcpp function I have written. The function takes in a string called vcfield, which takes the format od ...
1
vote
1answer
55 views

Statistical sampling from a vector

I have written a function/script to simulate data for sample size estimation. The following code samples from a vector of generated values with varying numbers of sample sizes and then concatenates ...
2
votes
3answers
51 views

Flagging system: Check for changing values in a data frame

I've written the following code to check if a value in a data frame changes. I'm looking at the last 5 values. If there was no change at all I want my code to return 1, if a single one (or multiple) ...
1
vote
0answers
51 views

code plots - spatial data

I am an absolute newcomer in R. For my bachelor thesis I worked with spatial data. I did many plots with the normal "points"-function. I already have my results, but would like to improve my code. ...

1
2 3 4 5
8