Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
The results are in! See what nearly 90,000 developers picked as their most loved, dreaded, and desired coding languages and more in the 2019 Developer Survey.

Questions tagged [r]

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

2
votes
1answer
35 views
5
votes
1answer
59 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
31 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, ...
3
votes
2answers
38 views

Fill missing value based on ID

Say that I have running id from 1 to n, and a value column: ...
3
votes
2answers
59 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
31 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
56 views

Producing a Pandas Data Frame from Long to Wide format Efficiently

I have a time series data in long format which looks like as follows: ...
3
votes
1answer
105 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
74 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
41 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
47 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
55 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
72 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
73 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
51 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
44 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
109 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 ...
2
votes
1answer
71 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
49 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
33 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
90 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
62 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
63 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
114 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
2answers
66 views

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

Consider the following sorting algorithm: ...
1
vote
1answer
170 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
62 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
126 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
84 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
65 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
61 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
59 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
279 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
25 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
74 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
56 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
66 views

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

The data file is here. Here are my codes: ...
-1
votes
1answer
43 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 ...
3
votes
2answers
61 views

Summations zu.mpfr and zstar based on a probability sample

Here is my code: ...
5
votes
1answer
62 views

Plain C implementation of 'Cumulative All' operation for R

I want to implement a cumulative all through R's C interface. I was able to hack something together that produces the correct results, but I have little idea what idiomatic C should look like. The ...
0
votes
1answer
77 views

R Function to Generate Predictions from Ratings and Save Results

I am trying to generate predictions using the function below in combination with sapply. My actual datasets are very large, I am attempting to make 1.5 million predictions with this function and it is ...
1
vote
1answer
89 views

R function to generate predictions from ratings

I am trying to improve the run time of a program I wrote in R. Generally, what I am doing is feeding a function a data frame of values and generating a prediction off of operations on specific columns....
2
votes
1answer
86 views

Average calculation

I'm testing the influence of the time period on the end result of my calculations. To do this, I'm using a for loop, that is detailed below. In the current state of ...
0
votes
1answer
47 views

Function to calculate Persistence Rate with optional group_by variable and logical arguments

The Function "Persistence" is sometimes also referred to as "retention". It is defined as the number of units (ID's) in a given term/period that are also found in the subsequent term/period. So, if I ...
3
votes
0answers
33 views

Function to calculate “Persistence Rate” by group with a logical argument to determine whether to include overall persistence

The Function "Persistence Rate" is also sometimes referred to as "retention rate". It is the number of ID's in a given term/period that are also found in the next term/period. (e.g., I have 10 ...
4
votes
2answers
150 views

Vectorizing a complex nested for loop in R (running models on different subsets of a data set, subsetting the data differently for each loop)

This is my first time posting so hopefully I manage to get my question across in a clear and concise manner. I'm not sure this is the right place but someone on StackOverflow suggested I ask here. I ...
4
votes
1answer
46 views

Streamline R code for Shiny usage

I have the following code to create a table for overall percentages and the past 30 day percentages. I would like to add this to a Shiny app I'm making, but I feel there is a simpler or shorter way ...
1
vote
0answers
69 views

Predicting Disaster (Titanic)

Intro I have started a new course (Analyzing Big Data with Microsoft R) and have an exam soon. So I wanted to test my skills, and a nice way to do this was by doing a Kaggle competition Titanic: ...
1
vote
0answers
76 views

Euclidean distance of two matrices

I am writing codes to have the Euclidean distance of two matrices and then storing them in a matrix. Could anyone please review this? ...