Jeff's recipes

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

Create a symmetric matrix by specifying upper or lower triangle

Average: 5 (1 vote)

The input is specified as the row-wise lower triangle or column-wise upper triangle.

Read more

Calling R from Python

No votes yet

This is an overview of RPy:

http://www.daimi.au.dk/~besen/TBiB2007/lecture-notes/rpy.html

While I've never used it, I can definitely see doing so, as I've recently become more and more a python programmer.

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()

Error suppression

No votes yet

Sometimes it is the case that you know your code will produce an error---or maybe you don't know, but you don't care. This is especially troublesome in loops. Here is how to essentially ignore the error allowing you to continue on as if it never happened.

Read more

Explode character vector into a list

No votes yet

I have a rectangular data file, where one variable is a column of quoted character vectors. This variable will need to be further broken into a list of substrings, split by some delimiter. It is in the data file this way because for each observation (each subject/each row), they may have any amount of these substrings. In PHP, there is a function called explode that splits a string into an array of substrings, based on some delimiter. In R, strsplit accomplishes the same thing.

Read more

Means for each level of a factor

Average: 5 (1 vote)

A person on the R-Help list wanted to find the mean of each group, based on ID. Their data looked like:

data<-data.frame(ID=rep(letters[1:4], 5), size=runif(20))

The person was trying to write a for loop, as most people generally do when first encountering R (I sure did), but as is the case with most procedures in R, what can be done with a for loop in other languages, can be done looplessly in R.

Read more

Remove all objects in the working environment

No votes yet

This is in the help files for rm, but for non-unix-ians, it seems to be hard to remember. Normally, if I am testing code that should stand on it's own, perhaps that I'll send off to the R-help list or put on here, I'll run this before I send it off:

rm(list=ls())

Read more

Syndicate content