---
title: "U09_ReadData"
author: "Feng-Li Lian"
date: "11/18/2020"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown
<http://rmarkdown.rstudio.com>.
**Knit** includes both content as well as the output

# U09: 檔案資料輸入與輸出
# 檔案資料輸入與輸出
# 設定工作目錄
```{r}
rm( list = ls() )
ls()

mywd <- "H:/DataR"

setwd( mywd )

getwd( )

```
# 資料儲存 – dump, source
# 儲存物件的名稱與其值（內容）
```{r}
x <- 1:10
y <- matrix( 1:6, nrow = 2, ncol = 3 )

x
y
ls()
```
# dump some data/command into a file
```{r}
ls()
dump( c("x", "y"), file = "mydump.txt")
ls()

```

# remove the variables
```{r}
rm(x); rm(y)
ls()
```
# read the content
```{r}
source( file = "mydump.txt" )
ls()
```

# 資料儲存 – dput, dget

# 儲存物件的值（內容）

```{r}
x <- 1:10
y <- matrix( 1:6, nrow = 2, ncol = 3 )

x
y
```
# save the data to file
```{r}
dput( y, file = "mydput.txt" )
```
# get the data from file
```{r}
newy <- dget( "mydput.txt" )
newy
```
# 資料儲存 – sink

# 螢幕輸出轉到檔案
```{r}
sink( "mysink.txt" )

x
y

```

```{r}
sink( )

x
y
```
# 資料儲存 – read.table, write.table
```{r}
dataf <- iris[c(1, 2, 51, 52, 101, 102), c(1, 2, 5)]
dataf
```
# edit
```{r}
dataf <- edit( dataf )
dataf
```
# write.table
```{r}
write.table( dataf, "mydataf.txt" )
```
# read again
```{r}
df <- read.table( "mydataf.txt" )
df
```

```{r}
sf0 <- read.table( "mydataf0.txt" )
sf0
```
```{r}
sf1 <- read.table( "mydataf1.txt", header = FALSE )
sf1
```

```{r}
sf2 <- read.table( "mydataf2.txt", header = TRUE, row.names = NULL )
sf2
```

```{r}
sf3 <- read.table( "mydataf3.txt", header = FALSE, row.names = NULL )
sf3
```
# 資料儲存 – read.table, read.csv

# csv: comma separated values
```{r}
read.table( "mydataf2.csv" )
```

```{r}
read.table( "mydataf2.csv", header = TRUE )
```

```{r}
read.table( "mydataf2.csv", header = TRUE, sep = "," )
```
```{r}
dt1 <- read.table( "mydataf2.csv", sep = ",", header = TRUE )
dt1
```

```{r}
dt2 <- read.csv( "mydataf2.csv" )
dt2
```

# 資料儲存 – read.table, read.csv

# 設定不同的目錄
```{r}
tphdata <- read.table( "H:/DataR/Typhoon-01.txt", header =  TRUE )
tphdata
```
# 資料儲存 – scan

# 讀取大量資料的文字檔
```{r}
scandata <- scan( "scanlist.txt", list( Sepal.Length = 0, Sepal.Width = 0, Species = "") )
scandata
```
# 讀取資料 – Insurance 例子

# 讀取資料
```{r}
Ins_c1 <- read.csv( "Insurance.csv" )
head( Ins_c1 )

```

```{r}
Ins_c2 <- read.table( "Insurance.csv" )
head( Ins_c2 )
```

```{r}
Ins_c3 <- read.table( "Insurance.csv", header = TRUE, sep="," )
head( Ins_c3 )
```

```{r}
Ins_t1 <- read.table( "Insurance.txt" )
head( Ins_t1 )
```

```{r}
Ins_t2 <- read.table( "Insurance.txt", header = TRUE, sep="" )
head( Ins_t2 )
```
# 讀取資料 – 從其他檔案格式

# 內建資料庫

# Datasets 資料集

# R 內建資料集：datasets

# 近百個資料集，涵蓋：醫學、自然、 社會、人體等資料
```{r}
data( package = "datasets" )

help( AirPassengers )

?AirPassengers

AirPassengers
```

```{r}
summary( AirPassengers )
```

```{r}
data( package = .packages( all.available = TRUE ))
```
# CO2		Carbon Dioxide Uptake in Grass Plants
```{r}
CO2
summary( CO2 )
```
# uspop		Populations Recorded by the US Census
```{r}
uspop
summary( uspop )
```

# Titanic		Survival of passengers on the Titanic
```{r}
Titanic
summary( Titanic )
```
# women		Average Heights and Weights for American Women
```{r}
women
summary( women )
```
# 其他軟體套件的資料集

# MASS
```{r}
library(MASS)
data( Insurance )
```

# 其他軟體套件的資料集data( Insurance )
```{r}
data( Insurance )
?Insurance
```

```{r}
head( Insurance )
tail( Insurance )
```



```{r}
dim( Insurance )
names( Insurance )
attributes( Insurance )
```

```{r}
class( Insurance$District )
class( Insurance$Age )
class( Insurance$Holders )
```

```{r}
levels( Insurance$Age )
attributes( Insurance )
```

# arules 軟體套件中，Groceries 資料集

```{r}
 install.packages( "arules" )
 library( arules )
```

```{r}
data( Groceries )

?Groceries
```

```{r}
Groceries[1:10]

inspect( Groceries[1:10] )		# 10位消費者購物車中的商品
```

# 其他資料庫的資料

# 網路上的資料庫

# 網頁的資料

# 讀取資料 – 從網路的檔案

# Reading a CSV-file from an URL

```{r}
read.csv("http://homepage.ntu.edu.tw/~fengli/Teaching/Computer/HW07_Phone.csv") -> HW07_Phone

#HW07_Phone
```

