r

What's the best way to use R scripts on the command line?

It's very convenient to have R scripts for doing simple plots from the command line. However, running R from bash scripts is not convenient at all. The ideal might be something like #!/path/to/R ... or #!/usr/bin/env R ... but I haven't been able to make either of those work. Another option is keeping the scripts purely in R, e....

How can a function paramater be used without mentioning it in the function body?

I've been trying to learn more about R (and writing C extensions) and I thought it might help to read the source for some well known packages. I decided to start with rpart which is defined as: rpart <- function(formula, data, weights, subset, na.action=na.rpart, method, model=FALSE, x=FALSE, y=TRUE, parms, control, cost, ...) ...

Emacs ESS Mode - Tabbing for Comment Region

I am using the Emacs-Speaks-Statistics (ESS) mode for Emacs. When editing R code, any comment lines (those starting with #) automatically get tabbed to the far right when I create a new line above it. How should I change my .emacs.el file to fix this? For example, I have: # Comment Now, after putting my cursor at the beginning of t...

Suppressing or setting CreationDate/ModDate in R pdf output

When R creates PDFs using pdf() it includes a CreationDate and a ModDate in the PDF. I have a number of such generated PDFs in an svn repository and the effect is that when figures are remade by R, even with the same data, they appear as modified (rightly so) to svn. What's the best way to get the two to play nicely together? I could ...

In R, what is a good way to aggregate String data

In R (or S-PLUS), what is a good way to aggregate String data in a data frame? Consider the following: myList <- as.data.frame(c("Bob", "Mary", "Bob", "Bob", "Joe")) I would like the output to be: [Bob, 3 Mary, 1 Joe, 1] Currently, the only way I know how to do this is with the summary function. > summary(as.data.frame(myL...

Plotting Simple Data in R

I have a comma separated file named foo.csv containing the following data: scale, serial, spawn, for, worker 5, 0.000178, 0.000288, 0.000292, 0.000300 10, 0.156986, 0.297926, 0.064509, 0.066297 12, 2.658998, 6.059502, 0.912733, 0.923606 15, 188.023411, 719.463264, 164.111459, 161.687982 I essentially have two questions: 1) How do I p...

Declaring a Const Variable in R

I'm working in R, and I'd like to define some variables that I (or one of my collaborators) cannot change. In C++ I'd do this: const std::string path( "/projects/current" ); How do I do this in the R programming language? Edit for clarity: I know that I can define strings like this in R: path = "/projects/current" What I really wa...

How to link with static libraries when building an R package...

I'm creating a package that is going to be used by R (the statistical program), I'm not an expert using this application but I have managed to create a very simple package, using the following logic, I have some classes in C++, as the code has to be compiled using the R compiler and it only allows C code, I have a wrapper C code that cal...

Regex group capture in R

In R, is it possible to extract group capture from a regular expression match? As far as I can tell, none of grep, grepl, regexpr, gregexpr, sub, or gsub return the group captures. I need to extract key-value pairs from strings that are encoded thus: \((.*?) :: (0\.[0-9]+)\) I can always just do multiple full-match greps, or do some...

Determining distribution so I can generate test data

I've got about 100M value/count pairs in a text file on my Linux machine. I'd like to figure out what sort of formula I would use to generate more pairs that follow the same distribution. From a casual inspection, it looks power law-ish, but I need to be a bit more rigorous than that. Can R do this easily? If so, how? Is there somet...

Weighted slope one algorithm? (porting from Python to R)

I was reading about the Weighted slope one algorithm ( and more formally here (PDF)) which is supposed to take item ratings from different users and, given a user vector containing at least 1 rating and 1 missing value, predict the missing ratings. I found a Python implementation of the algorithm, but I'm having a hard time porting it t...

Static Variables in R

I have a function in R that I call multiple times. I want to keep track of the number of times that I've called it and use that to make decisions on what to do inside of the function. Here's what I have right now: f = function( x ) { count <<- count + 1 return( mean(x) ) } count = 1 numbers = rnorm( n = 100, mean = 0, sd = 1 ) fo...

Line functions in R

Hi, I was wondering if it was possible to graph three lines in R using fuctions. For instance, how could I get the functions: 3x+1 4x+2 x+1 to show up on the same graph in r? Thanks so much! ...

Which IDE for R in Linux?

What good IDE's are there for R in Linux? I've tried Rcmdr and Eclipse, but neither seems to have the same usability as Tinn-R in Windows. Are there any other options? ...

Emacs and ESS: Adding "Other" Processes

I have installed R-2.9.1 and I am using Emacs+ESS, but when I start an R process, the version of R is 2.6. I thought maybe Emacs was running R from a weird starting directory. However, if I select my home directory, for example, ESS still starts R 2.6. (Running R at the terminal correctly brings up version 2.9.1.) How do I add a new E...

R: How to add variable key/value pair to list object?

R newbie question: I have two variables, key and value, and I want to add them as a key/value pair to a list: > key = "width" > value = 32 > mylist = list() > mylist$key = value The result is this: > mylist $key [1] 32 But I would like this instead: > mylist $width [1] 32 How can I do this? ...

Is there any emacs mode for Rscript?

Is there any usable emacs mode for Rscript? (Rscript is the script front-end for the R language.) If I type this: #!/usr/bin/Rscript print(commandArgs(TRUE)) and do indent in the ESS R-mode it indents the first line like crazy, since it sees it as a comment: #!/usr/bin/Rscript print(commandArgs(TRUE)) ...

How do you print to stderr in R?

How do you print to stderr in R? This would especially useful for scripts written in Rscript. ...

Is there a package to process command line options in R?

Is there a package to process command-line options in R? I know commandArgs, but it's too basic. Its result is basically the equivalent to argc and argv in C, but I'd need something on top of that, just like boost::program_options in C++, or GetOptions::Long in perl. In particular, I'd like to specify in advance what options are allow...

Creating an adjacency list from a data.frame

I have a data.frame with 2 columns: Node A, Node B. Each entry in the frame implies an edge in a graph between node A and B. There must be a nice one-liner to convert this data.frame into an adjacency list. Any hints? ...