r

Is is a good practice to update R packages often?

I've started to use R a little while ago and am not sure how often to update the installed packages (at this time, I'm using mostly ggplot2 and rattle). One one hand it's the typical geek impulse to have the latest version :-) On the other, updates can break functionality and, as an R beginner, I don't want to waste time looking into pac...

How do I email myself data from a R script?

I'm hoping to take advantage of Amazon spot instances which come at a lower cost but can terminate anytime. I want to set it up such that I can send myself data mid-way through a script so I can pick up from there in the future. How would I email myself a .rdata file? difficulty: The ideal solution will not involve RCurl since I am un...

ggplot: how to specify vertical order of multiple boxplots?

I would like to change the stacking order of hospitals in the diagram below so #1 is at the top and #4 at the bottom. The diagram was produced with ggplot(survey,aes(x=hospital, y=age))+geom_boxplot()+coord_flip()+xlab("")+ylab ("\nPatient Age") and I need the top->down order to be the reverse of what is now. I'm not sure why it com...

dputting an S4 object

How would a person dput() an S4 object? I tried this require(sp) require(splancs) plot(0, 0, xlim = c(-100, 100), ylim = c(-100, 100)) poly.d <- getpoly() #draw a pretty polygon - PRETTY! poly.d <- rbind(poly.d, poly.d[1,]) # close the polygon because of Polygons() and its kin poly.d <- SpatialPolygons(list(Polygons(list(Polygon(poly.d)...

ggplot2: adding a background layer

I want to added dark/light phase information to the background of my stacked area graphset to highlight the how light affects the shapes of the curves. My dataframe looks like this: > str(MDist.median) 'data.frame': 2880 obs. of 6 variables: $ groupname: Factor w/ 8 levels "rowA","rowB",..: 1 1 1 1 1 1 1 1 1 1 ... $ fCycle : Fact...

Restructure Data in R

Hi Everyone, I am just starting to get beyond the basics in R and have come to a point where I need some help. I want to restructure some data. Here is what a sample dataframe may look like: ID Sex Res Contact 1 M MA ABR 1 M MA CON 1 M MA WWF 2 F FL WIT 2 F FL CON 3 X GA XYZ I want the data to look lik...

In R, can I use a list as a hash? If so, why is it so slow?

Before using R, I used quite a bit of Perl. In Perl, I would often use hashes, and lookups of hashes are generally regarded as fast in Perl. For example, the following code will populate a hash with up to 10000 key/value pairs, where the keys are random letters and the values are random integers. Then, it does 10000 random lookups in th...

ggplot: How to change facet labels?

I have used the following ggplot command: ggplot(survey,aes(x=age))+stat_bin(aes(n=nrow(h3),y=..count../n), binwidth=10) +scale_y_continuous(formatter = "percent", breaks=c(0, 0.1, 0.2)) + facet_grid(hospital ~ .) + opts(panel.background = theme_blank()) to produce I'd like to change the facet labels, however, to something...

Optimizing the Verhoeff Algorithm in R

I have written the following function to calculate a check digit in R. verhoeffCheck <- function(x) { ## calculates check digit based on Verhoeff algorithm ## note that due to the way strsplit works, to call for vector x, use sapply(x,verhoeffCheck) ## check for string since leading zeros with numbers will be lost if (class(x)!="charac...

I can't read in data to R

I am trying to read in some data that is is a text file that looks like this: 2009-08-09 - 2009-08-15 0 2 0 2009-08-16 - 2009-08-22 0 1 0 2009-08-23 - 2009-08-29 0 1 0 2009-08-30 - 2009-09-05 0 1 0 2009-09-06 - 2009-09-12 0 1 0 2009-09-13 - 2009-09-19 0 1 0 2009-09-20 - 2009-09-26 0 1 0 2009-09-27 - 2009-10-0...

how to check if the number is integer?

I was surprised to learn that R doesn't come with a handy function to check if the number is integer. is.integer(66) # FALSE The help files warns: is.integer(x) does not test if x contains integer numbers! For that, use round, as in the function is.wholenumber(x) in the examples. The example has this custom function as a "...

How to fit a negative binomial distribution in R while incorporating censoring

I need to fit Y_ij ~ NegBin(m_ij,k), hence a negative binomial distribution to a count. However, the data I have observed are censored, I know the value of y_ij, but it could be more than that value. Writting down the loglikelihood going with this problem is: ll = \sum_{i=1}^n w_i (c_i log(P(Y_ij=y_ij|X_ij)) + (1- c_i) log(1- \sum_{k=1}...

Displaying the actual parameter list of the function during execution

I am trying to display the actual values of the parameters that were supplied when the function was called. `match.call' does something along the lines I want but it does not evaluate variables. For example foo <- function(x) match.call() foo(2) prints foo(x = 2) and I am happy with that. However: xxx <- 2 foo(xxx) will print ...

Reshape data based on column in dataframe

I need to take a data.frame in the format of: id1 id2 mean start end 1 A D 4 12 15 2 B E 5 14 15 3 C F 6 8 10 and generate duplicate rows based on the difference in start - end. For example, I need 3 rows for the first row, 1 for the second, and 2 for the third. The start and end fields should be in...

problem creating dynamic file name in R

I'm working on a script in R that processes some data and writes an output file. I'd like that output file to be named in a way that reflects the input file, and I'd like something about the file to be unique so older files aren't overwritten. So I thought to use a timestamp. But this isn't working the way I'd hoped, and I'd like to u...

R- converting data from fraction to decimal

Is there a simple way to convert data in a dataframe from fraction to decimal format? I have a column of data that that's been recorded as a fraction: Levels: 1/2 1/3 1/4 1/5 1/8 2/3 Is there a quick way to convert it to .5 .333 25 .2 .125 .67? ...

how to script in R over a factor's levels

I have a data frame with a quantitative variable, x, and several different factors, f1, f2, ...,fn. The number of levels is not constant across factors. I want to create a (single) plot of densities of x by factor level fi. I know how to hand code this for a specific factor. For example, here is the plot for a factor with two level...

How to fit a smooth curve to my data in R?

I'm trying to draw a smooth curve in R. I have the following simple toy data: > x [1] 1 2 3 4 5 6 7 8 9 10 > y [1] 2 4 6 8 7 12 14 16 18 20 Now when I plot it with a standard command it looks bumpy and edgy, of course: plot(x,y, type='l', lwd=2, col='red') How can I make the curve smooth so that the 3 edges are r...

mapping over the rows of a data frame

Suppose I have a data frame with columns c1, ..., cn, and a function f that takes in the columns of this data frame as arguments. How can I apply f to each row of the data frame to get a new data frame? For example, x = data.frame(letter=c('a','b','c'), number=c(1,2,3)) # x is # letter | number # a | 1 # b | 2 # c | 3 f...

multiple comboboxes in R using tcltk

I have been trying to define multiple combo boxes in R using the tcltk package but to no avail. I am using the below code. My inspiration was here, however I can't seem to just label them comboBox1, comboBox2, etc... so I decided to try and set their output values into a vector... but their output values don't make any sense to me... a...