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

learn more… | top users | synonyms

5
votes
1answer
39 views

Scraping comments from Reddit

I'm scraping some comments from Reddit using Reddit JSON API and R. Since the data does not have a flat structure, extracting it is a little tricky, but I've found a way. To give you a flavour of ...
-1
votes
0answers
8 views

I'm not able to perform LSI in R [closed]

This is the text I want to perform LSI on This text is “memoirs of Sherlock Holmes” Please download the text from http://www.gutenberg.org/cache/epub/834/pg834.txt My aim is too Apply LSI on the ...
4
votes
2answers
61 views

Simplify calculation of mortgage repayments in R

I've just started learning R, but I feel that I'm using it only as a normal procedural language without taking advantage of some built-in functions and data structures. I wrote a function that ...
4
votes
0answers
39 views

Vectorize Fisher's Exact Test

I have two data frames/ lists of data, humanSplit and ratSplit, and they are of the form ...
6
votes
1answer
49 views

Generating Cartesian product of strings in R

Sometimes I need to create character vectors like this one: ...
5
votes
1answer
41 views

Mean of many subsets of a dataframe

I have large dataframe containing many replicates. The replicates are in groups of 3. So the first 3 replicates are in column 1, 2 and 3. The second set 4, 5 and 6... and so on. Now I create a new ...
2
votes
1answer
58 views

Data model code running too slowly

The model takes a long time to run. I have tried to vectorise it as far as I can, but it's still taking about a day to compute. I'm wondering if it's possible to speed it up. Maybe the problem isn't ...
5
votes
0answers
86 views

Merging overlapping/intersecting string vectors

I am trying to merge overlapping/intersecting sets given as a list of string vectors below in R (my actual data set has thousands of such sets). The overlap/intersection is based on the ...
2
votes
1answer
24 views

Comments to speed up code

I wrote an R code that basically performs 2 operations: 1) For each segment in file A, find all segments in file B that lie in that segment 2) Find percentage of overlap for each case in previous ...
2
votes
0answers
41 views

Speed up my backward model selection

I have implemented a quite long program in R and I have noticed that one block, concearning a backward model selection, is too slow and I would like to receive some suggestions in order to speed up ...
4
votes
2answers
56 views

Imperative code in R keeping track of state

A friend of mine has a spreadsheet where she coded a classroom video for various teaching events, which I needed to help her plot. For each recording, she used several video cassettes, and her time ...
3
votes
1answer
141 views

Replace for loop while with apply family function in R

