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

learn more… | top users | synonyms

3
votes
2answers
34 views

Reducing memory usage for FizzBuzz in R

I've been trying all night to get my fizzbuzz to use below 20 MB of RAM but I can't seem to get it much smaller than this. ...
2
votes
1answer
32 views

Fast canonical indexing of elements in array

Given two arrays of elements: aaabbccdefaa zzzccddafgzz I want to check whether these two arrays are equivalent up to a relabeling. My strategy is to map them into a canonical form and ...
0
votes
0answers
67 views

Time-course of drug amounts in the different compartments of a pharmacokinetic system

I am looking for a way to speed up applying my function to every individual in the data frame. Currently I am using the ddply but it is very slow for big data sets. ...
2
votes
0answers
77 views

Genetic algorithm for the minimum of the Schaffer function

I have to implement a genetic algorithm in R to optimize the minimum of the Schaffer function. Below I describe each step and I need to create one big function and another loop for 100 iterations (and ...
0
votes
0answers
58 views

Stochastic gradient descent in R

Please, review my code, which counts global minimum with method stochastic gradient descent for schaffer function ...
4
votes
1answer
86 views

Simulated annealing in R

I am new in R and I have to implement simulated annealing for schaffer function and I did it. However I am not sure about the correctness of the code. ...
1
vote
0answers
59 views

Naive Grouping for Factorization

I have a naive grouping method for factorization. I am curious as to its novelty and aspects of the code below that will increase its efficiency. The method is best described with an example: For n ...
3
votes
1answer
97 views

Trial Multiplication for Factorization

I am trying a trial multiplication for factorization. This is a general example, but each sequence can be specified to pairs of sequences \${p,q}\$ for a given \$n\$. For example, if \$n\$ ended in 3, ...
2
votes
0answers
31 views

Parallelize (or otherwise speed up) a double loop over a symmetric matrix [duplicate]

I wrote a non-vectorized, non-parallel wrapper function for computing the pairwise distance covariances (Wikipedia) (original paper) in a data frame: ...
6
votes
2answers
49 views

2D lattice random walk plots in functional style

To practice writing code in the functional programming style, I wrote a program to plot two-dimensional lattice random walks. I'd appreciate any feedback about how to improve its "functionaliness". ...
4
votes
0answers
48 views

Crawling and parsing meteorological data from the web into R

I am interested in collecting directly into R data published by the Mexican Met-office. The data pieces are spread through several URLs, but one can start here. There I can get the names and ...
0
votes
0answers
44 views

Extract and rename specific columns of a CSV data file

I have a CSV data file with 3,896 columns (variables), and I am attempting to extract a particular subset of columns for use in a data analysis project. To avoid the burden of having to constantly ...
6
votes
1answer
53 views

Lookup GINI by year and country

This takes a dataframe as an argument, looks at all pairs of two columns in that data frame (country and year) and returns a column value of that pair in another data frame (the gini number). ...
4
votes
1answer
99 views

Sublime Text 2 R Build System (source file, send selection to interactive session)

The following is the description of the R build system I am using (very happy) with Sublime Text 2 on linux (Crunchbang). With it, I am able to: to source the current file.R to an interactive R ...
2
votes
0answers
90 views

Smoothing matrix for visual representation

I recently started playing around with R and am currently trying to plot GPS data points onto a perspective plot. I have a script that works and I get my desired results out of it and it works ok for ...
3
votes
0answers
33 views

Volatility updating with Heston-Nandi model

I'm trying to program a volatility updating rule using iteration. I start with the well-known Heston-Nandi model where the returns dynamics are: $$ \left\{ \begin{array}{rcl} R_{t+1} ...
1
vote
1answer
99 views

Quick estimation for empirical permutation and correlation test in R

I have two matrices (a, b), each including 3 genes and 6 observations. I have estimated the Spearman correlation for gene pairs (rcorr.observed). Then I want to do an empirical permutation (1000 ...
3
votes
2answers
63 views

Python path-breaking script for use in R

R does not support line continuation as Python, and it's disturbing to have long file paths like /media/user/data/something-not-very-important-but-super-long/some-curious-secret-file.pdf and ...
4
votes
1answer
48 views

Creating a special Toeplitz matrix in R

I'd like to know if there is a standard way of creating a matrix like the one shown below using the R programming language. $$ \begin{bmatrix} 1 & \rho & \rho^2 & \cdots ...
4
votes
2answers
98 views

Concisely adding values in a loop to a column

In my data frame I assign values to the column Register by checking another column (Source) for specific values. Based on those values, the output in Register changes. I am relatively new to R, but I ...
3
votes
1answer
57 views

Simulation of animal skins

This is a simulation of animal skins, like a cellular automat. I'd like it improved. ...
1
vote
0answers
27 views

Comparison of two regression types on the same set of models

I wrote a relatively simple R script to run a bunch of regressions. To be precise, it is a hurdle negative binomial regression (fancy stuff for count data). To print it out easily to ...
3
votes
1answer
31 views

Merging predicted values into measured values

I created the following function below to merge the real values with the predicted values (when real are absent) in a new column in data.frame. The function ...
1
vote
3answers
96 views

An R function for checking whether a variable is binary or continuous

This function takes a numeric vector and returns "con" if it's continous and "bin" if it's binary. It does not take the multinomial case into account, i.e. if a variable y has three possible values ...
4
votes
1answer
71 views

Speeding up nested for-loop analysis code

I have two matrices, the first one is mat 2000*500, the second one is 6000*500 ...
1
vote
1answer
48 views

Frequency form to case form - unexpected values in table

I'm taking count data on voting preferences by race and entering it into a table in frequency form, then expanding out to case form and using CrossTable to look at ...
3
votes
0answers
64 views

Finding the Cox regression coefficients in a mixed model for microarray data

I have written a code for a project which aims at finding the Cox regression coefficients in a mixed model for microarray data. The study was carried out on the Affymetrix Hgu133a platform. In the ...
5
votes
1answer
98 views

Vector comparison inside for-loop

The following code finds no-rain periods in a time series that were preceded by rain over a threshold (e.g. 10mm in 2 days [k=48]). ...
18
votes
2answers
174 views

Plot daily time spent on codereview.stackexchange.com

This draws an xyplot from csv data obtained from RescueTime API for activity time for "http://codereview.stackexchange.com". ...
-2
votes
1answer
51 views

How I can generalise this code

I have this code and I want to generalize it and make it efficient. Each time I generate a number of random variables and I put it in the m equation to generate m. Can I get any help to simplify this ...
2
votes
0answers
28 views

Computing values with several large numeric vectors

I have several large numeric vectors (close to 1e6 entries each) and I need to make some computations with their values: ...
5
votes
1answer
174 views

Using Reddit API in R

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 ...
4
votes
2answers
133 views

Calculation of mortgage repayments

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
67 views

Vectorize Fisher's Exact Test

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

Generating Cartesian product of strings in R

Sometimes I need to create character vectors like this one: ...
5
votes
1answer
221 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
71 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 ...
6
votes
1answer
162 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 ...
3
votes
1answer
101 views

Calculating overlap of segments in chromosome data

I wrote an R code that basically performs 2 operations: For each segment in file A, find all segments in file B that lie in that segment. Find the percentage of overlap for each case in previous ...
2
votes
0answers
42 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
68 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
389 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
41 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
40 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
120 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
195 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. ...
3
votes
1answer
1k 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
55 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
141 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
120 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 ...