r

Linear Regression and group by in R

I wan to do a linear regression in R using the lm() function. My data is an annual time series with one field for year (22 years) and another for state (50 states). I want to fit a regression for each state so that at the end I have a vector of lm responses. I can imagine doing for loop for each state then doing the regression inside the...

SQL-like functionality in R

I am used to writing data manipulation logic in SQL and now that I am learning R I find myself sometimes just wanting to do something that would be simple in SQL but I have to learn a bunch of stuff with R to do the same manipulation on an R data frame. Is there a simple work around? I'm considering writing to a database, doing my mani...

Large loops hang in R?

Suppose I want perform a simulation using the following function: fn1 <- function(N) { res <- c() for (i in 1:N) { x <- rnorm(2) res <- c(res,x[2]-x[1]) } res } For very large N, computation appears to hang. Are there better ways of doing this? (Inspired by: https://stat.ethz.ch/pipermail/r-help/2008-February/155591....

Differences between R and S

What are the differences between R and S? ...

How to increase the number of columns using R in Linux

This may seem menial, but it affects my productivity. I am using R in terminal mode on Linux. Unlike the Windows IDE, Linux limits the number of columns to 80, thus making harder the inspection of data sets. Is there a way to set the max number of columns? ...

Recommendations for Windows text editor for R

Any recommendations for a good Windows text editor for R? I've been using Tinn-R, and it's been working reasonably well, but am curious to know what else people use. ...

How do you pause an R script / animation?

How do you pause an R script for a specified number of seconds or miliseconds? The intended purpose is for self-timed animations. The desired solution works without asking for user input. ...

Portable use of dyn.load to call a C function in an R package

I am creating an R package that I intend to submit to CRAN that has a function calling a routine written in C. How do I load the compiled C routine in the R function in platform-independent way? I am able to make my package work on my intel-based Mac with: function(mydata) { dyn.load(file.path(.Library,"mypkg/libs/i386",paste("mypkg",...

What can be done in R that can't be done with Python/Numpy/SciPy

I've been recently wondering about the over-proliferation of DSLs like R - and thinking whether this is good or bad. Specifically, I wonder what right has R as a stand-alone language and environment? Wouldn't it be much better to build it as an internal DSL/library on some popular language (like Python)? What can R do that Python with s...

Does column exist and how to rearrange columns in R data frame

How do I add a column in the middle of an R data frame? I want to see if I have a column named "LastName" and then add it as the third column if it does not already exist. ...

R object identification

I am often ending up with a function producing output for which I don't understand the output data type. I'm expecting a list and it ends up being a list of lists or a data frame or something else. What's a good method or workflow for figuring out the output data type when first using a function? ...

Determining if a matrix is diagonalizable in the R Programming Language

I have a matrix and I would like to know if it is diagonalizable. How do I do this in the R programming language? ...

Goodness of fit functions in R

What functions do you use in R to fit a curve to your data and test how well that curve fits? What results are considered good? ...

Reshaping time series data from wide to tall format (for plotting)

I have a data frame containing multiple time series of returns, stored in columns. The first column contains dates, and subsequent columns are independent time series each with a name. The column headers are the variable names. ## I have a data frame like this t <- seq(as.Date('2009-01-01'),by='days',length=10) X <- rnorm(10,0,1) Y <- ...

unexpected agrep() results related to max.distance in R

This was tweeted to me by @leoniedu today and I don't have an answer for him so I thought I would post it here. I have read the documentation for agrep() (fuzzy string matching) and it appears that I don't fully understand the max.distance parameter. Here's an example: pattern <- "Staatssekretar im Bundeskanzleramt" x <- "Bundeskanzle...

A better GUI for R

I really love to see something like Reinteract for R. I have been battling with the sourcecode of Reinteract to be able to run it for R. (It is in Python) any ideas on how one may get it to work for R? it is a very useful kind of editor ...

Getting windows to start R in batch mode using the Start command

I know I must be making a simple syntax mistake, but I want to have a windows batch file that fires up 9 instances of R and runs a different routine in each one. I want these to run simultaneously (i.e. asynchronously). I can fire up 9 command prompt windows and type a command in each one, but it seems like with the START command I shoul...

Expert R users, what's in your .Rprofile?...

This question is maybe a little too cute, but I have always found startup profile files of other people both useful and instructive about the language. Moreover, while I have some customization for bash and vim, I have nothing for R. For example, one thing I always wanted is different colors for input and output text in a window terminal...

Hierarchical Bayes for R or Python

Hierarchical Bayes models are commonly used in Marketing, Political Science, and Econometrics. Yet, the only package I know of is bayesm, which is really a companion to a book (Bayesian Statistics and Marketing, by Rossi, et al.) Am I missing something? Is there a software package for R or Python doing the job out there, and/or a worked-...

dropping factor levels in a subsetted data frame in R

I have a data frame containing a factor. When I create a subset of this data frame using subset() or another indexing function, a new data frame is created. However, the factor variable retains all of its original levels -- even when they do not exist in the new data frame. This creates headaches when doing faceted plotting or using f...