I have trouble with for loop, my code runs very slowly. The thing I want to do is to use function from apply family to make my codes run faster (instead of using ...
2
votes
0answers
33 views

Merton default model with complex iterative approach

I am using Merton default model with complex iterative approach. I have already prepared my R codes but as I am quite new in R, they seem very inefficient, in sense that they runs almost 7 hours. My ...
0
votes
0answers
34 views

Create a simulation study using R to compare the accuracy (95% capture rate) and the lengths of the Chi-Squared confidence intervals

The minimum length confidence bounds for a normal population variance for chi-squared distribution is $$ g(a) = a ^\frac{n+1}{2} e^{-\frac a 2} - b^\frac{n+1}{2} e^{-\frac b 2} = 0. $$ Are they ...
2
votes
1answer
78 views

Backwards stepwise regression code in R (using cross-validation as criteria)

I want to write code that does backward stepwise selection using cross-validation as a criterion. I have only started learning R a month ago and I have almost zero programming experience prior to ...
0
votes
2answers
75 views

Optimizing the function in R by replacing for loop

I am trying to create a function that I want to use in my real data. Is there any way to optimize the following function? This is a function I created to filter the data. ...
2
votes
1answer
298 views

best way to apply across an xts object

As xts objects are arrays, getting apply functions to work is a little tricky if you want to preserve the dates. For example, take the ...
2
votes
1answer
46 views

finding names of un-named list elements

I have a naming problem: lists with un-named elements. I wish to get the names of the elements in the list, without going back upstream to where the list was created. An example is ...
3
votes
1answer
81 views

Simplifying loop for Incidence Matrix

I had to work in the Transport Problem and we're asked to try to make the fastest code for big matrices, so we're advised to try to avoid loops. And the only one I've got and I cannot imagine how to ...
2
votes
1answer
97 views

Basic graphical R code draws an ellipse instead of a circle

I wrote some very basic R code. This has at least two problems. It uses a very basic imperative-programming style instead of good R style. It draws the result as an ellipse, not a circle. This ...
2
votes
2answers
131 views

Should I be trying to imitate the control flow in this example R code?

I have heard that some patterns in R code are really anti-patterns and thus I am trying to imitate superior examples of R code. I am confused about control flow. I have been reviewing some R code ...
2
votes
1answer
61 views

Check column values are in different sets

I have a data frame called data with lots of columns, and Country is one of them. I created a new logical column called ...
1
vote
2answers
145 views

Learning R – repeated measures simulator optimisation

I am learning to program in R and to do that and create something useful in the process, I have decided to rewrite this Java applet for repeated measures simulation and to implement some new ...
3
votes
1answer
139 views

Vectorize or otherwise speed up a double for loop

I have this function containing some loops and a double for loop. What it does is set up a matrix first to store results in (the task at hand is comparing genomes) and then with sliding window, ...
1
vote
1answer
76 views

Which components of this r loop are inefficient?

I realize that there is a surfeit of posts on SE/SO about optimizing one's R for() loops. I'm afraid, though, that I don't understand all of the guidance in the answers there, or how to apply their ...
3
votes
1answer
130 views

More elegant filter script in R

I'm very new both to programming and to R so please bear with me :-) I've written the following bit of code, which works perfectly well and runs through a data file with 17446 rows in about 35 ...
1
vote
2answers
101 views

Is this correct R? remove first duplicate dataframe

I have a dataframe with several columns. One of them is an user id column, in this column, I have several ids that can be repeated several times. What I want to do is remove the first id, for ...
4
votes
0answers
220 views

Speed up a monte carlo simulation with nested loop

I would like to know if there is a more efficiency way to speed up below code. It uses a procedure where subsampling is required in the nested loop (which a previous answer helped to make more ...
3
votes
1answer
387 views

Compute intersections of all combinations of vectors in a list of vectors in R

My goal is to compute the intersections of several vectors (sets of identifiers, gene-names to be specific). I start with a list of vectors and run the function below, which loops through 1:n where n ...
2
votes
1answer
154 views

Increase efficiency for an R simulator of the Monty Hall Puzzle

The Monty Hall Problem is a simple puzzle involving probability that even stumps professionals in careers dealing with some heavy-duty math. Here's the basic problem: Suppose you're on a game show, ...
3
votes
2answers
57 views

Speeding up for-loop over a list

I have two lists with ca. 4000 elements where each each element have two columns. These are being fed into a function. Besides the function they are being fed into, is it possible to speed it up ...
3
votes
1answer
141 views

R code is slow on filling a matrix

I am running an R code, which is implementing a Markov Chain Monte Carlo algorithm for updating a density distribution There is a specific section of my code which tries to fill a very large matrix ...
2
votes
1answer
125 views

Speed up a sampling function

I have the following dataset kkk<-data.frame(days=1:100,positive=rbinom(100,1,0.05)) Over the monitoring period of 100 days, if an event occurs then for that ...
2
votes
1answer
77 views

How do I speed up the following R code?

I have files containing data set which contain 11,000 rows. I have to run a loop through each row, this is taking me 25minutes for each file. I am using following code: ...
1
vote
1answer
151 views

increase speed for 'for loop'

I have files containing data set which contain 11,000 rows. I have to run a loop through each row, this is taking me 25minutes for each file. I am using following code: ...
3
votes
2answers
161 views

How would you improve this code in R?

I'm trying to implement a function that given a data frame returns the same data frame with four columns added. These new four columns are: for each row, I get the maximun element and its index and ...
1
vote
1answer
447 views

Improving R script performance

i am having some performance problems with a little R script of mine that I use to visualize simulation results of a project of ( mine ). It now takes longer on my machine to run the R script than the ...
3
votes
2answers
113 views

Improving R code - remove redundant if's and assignments

I've written this brief function, it's to do with calculating which calendar year a student would be in Year 1 based on their DOB. In 1998 there was a change in the DOB cut points for starting Year 1, ...
-1
votes
1answer
369 views

Optimize C++ Code

I have the following C++ function which I'd like to speed up, if possible. This program will have many different users, so parallelization is not really an option. The C++ function is wrapped by C ...
5
votes
2answers
762 views

My R code runs very slowly, how to improve?

I wrote the following code in R, basically I want to convert all categorical column in a R dataframe to several binary columns: for example, a categorical column like ...
2
votes
1answer
193 views

Can anyone speed up this R function?

This script fetches a list of "end-positions" from a MYSQL database. The positions are array positions that are the same in both MYSQL and R. Next, the script takes the previous 34 elements for each ...
3
votes
1answer
543 views

Generate graph in Matlab

I use this Matlab code: ...
1
vote
1answer
55 views

Is there a more efficient way to simulate in R, by exposition factor, gender and disease profile?

I started working with R recently and need to do a simulation for 100 000 people that divides them in exposition factor for a disease, gender and presence or absence of disease. My background info is ...
3
votes
1answer
124 views

What is the best approach to use in R and why?

I'm starting working with R, and found some tutorials and exercises online. I want to divide one variable in two, bigger and equal 79 and smaller 79. Perhaps because I'm used to python, my first ...
4
votes
2answers
179 views

Is there a better way to get the index of a minimum?

Consider the following reproducible example: ...
3
votes
1answer
129 views

How can I get this R code to pass the “1 minute” test for this Project Euler question?

I am attempting to learn R programming by going through the questions in Project Euler. The code below is my solution to problem 5 which asks for the smallest number that is evenly divisible by 1 ...
2
votes
1answer
192 views

Making this code more R-like

I've written the code below to do some work on machine learning in R. I'm not overly happy with some bits of it, and I suspect I could improve it quite a bit. Bits I'm specifically interested in ...
3
votes
2answers
782 views

How to simplify nested conditional for loop in R?

I have this for loop for creating a list of variables: ...