90 likes | 107 Views
Getting Help on R. Manuals, Packages, and FAQs on R http://www.r-project.org/ (click on Manuals) help.start() (activates Manual) ?insert function name or for fuzzy matching help.search(“insert”) R archives: http://finzi.psych.upenn.edu/search.html Books on R
E N D
Getting Help on R • Manuals, Packages, and FAQs on R • http://www.r-project.org/ (click on Manuals) • help.start() (activates Manual) • ?insert function name or for fuzzy matching help.search(“insert”) • R archives: http://finzi.psych.upenn.edu/search.html • Books on R • Venables and Ripley (2002) **Classic** • Faraway (2006) • Bolker (2007) **My favourite** • R Ref Card • R help Digest: https://stat.ethz.ch/mailman/listinfo/r-help • Google it! Just type R and function of interest. • Ask other grad students, postdocs…
Getting Help • help.start() • ?read.table • http://www.zoology.ubc.ca/~ally/zoo502/zoo502.html
Bringing Data into R • click on R-stuff • download the Example Data set (use right click and Save Tgt Link) • create a text file – a new script or in Mac (a New Document) • comment your script files #Title: #Author: #Date originally written: #Date modified: • Using read.table() we will bring the example data into R as a start. • using setwd(dir) – this is especially good for when you set up loops later. • file.choose() – if the place your files lives is transient • assign your data a name like mydata mydata<-as.data.frame(read.table(file.choose()))
You need to change the header names so that they do not have odd symbols • a variable like Total VO2 (L.hr-1) has to be converted to something convenient like Total_VO2 • make sure all header names are on the same line • no spaces • units of measurement can be stored in your script file or commented out. What about missing data? • use the argument na.strings=“?” what happens? • or if blanks use fill=T
Getting data into R • read.table() has the following arguments: • file (the name of the file) • header=T (do you have a row with column names) • sep=“,” or “ “ or "\t“ • na.strings (tells you that missing data has ?) • as.is (very important) • skip (if you have written something can say skip=2, will skip first two lines of the datafile) • check.names • fill=T (blank fields added if empty cells) • blank.lines.skip=T (blank lines ignored) • comment.char=“#” • as. data.frame() vs. as.matrix
Check the data are the variables what you think they are? str(mydata) head(mydata,nrows) tail(mydata,nrows) sapply(mydata, class) sapply(mydata, mode) or if you want to combine the two use rbind rbind(class=sapply(x, class), mode=sapply(x, mode))
Basic data types • integer (signed) • numeric (real numbers) • logical (TRUE or FALSE) • character (alphanumeric strings) • dates • factors (how R deals with categorical variables)
not including as.is Plot Date Weight Total_VO2 Group Temperature class "integer" "factor" "factor" "factor" "factor" "integer" mode "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" character variables are reads as factors Plot Date Weight Total_VO2 Group Temperature class "integer" "character" "character" "character" "character" "integer" mode "numeric" "character" "character" "character" "character" "numeric" as.is=T
Managing the data • LONG vs WIDE formats • stack() unstack() • reshape() • melt/recast/cast() in library (reshape) • Subsetting data • subset() • library(doBy) • Indexing [,3] vs. [3,] • y<-x[!is.na(x)] • Replacements • mydata[4, “Weight”]=22.2 (Take the 4th observation and replace with value 22.2)