This file contains hidden or 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 DBMSs # | |
################################################# | |
# The ODBC Interface | |
library(RODBC) | |
myconn <-odbcConnect("mydsn", uid="Rob", pwd="xxxxxx") | |
crimedat <- sqlFetch(myconn, Crime) | |
pundat <- sqlQuery(myconn, "select * from Punishment") | |
close(myconn) |
This file contains hidden or 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 - Data Management # | |
#-----------------------------------------------------------------------------# | |
#-----------------------------------------------------------------------------# | |
install.packages(c('reshape', 'sqldf')) | |
This file contains hidden or 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
################################################# | |
## Recoding variables ## | |
################################################# | |
# Recoding categorical variable | |
leadership$agecat[leadership$age > 75] <- "Elder" | |
leadership$agecat[leadership$age > 45 & | |
leadership$age <= 75] <- "Middle Aged" | |
leadership$agecat[leadership$age <= 45] <- "Young" |
This file contains hidden or 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
################################################# | |
## Renaming variables ## | |
################################################# | |
# using the rename() function in the "reshape" package | |
install.packages("reshape") | |
library(reshape) | |
rename(leadership, c(manager = "managerID", date = "testDate")) |
This file contains hidden or 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
############################################################################# | |
## ------------------- Missing Values ----------------------########### | |
## 1. missing values in R are represented as NA (not available) # | |
## 2. Impossible values (devided by 0) are represented as NaN (not a number)# | |
############################################################################# | |
# Identify missing values using is.na() function | |
is.na(leadership[6:9]) | |
y <- c(1,2,3,NA) |
This file contains hidden or 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
############################################################## | |
## ------------ Date Values ------------- | |
## Dates are typically entered into R as character strings | |
## and then translated into date variables that are stored | |
## numerically | |
## | |
## Date format | |
## %d --> Day as a number (0-31) | |
## %a --> Abbreviated weekday (Mon, Tue, Fri) | |
## %A --> Unabbreviated weekday (Monday, Tuesday) |
This file contains hidden or 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 Type Conversion ------------- | |
## Converting one data type to another | |
## | |
## is.numeric() --> as.numeric() | |
## is.character() --> as.character() | |
## is.vector() --> as.vector() | |
## is.matrix() --> as.matrix() | |
## is.data.frame() --> as.data.frame() | |
## is.factor() --> as.factor() |
This file contains hidden or 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
################################################# | |
## SORT & MERGE ## | |
################################################# | |
################### | |
#Sorting a dataset | |
################### | |
attach(leadership) | |
newdata <- leadership[order(age), ] | |
newdata |
This file contains hidden or 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
################################################# | |
## Subsetting Dataset ## | |
################################################# | |
#################### | |
# Keeping variables | |
#################### | |
# method 1 |
This file contains hidden or 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 - Advanced Data Management # | |
#-----------------------------------------------------------------------------# | |
#-----------------------------------------------------------------------------# | |
################################################# | |
## Basic Math Functions ## | |
################################################# |