r

Count number of alternations in a coin flip sequence [R]

I have a sequence of ones and zeros and I would like to count the number of alternations. e.g. x <- rbinom(10, 1, 1/2) > x [1] 0 0 1 1 1 1 1 0 1 0 Thus I would like to count (in R) how many times the sequence alternates (or flips) from one to zero. In the above sequence the number of alternations (counted by hand) is 4. ...

Plotting several jpeg images in a single display

I need to plot and display several jpeg images in a single combined display (or canvas?). For example, suppose I have images {a,b,c,d}.jpg, each of different size, and I would like to plot them on one page in a 2x2 grid. It would be also nice to be able to set a title for each subplot. I've been thoroughly looking for a solution, but co...

R: quoting unquoted members in nested list-

Using R, I generate a list that contains certain unquoted elements. Please see at the bottom- it is invalid javascript code. R code (does not work) outq <- lapply (out, function (el){ el <- if( is.factor(el$ann) ){ el$ann <- apply(el$ann, 1, function(e){ e <- paste('"', e, '"', sep="") }) } }) In the R language, How can I ...

Read variables from global environment with inline Rcpp?

I'm following the example from the Rcpp intro Vignette, trying it with inline. f<-cxxfunction(signature(), plugin="Rcpp", body=" Environment global = Environment::global_env(); std::vector<double> vx = global['x']; ") but I get a compile error. file12384509.cpp: In function 'SEXPREC* file12384509()': file12384509.cpp:31: erro...

Area covered by a point cloud with R

Hi R experts! I have a cloud of points scattered in a 2D Euclidean space. I would like to calculate the area inside the polygon linking the most extreme (=peripheral) points of the cloud. In other words, I would like to estimate the area covered by the cloud in this space. Is there a formula in R? Thanks a lot for any response Julien ...

R: generate a repeating sequence based on vector

I am trying to take an existing vector and repeat each element of it six times. I feel like this should be easy using rep() but I keep hitting the wall. Basically I would like to take this vector: 1027 1028 1030 1032 1037 And turn it into this: 1027 1027 1027 1027 1027 1027 1028 1028 1028 1028 1028 1028 ... ...

setting levels inside lapply loop in r

Dear all, I´m trying to clean the factor variables in a dataframe from trailing spaces. However the levels assignment doesnt work inside my lapply function. rm.space<-function(x){ a<-gsub(" ","",x) return(a)} lapply(names(barn),function(x){ levels(barn[,x])<-rm.space(levels(barn[,x])) }) Any ideas how I can assign l...

In R, how do you count the character vectors?

I used sample function in R to generate a sample of 100 fair coin flips, here is the commands i used. fair.coin = c("heads" = 0.5, "tails" = 0.5) then, x <- sample(fair.coin, size = 100, replace = TRUE) > x tails tails tails heads tails tails tails heads heads tails heads heads tails tails 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ...

Allow the user to select range of data file to be analyzed?

I have the following XML File: <Company > <shareprice> <timeStamp> 12:00:00.01</timeStamp> <Price> 25.02</Price> </shareprice> <shareprice> <timeStamp> 12:00:00.02</timeStamp> <Price> 15</Price> </shareprice> <shareprice> <timeStamp> 12:00:01.025</timeStamp> <Price...

Creating a r package with a C++ dll in windows and ensuring portability to linux

Working in Windows, I've created an r package that links to a c++ dll as a shared library. This works fine and installs without problems on Windows. When I switch to linux, however, the so is not found. Am I right in thinking that the only file in the src directory should be the .cpp file? Do I actually need to run the SHLIB command in...

How can I resolve the "could not find function vapply" error in R?

I installed the stringr package on my Ubuntu 10.04 machine under R 2.10.1. When I try to use the str_extract() function R stops and gives the following error message: Error in recyclable(string, pattern, replacement) : could not find function "vapply" How can I solve this problem? Is there any specific package that contains this vap...

Linking to another package with R inline

I have a C++/Rcpp function the I need to compile and link to the pomp package to get access to a random number generator. I can get the header file included but how to I get it to link to the compiled code? CppColonized<-cxxfunction( sig=signature(x="numeric", t="numeric", params="numeric", dt="numeric"), plugin='Rcpp', includes ...

ggplot: How to increase spacing between faceted plots?

I have several faceted histograms (obtained with the command below) which are nicely plotted one under the other. I would like to increase the spacing between them, however, they are tight. I looked at the doc but didn't find a parameter for this. Thanks in advance. qplot (Happiness.Level, Number.of.Answers, data=mydata, geom="histog...

ggplot: How to override the ylim interval?

I have a faceted plot (about which I had this other question). I would like to control the interval of ylim() to reduce the clutter because it looks like this: It's too detailed and I would like to display only 0 and 500, that is not even the maximum (the thin horizontal lines are enough). The reasons I want only those 2 values are: ...

Can the print() command in R be quieted?

In R some functions can print information and return values, can the print be silenced? For example: print.and.return <- function() { print("foo") return("bar") } returns > print.and.return() [1] "foo" [1] "bar" > I can store the return like: > z <- print.and.return() [1] "foo" > z [1] "bar" > Can I suppress the print o...

How do I extract random effects for a particular group from an R lme object?

Hello, I have successfully fit a linear mixed-effects model, and I'm looking to extract the random effect component for individual groups. I know that the full list of random effects can be extracted using random.effects(model) Then print(random.effects(model)) gives a two-column list of group names and random effects, even though ...

Plotting with R - skewed eps image & resolution problem

Hello, I want to create a graphics of a function in R. The code is: x <- seq(from=0, to=1, by=0.00001) f <- function(x) .... y <- f(x) plot(x, y, xlab="x", ylab="f(x)", pch=16, cex=0.5) min(y) [1] 0.2291203 max(y) [1] 0.7708797 When I save the graphics as bmp from RGui, it looks like here and this is fine. When I save it as...

how to substitute a for loop in R with an optimized function (lapply?)

I've a data frame with time events on each row. In one row I've have the events types of sender (typeid=1) and on the other the events of the receiver (typeid=2). I want to calculate the delay between sender and receiver (time difference). My data is organized in a data.frame, as the following snapshot shows: dd[1:10,] timeid va...

[R, ggplot2] ggplot2, facet_grid, free scales?

In the following example, how do I get the y-axis limits to scale according to the data in each panel? mt <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + geom_point() Neither of these will do it: mt + facet_grid(. ~ cyl, scales="free") mt + facet_grid(. ~ cyl, scales="free_y") ...

R: speeding up "group by" operations

I have a simulation that has a huge aggregate and combine step right in the middle. I prototyped this process using plyr's ddply() function which works great for a huge percentage of my needs. But I need this aggregation step to be faster since I have to run 10K simulations. I'm already scaling the simulations in parallel but if this one...