Getting Started with R

From PeformIQ Upgrade
Jump to navigation Jump to search

Also see - Facebook and R | R Notes | R Articles

Some Commands

Misc

data     <- read.table(filename,header=TRUE)           # Read tab or space delimited file with a header
csv_data <- read.table(filename,header=TRUE,sep=',')   # Read csv files with a header

v1       <- c(1,2,3,4,5,6,7)                           # Create a vector from these values
v2       <- c(1:10)                                    # Create a vector from 1 to 10
v3       <- c(v1,v2)                                   # Concatenate the vectors

matrix1  <- cbind(v1,v2)                               # Column bind into a 2 x n matrix

ls                                                     # List objects

Mean, SD, etc.

> sapply(h, length)
WList_Len 
     4599 
> sapply(h, mean)
WList_Len 
  15.6845 
> sapply(h, sd)
WList_Len 
 15.74873 
> 

Installing Modules

> install.packages('vars',,'http://cran.ms.unimelb.edu.au')
also installing the dependencies ‘zoo’, ‘strucchange’, ‘urca’, ‘lmtest’, ‘sandwich’

trying URL 'http://cran.ms.unimelb.edu.au/bin/windows/contrib/3.3/zoo_1.7-13.zip'
Content type 'application/zip' length 900080 bytes (878 KB)
downloaded 878 KB

trying URL 'http://cran.ms.unimelb.edu.au/bin/windows/contrib/3.3/strucchange_1.5-1.zip'
Content type 'application/zip' length 789069 bytes (770 KB)
downloaded 770 KB

trying URL 'http://cran.ms.unimelb.edu.au/bin/windows/contrib/3.3/urca_1.2-9.zip'
Content type 'application/zip' length 935642 bytes (913 KB)
downloaded 913 KB

trying URL 'http://cran.ms.unimelb.edu.au/bin/windows/contrib/3.3/lmtest_0.9-34.zip'
Content type 'application/zip' length 284171 bytes (277 KB)
downloaded 277 KB

trying URL 'http://cran.ms.unimelb.edu.au/bin/windows/contrib/3.3/sandwich_2.3-4.zip'
Content type 'application/zip' length 504789 bytes (492 KB)
downloaded 492 KB

trying URL 'http://cran.ms.unimelb.edu.au/bin/windows/contrib/3.3/vars_1.5-2.zip'
Content type 'application/zip' length 559244 bytes (546 KB)
downloaded 546 KB

package ‘zoo’ successfully unpacked and MD5 sums checked
package ‘strucchange’ successfully unpacked and MD5 sums checked
package ‘urca’ successfully unpacked and MD5 sums checked
package ‘lmtest’ successfully unpacked and MD5 sums checked
package ‘sandwich’ successfully unpacked and MD5 sums checked
package ‘vars’ successfully unpacked and MD5 sums checked
> install.packages("Rfacebook")
--- Please select a CRAN mirror for use in this session ---
also installing the dependencies ‘jsonlite’, ‘mime’, ‘curl’, ‘openssl’, ‘R6’, ‘Rcpp’, ‘httr’, ‘rjson’, ‘httpuv’

trying URL 'http://cran.ms.unimelb.edu.au/bin/windows/contrib/3.3/jsonlite_1.0.zip'
Content type 'application/zip' length 1095337 bytes (1.0 MB)
downloaded 1.0 MB

trying URL 'http://cran.ms.unimelb.edu.au/bin/windows/contrib/3.3/mime_0.5.zip'
Content type 'application/zip' length 37477 bytes (36 KB)
downloaded 36 KB

trying URL 'http://cran.ms.unimelb.edu.au/bin/windows/contrib/3.3/curl_1.1.zip'
Content type 'application/zip' length 4415226 bytes (4.2 MB)
downloaded 4.2 MB

trying URL 'http://cran.ms.unimelb.edu.au/bin/windows/contrib/3.3/openssl_0.9.4.zip'
Content type 'application/zip' length 3001737 bytes (2.9 MB)
downloaded 2.9 MB

