r

Recommendations for database with R

I am using R to run simulations using time series data. I have been using arrays to store data but I need a less memory intensive solution for storing data at intermediate steps in order to document the process. I am not a programmer so I am looking for something relatively easy to setup on multiple platforms if possible (Windows, Mac, L...

issue running a test in Python, via rpy2

I have a feeling this will be a quick fix, given that I started coding two weeks ago. I am try to run a statistical test - a Mantel, looking for a correlation between two distance matrices - in Python, by using a function(?) that has already been written in R, via Rpy2. The R module is "ade4" and it contains "mantel.rtest" from rpy2 im...

In R, how to collapse categories or recategorize variables?

I am sure this is a very basic question: In R I have 600,000 categorical variables - each of which is classified as "0", "1", or "2" What I would like to do is collapse "1" and "2" and leave "0" by itself, such that after re-categorizing "0" = "0"; "1" = "1" and "2" = "1" --- in the end I only want "0" and "1" as categories for each of...

Instring Function in R

Hi all, Close to finishing up my first project entirely in R, but I am stuck. I don't really have a good grasp of regex, but usually can accomplish my tasks with substring, instring, len, or comparable functions. I have two questions. Where can I find a list of string functions in R, and 2nd, does R have an instring type function? ...

In what cases should new.env be used to create a new environment?

In the "What is the most useful R trick?" (here), I read that using environments give "pass-by-reference capabilities". Are there any limits and/or gotchas with this approach? Also, in general what are the pros and cons of using created environments? This is something I've been confused about for quite some time, so any clarity or refer...

Interactive Graphs in R using rggobi

From the following xml code: <?xml version = "1.0"?> <Company > <shareprice> <timeStamp> 12:00:00.01</timeStamp> <Price> 25.02</Price> </shareprice> <shareprice> <timeStamp> 12:00:00.02</timeStamp> <Price> 15</Price> </shareprice> <shareprice> <timeStamp> 12:00:01.025</timeStamp> <Price> 15.0...

How can I suppress the line numbers output using R CMD BATCH?

If I have an R script: print("hi") commandArgs() And I run it using: r CMD BATCH --slave --no-timing test.r output.txt The output will contain: [1] "hi" [1] "/Library/Frameworks/R.framework/Resources/bin/exec/x86_64/R" [2] "-f" [3] "test.r" ...

Assigning group ID with ddply

Hi all, Pretty basic performance question from an R newbie. I'd like to assign a group ID to each row in a data frame by unique combinations of fields. Here's my current approach: > # An example data frame > df <- data.frame(name=c("Anne", "Bob", "Chris", "Dan", "Erin"), st.num=c("101", "102", "105", "102", "150"), st.name=c("Main", "E...

Matrix power in R

Trying to compute the power of a matrix in R, I found that package expm implements the operator %^%. So x %^% k computes the k-th power of a matrix. > A<-matrix(c(1,3,0,2,8,4,1,1,1),nrow=3) > A %^% 5 [,1] [,2] [,3] [1,] 6469 18038 2929 [2,] 21837 60902 9889 [3,] 10440 29116 4729 but, to my surprise: > A [,1] [,2] [,3...

group by in R, ddply with weighted.mean

Hi, I am trying to do a "group by" - style weighted mean in R. With some basic mean the following code (using the plyr package from Hadley) worked well. ddply(mydf,.(period),mean) If I use the same approach with weighted.mean i get the following error "'x' and 'w' must have the same length" , which I do not understand because the w...

Limiting variable scope

I'm trying to write a function, which limits the scope of R variables. For example, source("LimitScope.R") y = 0 f = function(){ #Raises an error as y is a global variable x = y } I thought of testing the variable environment, but wasn't really sure of how to do this. The why I teach R to undergrads. In their first couple of ...

Flipping line endings in C++ (or R)

I have a C++ that I'm running though R (via Rcpp). Recently, I found that the C++ program I'm wrapping with Rcpp fails when using Windows-style line endings. I'm wondering if anyone knows of a cross-platform way for me to change Windows-style line endings to Unix-style line endings in either C++ or R or ash shell (which comes install...

Is it Possible to use PHP to select a file to pass into an R script as an argument to be operated on?

At the moment I am using a BATCH file that comprises of the following line of code: c:\R\bin\Rcmd.exe BATCH "<filepath>/shares.R" It opens and runs the following R code: library(ggplot2) library (XML) test.df <- xmlToDataFrame(file.choose()) test.df sapply(test.df, class) test.df$timeStamp <- strptime(as.character(test.df$timeStamp...

WHERE aequivalent in R, multiplication conditional on another column of the same df

Dear all, i am trying to run a simple multiplication of a data.frame column with a scalar A respectively scalar B based on the value of third column (id) of the same data.frame. Somehow I have some (order,sort?) problem – so far the result is definitely wrong. Here are several tries: mydf$result = subset(mydf,myid==123,multiplyme)*0.6...

R/C++ Interface

How to use RCPP_MODULE(yada) in C++. My C++ program gives error if I use const char* hello( std::string who ){ std::string result( "hello " ) ; result += who ; return result.c_str() ; } RCPP_MODULE(yada) { using namespace std; function( "hello", &hello ) ; }; Error are: 1. Error 1 error C2065: 'yada' : undeclared identifier 2. Error ...

[R] How do you select multiple variables from a matrix using a randomly selected vector of column indices?

Hopefully this has an easy answer I just haven't been able to find: I am trying to write a simulation that will compare a number of statistical procedures on different subsets of rows (subjects) and columns (variables) of a large matrix. Subsets of rows was fairly easy using a sample() of the subject ID numbers, but I am running into a...

Class Weight Syntax in Kernlab?

Hi I am trying out classification for imbalanced dataset in R using kernlab package, as the class distribution is not 1:1 I am using the option of class.weights in the ksvm() function call however I do not get any difference in the classification scenario when I add weights or remove weights? So the question is what is the correct syntax...

Determinant of a complex matrix in R

Is there a way to calculate the determinant of a complex matrix? F4<-matrix(c(1,1,1,1,1,1i,-1,-1i,1,-1,1,-1,1,-1i,-1,1i),nrow=4) det(F4) Error in determinant.matrix(x, logarithm = TRUE, ...) : determinant not currently defined for complex matrices library(Matrix) determinant(Matrix(F4)) Error in Matrix(F4) : complex matrices not...

Command Line in R code

Hi all, Let me start by saying I am new to programming. I am hoping to run a python script from the command line within an R script. I am running windows xp but also have a machine that runs Windows 7. I can run the following code without error for the dos-prompt. cd C:\Documents and Settings\USER\workspace\UGA - Website python test...

Rough sets in R

Do you know any R packages implementing rough set based methods? I believe the answer is there are no, but maybe I am missing something subtle. ...