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
7
votes
1answer
73 views

error-specific tryCatch

R doesn't have classed errors, just (for the most part) error and warning, so determining if the error should be caught or ...
2
votes
0answers
82 views

Efficient way to fill 2D matrices by rows in a list using R

I have a list of 2D matrices. Each matrix is filled using the function “fillMatrices”. This function adds a number of individuals to each day 0 in a matrix and updates the columns “a_M”, “b_M” and “...
2
votes
1answer
192 views

Merge dataframes on timestamps and time intervals using data.table in R

I have two dataframes (x and y) that I need to join, conditional on the timestamp in ...
1
vote
2answers
76 views

Generate a large block diagonal covariance matrix with exponential decay

I am implementing Kalman filtering in R. Part of the problem involves generating a really huge error covariance block-diagonal matrix (dim: 18000 rows x 18000 columns = 324,000,000 entries). We denote ...
2
votes
1answer
72 views

Group by consecutive values in first column and second column value

I have a typing log in which rows are grouped by consecutive package_name, and within those groups rows are grouped if they are logged within 180,000 ms from each ...
2
votes
1answer
31 views

dput() concatenated vector with no quotes, separated by commas in R

An oddly specific task - I have a dataframe with columns with values I need to concatenate in order to prepare a query for a website that requires very specific inputs: unquoted strings, separated by ...
1
vote
2answers
52 views

Replace values in R data frame according to values in other data frame

I would like to change row names values from a first data frame if these rows names are present in another data frame, and change it to a corresponding value (defined in the second data frame). I ...
4
votes
1answer
59 views

Generating random sparse matrix

My goal is to generate a large sparse matrix with majority (~99%) zeros and ones. Ideally, I would be working with 10,000 rows and 10,000,000 columns. Additionally, each column is generated as a ...
1
vote
1answer
27 views

Two-Part Hurdle Model Simulation with Bootstrapping

I am running a simulation study where I am using two-part hurdle modeling on different effect sizes. I tried to stream-line my code for the best optimization including foreach loops and ...
2
votes
0answers
68 views

Bias-corrected percentile Bootstrapping Optimization

I have never posted to this forum, but was referred here. I need some help printing key iterations as well as optimizing my code for run time. If someone had some time to help/explain how to correct ...
3
votes
1answer
50 views

Extracting small word parts (quadgrams) from text using R

I would like to extracts small word parts (quadgrams) from text. Example: hello world results in: ...
5
votes
1answer
72 views

Manually writing a function matrixpower() in R

Problem 1 - Matrix powers in R R does not have a built-in command for taking matrix powers. Write a function matrixpower() with two arguments mat and k ...
2
votes
1answer
46 views
5
votes
1answer
74 views

Passing uint64_t from C++ to R: Hilbert Mapping - xy2d function

I have been working with Rcpp to perform a forward and backward Hilbert Mapping. Below is an implementation based on this code. My application is in genomics and I may be dealing with enormous ...
3
votes
1answer
39 views

Sample size, P-values (its relationship), and data visualization with plots. ggplot and T test

We know that P-values (within t-test context as an example..) is highly sensitive to sample size. A larger sample will yield a smaller p-value remaining everything else constant. On the other hand, ...
4
votes
3answers
60 views

Fill missing value based on ID

Say that I have running id from 1 to n, and a value column: ...
3
votes
2answers
66 views

Finding values below a threshold

I am wondering if it's possible to avoid looping. I want to make the function faster. The function is to find how many times a particular value is lower in the overall data. For example: ...
0
votes
0answers
50 views

Text2Vec - fit_transform, $transform TF-IDF

I am sorry if this is a wrong place to post a question. I have two questions: ~ A) I am trying to do text similarity using TF-IDF cosine similarity. I somehow managed it in the fallowing code, ...
3
votes
1answer
143 views

Discrete event simulation for production defects

I have one machine, which produces parts. In machine_failure_rate% it produces faulty parts which need to be produced again. Thus, we end up with a simple queuing ...
3
votes
1answer
82 views

Plots of prior and posterior distributions for a model

