# Holcombe:ProgrammingInR

(Difference between revisions)
 Revision as of 18:59, 10 March 2011 (view source)← Previous diff Revision as of 23:22, 10 March 2011 (view source) (→dataframe stuff)Next diff → Line 18: Line 18: - ===dataframe stuff=== + ===dataframe tips=== Examining your data frame or object, let's say it's called ''datos'' Examining your data frame or object, let's say it's called ''datos'' typeof(datos) #returns "list!" typeof(datos) #returns "list!" Line 44: Line 44: Check your counterbalancing in your results file. Make a contingency table, Check your counterbalancing in your results file. Make a contingency table, table(dataRaw\$speed,dataRaw\$relPhaseOuterRing) table(dataRaw\$speed,dataRaw\$relPhaseOuterRing) + + Replace certain value with another + thr\$thresh[  thr\$task=='ident' ] = NA ==Creating Graphs (usu. ggplot2)== ==Creating Graphs (usu. ggplot2)==

## Revision as of 23:22, 10 March 2011

### Members

Alex Holcombe
Polly Barr
• Charlie Ludowici
• Kim Ransley
• Ingrid Van Tongeren
William Ngiam
Fahed Jbarah
• Patrick Goodbourn
Alumni

### Other

R is an interactive programming language for statistics. The syntax is very idiosyncratic, and not really in a good way. Try R for programmers for a description. However it may have menu-driven versions maybe available R commander we haven't tried that and another one is pmg GTK maybe here

In the lab we have the book Using R for Introductory Statistics. R_Statistics introduces you to R

Dani has posted some example code and graphs on his personal website.

R reference cheatsheet, also a file here Media:Matlab-python-xref.pdf‎ that gives equivalent code for doing array operations in MATLAB, Python, and R plot parameters

There is a wiki with some good tips here. Also Data frame tips, list of R websites

Functions in R can only return one parameter.

### dataframe tips

Examining your data frame or object, let's say it's called datos

```typeof(datos) #returns "list!"
str(datos) #tells you it's a dataframe, number of observations, columns, etc
str(datos)
summary(datos) #good for ggplot objects also
```
```df\$varWithExtraLevels = factor(df\$varWithExtraLevels)
```
```length(df) #number of columns of dataframe
```
```names(df) #names of columns of dataframe
```
```library(Hmisc);
describe(df)
```
```#Calling typeof() on a dataframe returns "list"
```
```rm(objectToBeDeleted)
rm(list = ls())  #Delete nearly everything in memory
```

expand.grid() to create dataframe with every combination of some factors

```table(dataRaw\$speed,dataRaw\$relPhaseOuterRing)
```

Replace certain value with another

```thr\$thresh[  thr\$task=='ident' ] = NA
```

## Creating Graphs (usu. ggplot2)

how I Holcombe:fit psychometric functions and bootstrap

## Debugging in R

How to examine and try things with a questionable variable within a function?

``` ee <<- resultsMeans #make global, violating all principles of good coding #DEBUG
STOP
```

After an error, calling traceback() gives you the stack

## doing ANOVAs etc

I think I had too many error terms reducing error terms

Anovas with repeated measures can be complicated in R.

We have some R books in the lab

## Dealing with circular data

von Mises vs. wrapped Gaussian,

see Swindale, N. V. (1998). Orientation tuning curves: empirical description and estimation of parameters. Biol Cybern, 78(1), 45-56.

## Setting up a proxy in R on a Mac

The easiest way to set up a proxy is simply to create a file called ".Rprofile" (make sure the file does note have a .txt or .rtf extension. Textwrangler will automatically give it an extension, it may be best to create the file using Vi in a terminal - using emacs probably won't work) in your user directory (~ or /Users/username/) with the line: ``` ```

``` Sys.setenv(http_proxy=”http://username:password@tcdproxy.tcd.ie:8080″) ```

Then restart R. This information (and more) can be found on Ken Benoit's webpage

For Sydney Uni, use: ``` ```

``` Sys.setenv(http_proxy=”http://www-cache.usyd.edu.au:8080″) ```