Skip to content

Instantly share code, notes, and snippets.

@geofferyzh
geofferyzh / gist:2400262
Created April 16, 2012 17:46
RinAction - R Graph Parameter Basics
#-----------------------------------------------------------------------------#
#-----------------------------------------------------------------------------#
# R in Action - Working with graphs #
#-----------------------------------------------------------------------------#
#-----------------------------------------------------------------------------#
setwd <- "C:/RinAction/data/workspace"
setwd <- "C:\RinAction\data\workspace"
mtcars <- read.table("C:/RinAction/data/mtcars.csv", header=TRUE, sep=",")
@geofferyzh
geofferyzh / gist:2400391
Created April 16, 2012 18:04
RinAction - R Data Structure - Vectors
#-----------------------------------------------------------------------------#
# R in Action - R Data Structures #
# - Working with Vectors, Matrices, Arrays,Dataframes & Lists #
#-----------------------------------------------------------------------------#
#################################
# Vectors #
#################################
# creating vectors
@geofferyzh
geofferyzh / gist:2400394
Created April 16, 2012 18:05
RinAction - R Data Structure - Matrix
#################################
# Matrix #
#################################
# Creating Matrices
y <- matrix(1:20, nrow = 5, ncol = 4)
y
# fill by rows
cells <- c(1, 26, 24, 68)
@geofferyzh
geofferyzh / gist:2400401
Created April 16, 2012 18:06
RinAction - R Data Structure - Arrays
#################################
# Arrays #
#################################
# Creating an array
dim1 <- c("A1", "A2")
dim2 <- c("B1", "B2", "B3")
dim3 <- c("C1", "C2", "C3", "C4")
z <- array(1:24, c(2, 3, 4), dimnames = list(dim1, dim2, dim3))
@geofferyzh
geofferyzh / gist:2400414
Created April 16, 2012 18:07
RinAction - R Data Structure - DataFrame
#################################
# Data Frame #
#################################
# Creating a dataframe
patientID <- c(1, 2, 3, 4)
age <- c(25, 34, 28, 52)
diabetes <- c("Type1", "Type2", "Type1", "Type1")
status <- c("Poor", "Improved", "Excellent", "Poor")
patientdata <- data.frame(patientID, age, diabetes, status)
@geofferyzh
geofferyzh / gist:2400424
Created April 16, 2012 18:08
RinAction - R Data Structure - Lists
#################################
# Lists #
#################################
# Creating a list
g <- "My First List"
h <- c(25, 26, 18, 39)
j <- matrix(1:10, nrow = 5)
k <- c("one", "two", "three")
mylist <- list(title = g, ages = h, m=j, k)
@geofferyzh
geofferyzh / gist:2400436
Created April 16, 2012 18:10
RinAction - R Importing Data - Keyboard
#-----------------------------------------------------------------------------#
#-----------------------------------------------------------------------------#
# R in Action - Importing Data #
#-----------------------------------------------------------------------------#
#-----------------------------------------------------------------------------#
########################################
# Entering Data from Keyboard #
########################################
@geofferyzh
geofferyzh / gist:2400448
Created April 16, 2012 18:13
RinAction - R Importing Data - Text
#################################################
# Import data from delimited text file #
#################################################
help(read.table)
mydataframe <- read.table("C:/RinAction/data/data", header=TRUE, sep="\t",row.names="X")
mydataframe
str(mydataframe)
mydataframe[1,]
@geofferyzh
geofferyzh / gist:2400454
Created April 16, 2012 18:14
RinAction - R Importing Data - Excel
#################################################
# Import data from Excel #
#################################################
# the best way to read an Excel file is to export it to a comma-delimted file from
# within Excel and import it to R using the method described earlier
# RODBC on Windows
install.packages("RODBC")
library(RODBC)
@geofferyzh
geofferyzh / gist:2400459
Created April 16, 2012 18:15
RinAction - R Importing Data - SAS
#################################################
# Import data from SAS #
#################################################
# Save SAS dataset as comma-delimited text file using PROC EXPORT
proc export data=mydata
outfile="mydata.csv"
dbms=csv;
run;