2.6.x

Histogram with "other" value at the end to show how many values were cut off

No votes yet

Sometimes you have to cut your histogram at some value but you want to still inform the reader how many values you cut off. Here I simply add one additional histogram bar at the right hand side, ">30", to display the missing values.

data(USArrests, "VADeaths")
data <- USArrests$Rape
h <- hist(data, breaks=seq(0,3000,1), plot=F)

subset <- h$counts[seq(1,limit)]
rest <- sum(h$counts[seq(limit+1,length(h$counts))])
data <- c(subset, rest)

labels <- c(seq(1,limit,1),paste(">",toString(limit), seq=""))

Read more

Parsing date strings

No votes yet

# Another version using location and substrings:

x <- as.POSIXct("2005-09-01 01:25:46.9")
print(x) #note R didn't round up seconds, and added timezone

yr <- substring(x,1,4)
m <- substring(x,6,7)
d <- substring(x,9,10)
hour <- substring(x,12,13)
min <- substring(x,15,16)
sec <- substring(x,18,19) #note, I decided to truncate decimals,
#you don't have to

## This is simple and you only have to print a variable first, to see
## how it is arranged.

Convert data from multivariate (wide) to univariate (stacked/tall/long) form

Average: 5 (1 vote)

Our data in multivariate (wide) format:

my_data<- data.frame(
    id=c(1:50),
    depression1=rnorm(50),
    anxiety1=rnorm(50),
    depression2=rnorm(50),
    anxiety2=rnorm(50),
    depression3=rnorm(50),
    anxiety3=rnorm(50)
)
my_data

Read more after the jump.

Read more

Select time variables in a (multivariate/wide) data frame by name

No votes yet

This function will extract time labeled variables (or any variables with consistent naming) by name from a data frame in the order in which they appear in the data frame. This is especially useful in cases when one has a longitudinal or time-series data set where each row (subject) has many occasions of measurements for each measure. Code and examples after the jump.

Read more

Convert data from univariate (stacked/tall/long) to multivariate (wide) form

No votes yet

The function and documentation is listed after the jump.

Read more

Generating PDF graphs

No votes yet

Creating a PDF version of your graphs is very easy:

pdfname<-paste("myfilename",".pdf",sep="")
pdf(pdfname, height=6.4,width=6.4)
# Plotting code
dev.off()

More than one series in plot

No votes yet

From the r-help mailing list:

x <- rnorm(10)
y <- runif(10)
z <- 1:10

plot(x~z, ylim=range(x, y), type='l') lines(y, col='red')

Customize plot axes

No votes yet

If you're not happy with what R puts on the axes of your plot (tickmarks, labels etc), plot with "axes=F", then add custom axes with the "axis()" function.

Read more

Grid behind plot

No votes yet

You have a plot, and you want to draw a grid behind it. If you add the grid to the plot, using either

grid()
or
abline()
, the lines will be added on top of your plot, not behind it. I found the solution on the R-help mailing list, here. Redraw the plot after adding the grid, but first set
par(new=T)
so that it will be drawn on top of the old plot on the same device.

Read more

Replace NAs with zeroes

No votes yet

To replace all NA values in an object with zeroes:

x[is.na(x)] <- 0

If you want to do this only for some columns in a matrix or data frame, try this tip, which was posted by John Fox on the R-help mailing list:

cols <- c(1,3)
x[,cols][is.na(x[,cols])] <- 0

If you want to replace the NAs everywhere except in a single column, spare yourself some typing by:

cols <- -3

(then,

x[,cols][is.na(x[,cols])] <- 0
replaces everywhere except in the third column.)

Syndicate content