I have an exercise that ask to plot the prior and posterior distribution in the Poisson-Gamma model. I did it (I think it's correct) inspired in the answer of this question https://stats.stackexchange....
2
votes
1answer
59 views

A faux parser for fdf files

Introduction If you know what FDF files are, you can skip this section. PDF files sometimes includes form fields. These fields can be represented as FDF in plain text form, extracted using certain ...
1
vote
1answer
70 views

Using findInterval to assign vector elements to corresponding brackets

The following function receives a vector of positive integers and list of brackets. The function returns a data frame with a logical vector for each bracket indicating whether a given element of a ...
0
votes
1answer
144 views

Calculate decile table with some loop in R

I wrote code which calculates: Decile threshold for every decile group Total income in the decile group Number of persons Share of decile in total income (%) Tax Share of tax (%) But unfortunately I ...
1
vote
1answer
74 views

Simulating data generating process correctly

My aim is to simulate the following model by means of a Monte Carlo simulation. I wonder if my R code is correct for generating the data. Could somebody check? The model: $$Y = \sum_{j=1}^{100} (...
1
vote
1answer
76 views

Get maximum sequence lengths taking into account the tradeoff of omitted observations

From a data frame d I have to select the longest possible sequences of x. variables. Example: ...
-1
votes
1answer
53 views

R Regex : Need only numeric figures [closed]

I'm working on R and using regex to keep only numeric figures in a column (I need to keep digits, negative and decimal sign). Suppose I've a value ...
2
votes
1answer
66 views

dplyr — tidyverse, pipes, correlation, same variable across time “a” “b”

Have a look at this dataset. The"a" and "b" are used to make possible differentiate when the same variable was measured. In this case X1a and X1b access the same variable, but "a" was (suppose..) in ...
1
vote
1answer
203 views

Using dplyr and pipes for logistic regression plotting

I have successfully created a plot of a binomial glm using example data. https://sciences.ucf.edu/biology/d4lab/wp-content/uploads/sites/125/2018/11/parasites.txt The predictors of the model include ...
3
votes
1answer
80 views

Applying a function to each row of a matrix

My goal is to apply a function func1 to each row of the matrix input and then return a new one resulting from the transformation....
1
vote
1answer
58 views

Speed up computation on lists of lists in R (scalability issues)

My goal is to take a source list of vectors and apply a mapping to each vector. The mapping creates a list of lists. When the vector size in each list element is large (~20000), and the number of ...
1
vote
0answers
37 views

Cluster correlations for many (> 500k) features by threshold

I have a matrix with roughly 500,000 features that I'd like to correlate and find features that have correlations >= 0.90, then cluster these into one group. Is there an efficient algorithm to do this?...
3
votes
1answer
91 views

A series of yes-no questions to predict yes-no outcomes

I have been writing R scripts for about a week. I have been writing code for 20 years. I have a series of yes-no questions that I hope to predict yes-no outcomes so I know the R-squared will be ...
1
vote
1answer
66 views

Radix sort function in R

I know that sort in R uses radix sort, and I played around in implementing it (mainly to be sure I understood the algorithm properly). However, my implementation is ...
1
vote
1answer
66 views

Transform wide dataset into “tidy-data” within R

I have some really wide data in R that I want to transform into the "tidy data" format. Unfortunately, I cannot change the process that generates this data, so it must be done within R. My raw data ...
3
votes
1answer
163 views

Effective 3d array transposition with R/C++ (Rcpp)

Task is reshape 3d array from [row, col, slice] to [slice,row,col]. I tried implement base::aperm analog with Rcpp. ...
1
vote
1answer
35 views
1
vote
2answers
67 views

Speed up sorting algorithm in R: make one column “smaller” than the other

Consider the following sorting algorithm: ...
1
vote
1answer
312 views

Character pattern replacement using gsub, loops, and data.table

I was trying to see if data.table could speed up a gsub pattern matching function over a list. Data for reprex. It's a list of ...
1
vote
2answers
68 views

Stitching together badly formatted log files

I needed to stitch together a large number of badly formatted log files. I [painfully] developed a script for it in R, one that successfully dealt with the various flaws in the dataset. However, this ...
1
vote
1answer
142 views

Efficiently sample a single row of an array at random

I have the following C++ code snippet that randomly samples a single row in an array called pop with num_specs columns and ...
1
vote
2answers
86 views

Checking the presence of certain variables in a dataframe

I'm writing an Exploratory Data Analysis using R Markdown. First of all, I need to check the "sanity" of the input data set. Among the other check I perform, I need to check if at least one between ...
2
votes
1answer
66 views

Iteratively simulate a parasite population

I'm trying to build a simulation in R to model the effects of different parasite egg laying behaviors on how many offspring the parasite has, given a limited amount of resources. In brief, this ...
2
votes
1answer
65 views

Read a directory of CSV files in R

I'm writing a lot of R in my spare time and am trying to get into good habits by using TidyVerse as much as possible. A large part of this has required reading entire directories of dated log files ...
2
votes
1answer
60 views

Fast algorithm for any(M==2)

I want to find a quick way to see if a matrix M has at least one value that is, say, 2. In R, I would use any(M==2). However, this computes first ...
4
votes
1answer
284 views

Group-wise deviations from means / centering with dplyr vs. data.table

I am trying in R to "center" a matrix (i.e. remove mean) group-wise (i.e. remove group means for each variable). Input is a matrix (including the grouping variable), and output is the same matrix, ...
1
vote
1answer
29 views

Creating the design matrices for a canonical correlation analysis

I have this code to generate symmetric matrices for testing how the design of the canonical correlation analysis I am performing works out. This is a extension of this solution Each row of the matrix ...
6
votes
1answer
77 views

Random Weighted Classifier in R

I am computing a random weighted classifier based on the rates at which 3 labels appear in a "train" set. I want to use this RWC as a baseline for other classifiers. I'm doing this over 1000 ...
1
vote
1answer
59 views

Extract numerics from matrix A with logical matrix B

I have a matrix A containing numeric values and a matrix B containing 0/1s: ...
0
votes
1answer
68 views

Is there a more efficient way of doing function 'which' in R?

The data file is here. Here are my codes: ...
0
votes
1answer
44 views

Using R to transform Excel files to be combined

I have 50+ excel files that I need to transform (and eventually combine) to use in analyses. I’ve written a code that transforms them into the format I need, but it is extremely slow for files with ...