```{r}
read.csv("http://homepage.ntu.edu.tw/~fengli/Teaching/Computer/LearnTime_201116.csv") -> Data_LearnTime_file

#Data_LearnTime_file
```

```{r}
read.csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vS_zBSujPgYcwAyCxUu1wA8T35bxqme6k1JK8zuVgJnvrkPgwTbGmx0EfKhJYyPE-YiwHgU33S_RPWS/pub?gid=1947393170&single=true&output=csv") -> Data_LearnTime_web

#Data_LearnTime_web

```

# 和訊網 萬科 股票的相關金融資料

```{r}
install.packages("XML")
library(XML)
```

```{r}
url = "http://stockdata.stock.hexun.com/2008en/zxcwzb.aspX?stockid=000002&type=1&date=2013.06.30"
url
```

```{r}
tales1 = readHTMLTable( url )

#tales1
```
# 和訊網 萬科 股票的相關金融資料
```{r}
ls()
names(tales1)
tales1[[1]]
tales1[[2]]
tales1[[3]]
tales1[[4]]
```

```{r}
aa <- tales1[[2]]
aa
```
```{r}
class(aa)
```

```{r}
aa[1]
aa[2]
aa[3]
```


# 網頁
# 2016年美國總統選舉, ..., 競選資金


```{r}
install.packages( "XML" )
library(XML)

install.packages( "RCurl" )
library(RCurl)
```



```{r}
url <- "http://homepage.ntu.edu.tw/~fengli/Teaching/Computer/ReadFile/2016_USA_Election.html"

url
```

```{r}
html <- getURL( url )

# html
```

```{r}
doc = htmlParse(html, asText=TRUE)

# doc
```

```{r}
plain.text <- xpathSApply(doc, "//text()[not(ancestor::script)][not(ancestor::style)][not(ancestor::noscript)][not(ancestor::form)]", xmlValue)

# plain.text
```

```{r}
NN <- length( plain.text )
NN
```

```{r}
# get the 100-th element of the website file data
plain.text[100]

# do some comparison, check

plain.text[178] == "民主黨"

plain.text[179] == "民主黨"

plain.text[180] == "民主黨"

```


```{r}
# find the location of 競選資金

di1 <- matrix(0,10)
j <- 1
for ( i in 1:NN){
  if( plain.text[i] == "競選資金" ){
    di1[j] <- i
    j <- j + 1
  }
}

di1
j

```

```{r}
# find the location of 辯論

di2 <- matrix(0,10)
j <- 1
for ( i in 1:NN){
  if( plain.text[i] == "辯論" ){
    di2[j] <- i
    j <- j + 1
  }
}
di2
j
```


```{r}

# set up a set of keyword texts and find the length of the set

key.text <- plain.text[ di1[2]: di2[2] ]
key.text

NK <- length( key.text )
NK

```

```{r}
NK <- length( key.text )
NK
```

```{r}
# obtain the data within this area

# set up a 10x8 matrxi to store the numbers
data.main <- matrix(0, nrow = 10, ncol = 8)
data.main.num <- matrix(0, nrow = 10, ncol = 8)
```

```{r}
# match the keywords: name of candidates: 希拉莉·克林頓, 唐納·川普, 加里·約翰遜
for( i in 1:NK ){
  if( key.text[i] == "希拉莉·克林頓" ){
    for( k in 1:8 ){
      data.main[1,k] <- key.text[ i + 2 + k*2 ]
    }
  }
  if( key.text[i] == "唐納·川普" ){
    for( k in 1:8 ){
      data.main[2,k] <- key.text[ i + 2 + k*2 ]
    }
  }
  if( key.text[i] == "加里·約翰遜" ){
    for( k in 1:8 ){
      data.main[3,k] <- key.text[ i + 2 + k*2 ]
    }
  }
}

data.main
```

```{r}
# conver the data into numeric format
for( i in 1:10 ){
  for( j in 1:8 ){
    data.main.num[i,j] <- as.numeric(gsub('[$,]', '', data.main[i,j]))
  }
}

data.main.num

```

```{r}

```


# 網頁 
# 科比-布莱恩特/Kobe Bryant


```{r}
install.packages( "XML" )
library(XML)

install.packages( "RCurl" )
library(RCurl)
```



```{r}
url <- "http://www.stat-nba.com/player/195.html"
url
```

```{r}
html <- getURL( url )

# html
```

```{r}
doc = htmlParse(html, asText=TRUE)

# doc
```

```{r}
plain.text <- xpathSApply(doc, "//text()[not(ancestor::script)][not(ancestor::style)][not(ancestor::noscript)][not(ancestor::form)]", xmlValue)

# plain.text
```

```{r}
NN <- length( plain.text )
NN
```

```{r}
plain.text[991]
plain.text[990] == "洛杉矶湖人"
plain.text[991] == "洛杉矶湖人"
plain.text[992] == "洛杉矶湖人"
```


```{r}
di1 <- matrix(0,10)
plain.text[990] == "洛杉矶湖人"
plain.text[991] == "洛杉矶湖人"
plain.text[992] == "洛杉矶湖人"

```

```{r}
di1 <- matrix(0,10)
j <- 1

for ( i in 1:NN){
if( plain.text[i] == "洛杉矶湖人" ){
di1[j] <- i
j <- j + 1
}
}

di1
j
```

```{r}
di2 <- matrix(0,10)
j <- 1

for ( i in 1:NN){
if( plain.text[i] == "助攻" ){
di2[j] <- i
j <- j + 1
}
}

di2
j
```


```{r}
key.text <- plain.text[ di1[2]: di2[2] ]
key.text
```

```{r}
NK <- length( key.text )
NK
```

