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.
...
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...
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...
...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/...
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...
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...
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 ...
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,...
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$...
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...
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 ...
...
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...
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...
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 ...
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!
...
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
...
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...
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...
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 ...
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.
...