Monday, January 06, 2014

Posting conventions I'll use for R

Since my main goal here is to provide usable code snippets, I'll try to standardize how I present said code.

I'll put all code in the Courier font.

citation() #the pound sign here is used for commenting in R code,
#so I will comment the code extensively so you can directly copy it into your 
#R script to modify as you need.
#this command in R gives you the citation for the R program.
#You can use it to get citations for individual packages that you have installed, too.
citation("lme4") #this cites the package lme4 if you have it installed.

#The quotes seem to be needed.  If you do not use the quotes around the package name, it gives this error #message:
#>Error in system.file(package = package, lib.loc = lib.loc) :
#>  object 'lme4' not found

#That brings me to output; I'll use ">" to indicate lines of output when it appears that way in R
# (as I just showed the error message).  If you ask to see data,
#it shows up as either the row names or as a number.

test.data<-c(0,1,2,3,4)
test.data
#[1] 0 1 2 3 4
#I'll usually comment the output and all the comments in and around the code so you can copy it directly
#into your script and edit the comments as you need without having to copy around it.

R.Version() #the caps are important.  R is case-sensitive.

I am currently using RStudio 0.97.314 with R 3.0.1, so my code is written and run under these conditions.  To find out what version you are using, if you're in the middle of the session and you don't see it in your output anymore, the above function lets you know.


No comments:

Post a Comment

Comments and suggestions welcome.