Sept.24
readxl
different sheets with tabular data
excel_sheets() list different sheets
read_excel() actually import data into R
dir()
excel_sheets(“cities.xlsx”)
read_excel(“cities.xlsx”,sheet=2)
read_excel(“”cities.xlsx,sheet=“year_2000”)
col_names=c()
col_type=c(“text”,”blank”)
col_type=NULL
skip=2 n_max is not available
# Print out the first observation from urbanpop_sel
urbanpop_sel[1,]
gdata
read.xls()
xls-csv-r frame
Extend the cbind() call so that it also includes urban_sheet3. Make sure the first column of urban_sheet2 and urban_sheet3 are removed, so you don't have duplicate columns. Store the result in urban.
urban <- cbind(urban_sheet1, urban_sheet2[-1],urban_sheet3[-1])
XLConnect bridge between Excel and R
loadWorkbook()
getSheets()
readWorksheet(loadWorkbook(),sheet=“”)
startRow=
endRow=
startCol=
adapting sheets
createSheet(book,name=“year_2010”)
writeWorksheet(book,pop_2010,sheet=“year_2020)
saveWorkbook(book,file=“cities2.txt”)
renameSheet(book,””,””)
saveWorkbook(book,file=“cities3.xlsx”)
removeSheet(book,sheet=“”)
Sept.25
connect to a database
what is relational database
how to connect
how to read table
database management system
con<-dbConnect(RMySQL::MySQL(),dbname=,host=,port=,user=,password=)
import table data
dbListTables(con)
dbReadTable(con,”employees”)
dbDisconnect(con)
tables <- lapply(table_names,dbReadTable, conn = con)
SQL Queries from inside R
selective importing
dbGetQuery(con,”SELECT name FROM employees WHERE started_at > “2012-09-01” ”)
products<-dbReadTable(con,”products”)
subset(products,subset=contract==1)
CHAR_LENGTH return the numbers of characters in a string
short<-dbGetQuery(con,"SELECT id,name FROM users WHERE CHAR_LENGTH(name)<5")
SELECT name, post
FROM users INNER JOIN tweats on users.id = user_id
WHERE date > "2015-09-19"
res<-dbSendQuery()
dbFetch(res)
while(!dbHasCompleted(res)){chunk<-dbFetch(res,n=1) print(chunk)}
dbClearResult()
HTTP HyperText Transfer Protocol , language of web
read.csv()
read.tsv()
read.csv() is different from read_csv() (need to install read)
HTTPS support since R version 3.2.2
download file
Sept.26
# Specification of url: url_xls
url_xls <- "http://s3.amazonaws.com/assets.datacamp.com/production/course_1478/datasets/latitude.xls"
# Import the .xls file with gdata: excel_gdata
excel_gdata<-read.xls(url_xls)
# Download file behind URL, name it local_latitude.xls
local_latitude.xls<-download.file(url_xls,destfile="local_latitude.xls")
# Import the local .xls file with readxl: excel_readxl
excel_readxl<-read_excel(local_latitude)
url<-
dest_path<-file.path(“~”,”local_cities.xlsx”)
download.file(url,dest_path)
read_excel(dest_path)
load data from an RData file using the load()
# Load the httr package
library(httr)
# Get the url, save response to resp
url <- "http://www.example.com/"
resp<-GET(url)
# Print resp
resp
# Get the raw content of resp: raw_content
raw_content<-content(resp,as="raw")
# Print the head of raw_content
print(head(raw_content))
APIs JSON
API Application Programming Interface
jsonlite
fromJSON()
fromJSON() can convert character strings that represent JSON data into a nicely structured R list.
JSON object {“id”=1,””}
JSON array [1,2,3,”kkkk”]
fromJSON(‘[]’)
toJSON()
prettify()
minify()
haven foreign
SAS Statistics Analysis Software .sas7bdat .sas7bcat
STATA statistics and data .dta
SPSS statistical package for social science .sax .por
haven single argument:path to file result:R data frame
read_sas()
read_stata() read_dta()
ontime$Airline<-as.charaactor(as_factor(ontime$Airline))
read_spss() read_por() read_sav()
subset(traits,Extroversion>40 & Agreeableness>40)
foreign
read.dta(file,convert.factors=TRUE,convert.dates=TRUE,missing.type=FALSE)
convert.factors convert labelled STATA values to R factors
convert.dates convert STATA dates and times to Date and POSIXct
missing.type if FALSE,convert types of all missing values to NA,if TRUE,store how values are missing in attributes
read.spss(file, use.value.labels=TRUE,to.data.frame=FALSE)
use.value.labels=TRUE convertt labelled SPSS values to R factors
to.data.frame return data frame instead of a list
boxplot(a$b)