r

How to learn R as a programming language?

I'd like to know how to learn the R language as as 'programming' language as opposed to learning it as a statistical system. My question is prompted by lack of understanding of such functions as parse, eval, etc. which may not find frequent use by an R user with a 'statistics' persuasion. Edit: I've been exploring such tools like Rpy RS...

How to define argument types for R functions?

I am writing an R function, and I want to make sure that the argument of my R function is of a certain class (eg, "matrix"). What is the best way to do this? Say I have a function "foo" which computes the inverse of a matrix: foo <- function(x) { # I want to make sure x is of type "matrix" solve(x) } How can I say - as you mig...

Best way to allocate matrix in R, NULL vs NA?

I am writing R code to create a square matrix. So my approach is: Allocate a matrix of the correct size Loop through each element of my matrix and fill it with an appropriate value My question is really simple: what is the best way to pre-allocate this matrix? Thus far, I have two ways: > x <- matrix(data=NA,nrow=3,ncol=3) > x ...

REvolution for R

Hi all, since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix calculations. And it seems to really work, apparently. For example on a matrix transposition with REvolution: > system.time(t(matrix...

specifying column names of a data frame within a function ahead of time

Suppose you're trying to create a data frame within a function. I would like to be able to define the column names ahead of time as one of the parameters of the function. Take the following code: foo <- function(a) { answer <- data.frame(a=1:5) return(answer) } In the above example, I would like to be able to specify t...

Help using predict() for kernlab's SVM in R?

I am trying to use the kernlab R package to do Support Vector Machines (SVM). For my very simple example, I have two pieces of training data. A and B. (A and B are of type matrix - they are adjacency matrices for graphs.) So I wrote a function which takes A+B and generates a kernel matrix. > km [,1] [,2] [1,] 14.33333 18....

Passing a function argument through R-function nlm

I am probably being unreasonable asking for help debugging a program, but I have spent a day and a half on this pretty simple bit of code and have run out of ideas. I am trying to optimise a function called "log.pr.data" with respect to its first argument. Because the function optimise requires you to set bounds on the argument I decid...

R plot model over observatio

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the fitted model. So I try something like: model <- lm(x~y+z) plot(x) lines(fitted(model)) But this just plots x with lines. Thanks ...

R demo() and example() methods?

Is there a simple way to check if R functions and packages have demo() and example() methods? When building a package, does the package need to have the necessary objects so that demo() and example() can be called on it? Edit: In trying to answer this, i checked the source code of demo() demo(package=.packages(all.available = TRUE)) #...

Adding a line to ggplot

Hi all, I am trying to add a line to a plot of points, and I can't figure it out. My y values are numbers from 0 to Inf, while my x values are from an ordered factor. Here is the plotting code, which only displays points: g = ggplot() + layer(data = ratesdf, mapping = aes(x = age, y = rates), geom = "point", stat="identity") + la...

R: Use VAR model to predict response to change in values of certain variables

Hi I've fitted a VECM model in R, and converted in to a VAR representation. I would like to use this model to predict the future value of a response variable based on different scenarios for the explanatory variables. Here is the code for the model: library(urca) library(vars) input <-read.csv("data.csv") ts <- ts(input[16:52,],c(200...

how to remove partial duplicates from a data frame?

I'm new to R and it looks like it is a good language to allow my colleagues to their job in a quick way, but I have to prepare the data for them and I want to do it "the R way". the data I'm importing describes numeric measurements taken at various locations for more or less evenly spread timestamps. sometimes this "evenly spread" is n...

R: access field values

Hi I would like to know how I can access the individual fields contained in an R object. Or, more precisely, how to get R to tell me how. For example, if I run the following code: dx.ct <- ur.df(dat1[,'dx'], lags=3, type='trend') summary(dx.ct) then I get this output: ############################################### # Augmented Dic...

Growing matrices in R from NULL

What is the problem with initializing a matrix object to NULL and then growing it with cbind() and rbind() ? In case the number of rows and columns are not known a priori, is it not necessary to grow from NULL? Edit: My question was prompted by the need to understand memory efficient ways of writing R code. The matrix context is more ge...

R: an NA in subsetting a data.frame does something stupid

I do find R to be a very frustrating language. It seems to excel on data-dependent bugs. Consider the following code. When you don't explicitly test for NA in your condition, that code will fail at some later date then your data changes. > # A toy example > a <- as.data.frame(cbind(col1=c(1,2,3,4),col2=c(2,NA,2,3),col3=c(1,2,3,4),c...

Eclipse 3.5 / Statet / R Console not working on Ubuntu linux

Hi, I had Eclipse 3.4 working nicely with the R Console over rJava. I want to get Sweave working in Eclipse, but need Eclipse 3.5 / statet 0.8 to do it. When I try to start the rJava Console the JVM starts, but no feedback comes back to the console in Eclipse. JVM process below - any help gratefully received. Thanks, Dave dave 2...

Getting R to store the working directory for every session

I asked this on Super User, but someone suggested that I take it here because there are many more R experts. The question: I have to keep navigating to my directory when I go to File > Change dir..., which is particularly annoying. Does anyone know how to make R remember the previously used directory? ...

Importing Functions into Current Namespace

Let's say I have an R source file comprised of some functions, doesn't matter what they are, e.g., fnx = function(x){(x - mean(x))/sd(x)} I would like to be able to access them in my current R session (without typing them in obviously). It would be nice if library("/path/to/file/my_fn_lib1.r") worked, as "import" works in Python, but ...

how do tell if its better to standardize your data matrix first when you do principal component analysis in R ?

Hi guys , Im trying to do principal component analysis in R . There is 2 ways of doing it , I believe. One is doing principal component analysis right away the other way is standardizing the matrix first using s = scale(m)and then apply principal component analysis. How do I tell what result is better ? What values in particular s...

Specific date format conversion problems in R

Hello, Basically I want to know why as.Date(200322,format="%Y%W") gives me NA. While we are at it, I would appreciate any advice on a data structure for repeated cross-section (aka pseudo-panel) in R. I did get aggregate() to (sort of) work, but it is not flexible enough - it misses data on columns when I omit the missed values, for exam...