This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-----------------------------------------------------------------------------# | |
#-----------------------------------------------------------------------------# | |
# 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=",") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-----------------------------------------------------------------------------# | |
# R in Action - R Data Structures # | |
# - Working with Vectors, Matrices, Arrays,Dataframes & Lists # | |
#-----------------------------------------------------------------------------# | |
################################# | |
# Vectors # | |
################################# | |
# creating vectors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################# | |
# Matrix # | |
################################# | |
# Creating Matrices | |
y <- matrix(1:20, nrow = 5, ncol = 4) | |
y | |
# fill by rows | |
cells <- c(1, 26, 24, 68) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################# | |
# 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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################# | |
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################# | |
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-----------------------------------------------------------------------------# | |
#-----------------------------------------------------------------------------# | |
# R in Action - Importing Data # | |
#-----------------------------------------------------------------------------# | |
#-----------------------------------------------------------------------------# | |
######################################## | |
# Entering Data from Keyboard # | |
######################################## |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################# | |
# 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,] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################# | |
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################# | |
# 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; |
OlderNewer