r

How do I install an R package from source?

A friend sent me along this great tutorial on webscraping NYtimes with R. I would really love to try it. However, the first step is to installed a package called RJSONIO from source. I know R reasonable well, but I have no idea how to install a package from source. I have a mac OSX, if that is meaningful. ...

lag in apply statement doesn't work in R

I'm trying to "apply" a function that does "lag"s on zoo objects in R. The function works correctly if I pass a single zoo vector - it applys the lag and everything works. However, if I "apply( data, 1, function )" then the lag doesn't work. There is no error, just the equivalent of a zero lag. This is also the case with a simple "ap...

R: update plot [xy]lims with new points() or lines() additions?

Background: I'm running a Monte Carlo simulation to show that a particular process (a cumulative mean) does not converge over time, and often diverges wildly in simulation (the expectation of the random variable = infinity). I want to plot about 10 of these simulations on a line chart, where the x axis has the iteration number, and th...

How to overlay a line for an lm object on a ggplot2 scatterplot

I have some data, calvarbyruno.1<-structure(list(Nominal = c(1, 3, 6, 10, 30, 50, 150, 250), Run = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", "2", "3"), class = "factor"), PAR = c(1.25000000000000e-05, 0.000960333333333333, 0.00205833333333334, 0.00423333333333333, 0.0322333333333334, 0.614433333333334, ...

How to use Predict.lm in r to reverse the regression

Hello. I have some data in a dataframe calvarbyruno.1 with variables Nominal and PAR that represent the Peak Area Ratio (PAR) found from analysis of a set of standards using a particular analytical technique, and two lm models of that data (linear and quadratic) for the relationship PAR ~ Nominal. I'm trying to use the predict.lm funct...

Changing shapes used for scale_shape() in ggplot2

Suppose I have the following y <- rnorm(10) b <- as.factor(sample(1:4,10,replace=T)) qplot(1:10, y, shape=b) How do I change the shapes that are used using ggplot2? ...

Optimizing the computation of a recursive sequence

What is the fastest way in R to compute a recursive sequence defined as x[1] <- x1 x[n] <- f(x[n-1]) I am assuming that the vector x of proper length is preallocated. Is there a smarter way than just looping? Variant: extend this to vectors: x[,1] <- x1 x[,n] <- f(x[,n-1]) ...

Help plotting Geographic Data in R using PBSMapping and Shapefiles

Using O'Reilly's Data Mashups in R as inspiration, I'm trying to plot a handful of addresses on a shapefile of Salt Lake County, Utah found here. I have data frame geoTable: > geoTable address Y X EID 1 130 E 300 S 40.76271 -111.8872 1 2 875 E 900 S 40.74992 -111.8660 2 3 2200 S 700 E 40.72298 -111.8...

label of log y-axis: 1000 instead of 1e+03?

I've a problem concerning construction of log y-axis in a graphic. How can I manage that the units/numbers of my log y-axis aren't shown in 1e+03, 1e+04, 1e+05 etc...., but only in regluar arabic numbers (1000, 10000, 100000)? Thanks. ...

Adding summary statistics (or even raw data points) to dodged position boxplots

Say you have the following dataset: trt <- ifelse(runif(100)<0.5,"drug","placebo") inj.site <- ifelse(runif(100)<0.5,"ankle","wrist") relief <- 20 + 0.5*(inj.site=="ankle") + 0.5*(trt=="drug") + rnorm(100) to.analyze <- data.frame(trt,inj.site,relief) Now, the idea is to make a boxplot with injury site on the x-axis and boxes by trea...

Renaming rows and columns in R

I'm running the following script: cause = c(1, 1, 1, 1, 1, 2, 1, 2, 2, 2, 2); time = c(1, 1, 2, 3, 3, 2, 2, 1, 1, 2, 2); table(cause, time) And I get the following:           time cause 1 2 3        1 2 2 2        2 2 3 0 What I want is this:                time      cause 1 2 3   Maltreat 2 2 2 Non-Maltr 2 3 0 So, my questi...

Blogging with R: easy way to embed R in a blog post?

There is a very nice Mediawiki plugin for R which allows you to embed R code in any wiki page. It takes a little effort to configure, but it's really useful once you have it in place. Does anyone know of an equivalent for embedding R in a blog post? The only thing I could find was this wordpress plugin called RWebFriend, but it only a...

How can I take multiple vectors and recode their datatypes in R?

I'm looking for an elegant way to change multiple vectors' datatypes in R. I'm working with an educational dataset: 426 students' answers to eight multiple choice questions (1 = correct, 0 = incorrect), plus a column indicating which instructor (1, 2, or 3) taught their course. As it stands, my data is sitting pretty in data.df, like th...

Trying to loop through a dataframe and reference multiple fields

I have a dataframe with Address, City, State, Zip entities. From there, I'm trying to use the Yahoo APIs to Geocode each address. I'm basing this off the code in O'Reilly's Data Mashups using R Tutorial. The original example takes a vector of street addresses and uses a hard-coded city. I'm trying to make a dynamic example that suppo...

Trying to return a specified number of characters from a gene sequence in R

Hi, I am working in R and have a DNA sequence for example:"cgtcgctgtttgtcaaagtcg...." that is possibly 1000+ letters long. However, I only want to look at letters 5 to 200, for example, and to define this subset of the string as a new object. I tried looking at the nchar function, but haven't found something that would do this. THank...

Rounding output from by function in R

I'm trying to round an output from a simple by() function in R. This is what I have... > by(glaciers[,1:3],glaciers$activity.level,mean) glaciers$activity.level: Active aspect sun.duration latitude -9.444444e+00 1.771778e+03 3.247643e+09 ------------------------------------------- glaciers$activity.level: Inactive ...

How to insert elements into a vector?

Hi, I have a logical vector, which i wish to insert new elements at particular indexes - I've come up with a clusmy solution below, but is there a neater way? probes <- rep(TRUE, 15) ind <- c(5, 10) probes.2 <- logical(length(probes)+length(ind)) probes.ind <- ind + 1:length(ind) probes.original <- (1:length(probes.2))[-probes.ind] pro...

How to convert vector from 1-row table in R

In R, I have a 1-row table. How do I convert that to a vector? Specifically, the table is this: 0 1 2 3 4 21 35 46 62 36 I've tried bracket notation but to no avail! ...

How to apply Min or Max to each result of a function separately?

I have a function to calculate the inverse of a quadratic equation. By default it gives the two possible solutions: invquad<-function(a,b,c,y,roots="both") { #Calculate the inverse of a quadratic function y=ax^2+bx+c (i.e. find x when given y.) #Gives NaN with non real solutions. root1<-sqrt((y-(c-b^2/(4*a)))/a)-(b/(2*a)) ...

Fitting a density curve to a histogram in R

Is there a function in R that fits a curve to a histogram? Let's say you had the following histogram hist(c(rep(65, times=5), rep(25, times=5), rep(35, times=10), rep(45, times=4))) It looks normal, but it's skewed. I want to fit a normal curve that is skewed to wrap around this histogram. This question is rather basic, but I can't ...