trying URL 'http://cran.ms.unimelb.edu.au/bin/windows/contrib/3.3/R6_2.1.2.zip'
Content type 'application/zip' length 271199 bytes (264 KB)
downloaded 264 KB

trying URL 'http://cran.ms.unimelb.edu.au/bin/windows/contrib/3.3/Rcpp_0.12.6.zip'
Content type 'application/zip' length 3256650 bytes (3.1 MB)
downloaded 3.1 MB

trying URL 'http://cran.ms.unimelb.edu.au/bin/windows/contrib/3.3/httr_1.2.1.zip'
Content type 'application/zip' length 281284 bytes (274 KB)
downloaded 274 KB

trying URL 'http://cran.ms.unimelb.edu.au/bin/windows/contrib/3.3/rjson_0.2.15.zip'
Content type 'application/zip' length 561827 bytes (548 KB)
downloaded 548 KB

trying URL 'http://cran.ms.unimelb.edu.au/bin/windows/contrib/3.3/httpuv_1.3.3.zip'
Content type 'application/zip' length 895070 bytes (874 KB)
downloaded 874 KB

trying URL 'http://cran.ms.unimelb.edu.au/bin/windows/contrib/3.3/Rfacebook_0.6.6.zip'
Content type 'application/zip' length 79878 bytes (78 KB)
downloaded 78 KB

package ‘jsonlite’ successfully unpacked and MD5 sums checked
package ‘mime’ successfully unpacked and MD5 sums checked
package ‘curl’ successfully unpacked and MD5 sums checked
package ‘openssl’ successfully unpacked and MD5 sums checked
package ‘R6’ successfully unpacked and MD5 sums checked
package ‘Rcpp’ successfully unpacked and MD5 sums checked
package ‘httr’ successfully unpacked and MD5 sums checked
package ‘rjson’ successfully unpacked and MD5 sums checked
package ‘httpuv’ successfully unpacked and MD5 sums checked
package ‘Rfacebook’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
        C:\Users\xxx\AppData\Local\Temp\RtmpmkZC5k\downloaded_packages
> 

Some Links



Personality Project

Neil Gunther's 911 Help

See File:911r.pdf for the PDF document.

# Emergency help for R newbies
# Created by NJG on Fri Aug 15, 2011
# Updated by NJG on Mon Jan  9  2012

# 1. Set YOUR path to this 911.r script in the following variable:

# LUNUX:   Command needs to gnome-open
# WINDOWS: C:\\You path\\ needs "\\" to esc '\' and '9' in 991.r
# MACOSX:  Put in explicit full path name
path911 <- "/Users/njg/Desktop//911/"

# 2. Now run this script in the R Console.
# It is assumed that Acrobat Reader is installed.

help.911 <- function() {
	ost <- "unknown"
	switch(Sys.info()[['sysname']],
		Darwin  = {
			ost <- "Mac OS X"
			cmd <- "open"
		},
		Linux = {
			ost <- "Linux"
			cmd <- "gnome-open"
		},
		Windows = {
			ost <- "Windows"
			cmd <- "open"
		}
	)
	if(ost == "unknown") { stop("Unknown operating system") }
	setwd(path911)
	t <- try(system(paste(cmd, "911r.pdf")))
	if(t != 0) { stop(sprintf("system() call problem running on %s system", ost)) }
}

# 3. Usage
cat("Type 'help.911()' at anytime at the R Console prompt\n")
cat("to open the 911r.pdf file in Acrobat Reader.\n")
cat("Like this:\n")
cat("> help.911()\n")

help.911()

Command History

files <- list.files(pattern="*.csv")
files

data <- lapply(files, read.table, sep=",", header=T)
data

stuff <- as.data.frame(do.call(rbind, data))
stuff

dim(stuff)


print(dframe)
print(stuff)

mean(stuff)
sd(stuff)

sapply(stuff, length)
sapply(stuff, mean)
sapply(stuff, sd)

getwd()
ls()
ls
options()
history()
history(max.show=Inf)