r

create a boxplot in R that labels a box with the sample size (N)

Is there a way to create a boxplot in R that will display with the box (somewhere) an "N=(sample size)"? The varwidth logical adjusts the width of the box on the basis of sample size, but that doesn't allow comparisons between different plots. FWIW, I am using the boxplot command in the following fashion, where 'f1' is a factor: boxpl...

view source code in R

How do I view source code in R? For example, for function portfolio.optim > require(tseries) > portfolio.optim function (x, ...) UseMethod("portfolio.optim") <environment: namespace:tseries> > methods(portfolio.optim) [1] portfolio.optim.default* portfolio.optim.ts* Non-visible functions are asterisked > portfolio.optim.ts Error...

Useful little functions in R?

What are the functions that you wrote, don't quite deserve a package, but you wish to share? I will throw in some of mine: destring <- function(x) { ## convert factor to strings if (is.character(x)) { as.numeric(x) } else if (is.factor(x)) { as.numeric(levels(x))[x] } else if (is.numeric(x)) { x ...

Reading 3-dimensional datasets into R

Gnuplot allows for three dimensional datasets, which are a set of tables separated by empty lines, for instance: 54.32,16.17,7.42,4.28,3.09,2.11,1.66,1.22,0.99,0.82,7.9 54.63,15.50,8.53,5.31,3.75,1.66,1.14,0.83,0.94,0.52,7.18 56.49,16.67,6.38,3.69,2.80,1.45,1.12,0.89,1.12,0.89,8.50 56.35,16.26,7.76,3.57,2.62,1.89,1.05,1.15,0.63,1.05,7....

Direct STDERR when opening pipe in perl

I am using open( my $command_out, "-|", $command_string ) to execute a command and process its output on the fly (not having to wait for the command to finish first, as in system()). I noticed that when I call some R scripts this way, some of R messages are printed to the screen (e.g. Loading required package: ...). I guess this is bec...

Installing R packages available for all users

I have a few R packages installed under ~/R/i486-pc-linux-gnu-library/2.11. I would like to make them, and any other R package I install from now on, available to all R users. I don't mind re-installing the packages I already have in a neutral place (they are just a few). So how do I do that? ...

R: How to read different files into a two-dim vector?

I have an R newbie question about storing data. I have 3 different files, each of which contains one column. Now I would like to read them into a structure x so that x[1] is the column of the first file, x[2] is the column of the second file, etc. So x would be a two-dim vector. I tried this, but it wants x[f] to be a single number ra...

normalizing matrices in R

How do I normalize/scale matrices in R by column. For example, when I compute eigenvectors of a matrix, R returns: > eigen(matrix(c(2,-2,-2,5),2,2))$vectors [,1] [,2] [1,] -0.4472136 -0.8944272 [2,] 0.8944272 -0.4472136 // should be normalized to [,1] [,2] [1,] -1 -2 [2,] 2 -1 The function "scale" subtract...

how to make "pretty rounding" in R?

I need to do a rounding like this and convert it as a character: as.character(round(5.9999,2)) I expect it to become "6.00", but it just gives me "6" Is there anyway that I can make it show "6.00"? Thanks! ...

Creation of a specific vector without loop or recursion in R

Hi guys! I've got a first vector, let's say x that consists only of 1's and -1's. Then, I have a second vector y that consists of 1's, -1's, and zeros. Now, I'd like to create a vector z that contains in index i a 1 if x[i] equals 1 and a 1 exists within the vector y between the n precedent elements (y[(i-n):i])... more formally: z <- ...

adjusting x-axis in R histograms

This histogram is really ugly: hist(rbinom(10000, 20000, 0.0001),freq=F,right=F) I don't want spaces between my bars. I tried different breaks= methods but they all produce similar results. Any ideas? I also want each bin value (or mean values )to be printed under the center of it's bar. ...

Slicing plots generated by ggplot2

I wonder if it's possible to slice a graph respectively a .png file generated with ggsave. If I do not want to use the default legend ort title and set it to FALSE it leaves me with a lot of white space. So is there an R way to just cut the file several pixels above and below the graph itself? Thx in advance! ...

data.frame rows to a list

I have a data.frame which I would like to convert to a list by rows, meaning each row would correspond to its own list elements. In other words, I would like a list that is as long as the data.frame has rows. So far, I've tackled this problem in the following manner, but I was wondering if there's a better way to approach this. xy.df <...

Calculate the sum of matrices in a list or a 3D array

Given a list (length = n) of 2x2 matrices, how do I calculate the sum of all those matrices (and get a 2x2 matrix) ? How can I do it, if instead of a list I have those matrices in a (2 x 2 x n) dimensional array ? ...

[R] Shading a kernel density plot between two points.

I frequently use kernel density plots to illustrate distributions. These are easy and fast to create in R like so: set.seed(1) draws <- rnorm(100)^2 dens <- density(draws) plot(dens) #or in one line like this: plot(density(rnorm(100)^2)) Which gives me this nice little PDF: I'd like to shade the area under the PDF from the 75th to ...

Manipulating values with commas

I actually have a solution for this problem, but I am curious if there is a better way to do what I was trying to do. I scraped some data from the majorleaguesoccer.com and read it into R using mls.reg.tmp <- read.table("../data/mls_reg_season_20100812.csv", header = F, sep = ";") Note that I used sep = "...

Rscript + PHP exec() command vs rapache

Say I want to have a simple web app that takes some user input, performs a quick calculation in some predefined R script, and returns some cool looking graphic with say ggplot. One way to do this would be: Have PHP accept some input from a web form Sanitize the user input in PHP Send the arguments to some pre-written R script using som...

Barplot totals by month with ggplot?

I have time series data (I've posted it here as a data.frame): x <- structure(list(date = structure(c(1264572000, 1266202800, 1277362800, 1277456400, 1277859600, 1278032400, 1260370800, 1260892800, 1262624400, 1262707200), class = c("POSIXt", "POSIXct"), tzone = ""), data = c(-0.00183760994446658, 0.00089738603087497, 0.0004235135983...

solve.QP require D to be symmetric positive definite in R

When I run solve.QP on my problem, I get the following error from R: Error in solve.QP(sigma, rep(0, 5), t(Amat), bvec, meq = 2) : matrix D in quadratic function is not positive definite! My sigma matrix is symmetric but is not positive definite. Why is this needed? If I solve it myself using Lagrangian functions, I am able to get ...

R solver for non linear constraints

Is there R solver function similar to solve.QP but for non linear constraints? Also, is there another generic solver for cubic or higher degree contraints and minimum/maximum functions? ...