r

How can I count runs in R?

In R, what would be the most efficient/simplest way to count runs of identical elements in a sequence? For example, how to count the numbers of consecutive zeros in a sequence of non-negative integers: c(1,0,0,0,1,0,0,0,0,0,2,0,0) should give 3,5,2. Thanks. ...

Associate .Rnw with vim latex suite

This I am sure is really basic stuff. I am just beginning using gvim and latex-suite. However I would like latex-suite to load when I edit a sweavefile with.Rnw extension. my .vimrc looks like this " These settings are needed for latex-suite filetype indent on filetype plugin on filetype on let g:tex_flavor='latex' set grepprg=grep\ -n...

Pipe ESS to terminal outside of Emacs?

I'm not sure if this is possible, but does anyone know if I can pipe ESS commands (i.e. evaluate region) to a R process running outside of Emacs? The Emacs terminal hangs up a bit (more often than Apple's terminal) and I'd like to just ditch it, while still using ESS commands. Currently I am doing the less efficient copy and paste techni...

Help me replace a for loop with an "apply" function

...if that is possible My task is to find the longest streak of continuous days a user participated in a game. Instead of writing an sql function, I chose to use the R's rle function, to get the longest streaks and then update my db table with the results. The (attached) dataframe is something like this: day user_id 2008/11/...

Selectively replacing columns in R with their delta values

Hello everyone, I've got data being read into a data frame R, by column. Some of the columns will increase in value; for those columns only, I want to replace each value (n) with its difference from the previous value in that column. For example, looking at an individual column, I want c(1,2,5,7,8) to be replaced by c(1,3,2,1) w...

How to count number of Numeric values in a column

I have a dataframe, and I want to produce a table of summary statistics including number of valid numeric values, mean and sd by group for each of three columns. I can't seem to find any function to count the number of numeric values in R. I can use length() which tells me how many values there are, and I can use colSums(is.na(x)) to c...

Finding uncommon elements across multiple vectors

I am attempting to find the elements that are not common across multiple vectors. That is, I want to know exactly the elements (not just their position, etc.) that are not shared across all vectors. The best implementation I could come up with uses a nested-loop, which I realize is probably the least efficient, most notably because the ...

Count by factor in ggplot2 chart

Given the following ggplot2 chart: ggplot(my_data, aes(colour=my_factor) + geom_point(aes(x=prior, y=current)) + facet_grid(gender ~ age) I would like to make the size of the points be proportional to the count of my_factor for that prior/current combination. ggplot(my_data, aes(colour=my_factor,...

Platform-specific graphic device

I'd like to write a function that takes a filename and produces a .pdf file on a *nix platform and a .wmf on a windows platform with that filename and width of 6 inches height 4. graph <- function(filename){ setwd("graphics") ext <- ifelse(.Platform$OS.type == "unix", "pdf", "wmf") name <- paste(filename, ext, sep=".") ifelse(.Platform$...

converting a matrix of lists to a regular matrix

Take the following code: foo <- list() foo[[1]] <- list(a=1, b=2) foo[[2]] <- list(a=11, b=22) foo[[3]] <- list(a=111, b=222) result <- do.call(rbind, foo) result[,'a'] In this case, result[,'a'] shows a list. Is there a more elegant way such that result is a "regular" matrix of vectors? I imagine there are manual ways of going abou...

Help me make conditionally grouped histograms from my dataset

My current dataset data.df comes from about 420 students who took an 8-question survey under one of 3 instructors. escore is my outcome variable of interest. 'data.frame': 426 obs. of 10 variables: $ ques01: int 1 1 1 1 1 1 0 0 0 1 ... $ ques02: int 0 0 1 1 1 1 1 1 1 1 ... $ ques03: int 0 0 1 1 0 0 1 1 0 1 ... ...

How to read a csv file in R where some numbers contain commas?

I have a large csv file with a mix of character and numeric columns. Some of the numerical values are expressed as strings with commas. e.g., "1,513" instead of 1513. What is the simplest way to read the data into R? I can use read.csv(...,colClasses="character"), but then I have to strip out the commas from the relevant elements before...

Plotting predefined density functions using ggplot and R

I have three data sets of different lengths and I would like to plot density functions of all three on the same plot. This is straight forward with base graphics: n <- c(rnorm(10000), rnorm(10000)) a <- c(rnorm(10001), rnorm(10001, 0, 2)) p <- c(rnorm(10002), rnorm(10002, 2, .5)) plot(density(n)) lines(density(a)) lines(density(p)) W...

Installing a local package that's not a .tar.gz

Hey Fellow Rstaters, I'm working on a script that creates a package in the current directory (using pdInfoBuilder from BioConductor), and I'd like to install it while the script is running. install.packages() with repo=NULL seems like an obvious choice, but this seems to only except package directories tarballed and gzipped. Is there a ...

R: MCMClogit confusion

Could anybody explain to me why simulatedCase <- rbinom(100,1,0.5) simDf <- data.frame(CASE = simulatedCase) posterior_m0 <<- MCMClogit(CASE ~ 1, data = simDf, b0 = 0, B0 = 1) always results in a MCMC acceptance ratio of 0? Any explanation would be greatly appreciated! ...

R: how do I dichotomise efficiently

Is there a more "R-minded" way to dichotomise efficiently? Thanks. y<-c(0,3,2,1,0,0,2,5,0,1,0,0);b<-vector() for (k in 1:length(y)) { if (y[k] == 0) b[k] = 0 else b[k] = 1 } y;b ...

LanguageR/ lme4: any help on p-values for models with random correlation parameters?

Hello R programmers! I used the package languageR for mixed effect models with the syntax at the end of this posting. I can use pvals.fnc to get p-values for model 1 and 3 (hd_lmer1 and hd_lmer2). Using this with model two gives the following error message: p2 = pvals.fnc(hd_lmer2) Error in pvals.fnc(hd_lmer2) : MCMC sampling...

Showing multiple axis labels using ggplot2 with facet_wrap in R

I've got a nice facet_wrap density plot that I have created with ggplo2. I would like for each panel to have x and y axis labels instead of only having the y axis labels along the left side and the x labels along the bottom. What I have right now looks like this: library(ggplot2) myGroups <- sample(c("Mo", "Larry", "Curly"), 100, replac...

Reshaping data frame in R

I'm running into difficulties reshaping a large dataframe. And I've been relatively fortunate in avoiding reshaping problems in the past, which also means I'm terrible at it. My current dataframe looks something like this: unique_id seq response detailed.name treatment a N1 123.23 descr. of N1 T1 a ...

What's the biggest R-gotcha you've run across?

Is there a certain R-gotcha that had you really surprised one day? I think we'd all gain from sharing these. Here's mine: in list indexing, my.list[[1]] is not my.list[1]. Learned this in the early days of R. ...