羅
羅左欣 8 years ago
可以參考 http://meliodaseren.blogspot.tw/
羅
羅左欣 8 years ago
要查一下"sourse()"和"dget()"之間的差異
羅
羅左欣 8 years ago
要查一下"load()"和"unserialize()"之間的差異
羅
羅左欣 8 years ago
要查一下"dump()"和"dput()"之間的差異
- 20150916 [Coursera] R Programming (4)
- 整理自 R Programming (Week 1) -- Reading Data
- [ Week 1 課程內容 ]
- [ 本次筆記內容 ] Reading Data
- (一)Reading Tabular Data
- [ 重點整理 ]
- 1. 讀取資料用的函數
- 2. 寫入資料用的函數
- 3. read.table()
- (二)Reading Large Tables
- [ 重點整理 ]
- 1. 如何增加讀取大型數據的效率? >> read.table()
- 2. 了解系統配備
- 3. 計算需要使用的記憶體
- (三)Textual Data Formats
- [ 重點整理 ]
- 1. R的文本操作
- 2. 寫入資料 << dput
- 3. 讀取資料 << dump
- (四)Connections: Interfaces to the Outside World
- [ 重點整理 ]
- 1. R與外部進行聯繫與互動的界面
- 2. 檔案的聯繫
- (2)open 檔案的參數
- 3. 讀取文件或網站 << readLines()、url()
- (1)讀取文件 -- readLines()
- (2)讀取網站 -- url()、readLines()
整理自 R Programming (Week 1) -- Reading Data
Introduction(略過)Overview and History of R[16:07](完成)Getting Help[13:53] (略過)Console Input and Evaluation[4:46](完成)Data Types - R Objects and Attributes[4:43](完成)Data Types - Vectors and Lists[6:27](完成)Data Types - Matrices[3:24](完成)Data Types - Factors[4:31](完成)Data Types - Missing Values[2:10](完成)Data Types - Data Frames[2:44](完成)Data Types - Names Attribute[1:49](完成)Data Types - Summary[0:43](完成)(一)Reading Tabular Data
data <- read.table("foo.txt") 預設情況下除了文件名稱,不需要加上其他參數
(二)Reading Large Tables
(三)Textual Data Formats
> y <- data.frame(a = 1, b = "a")
> dput(y)
structure(list(a = 1,
b = structure(1L, .Label = "a",
class = "factor")),
.Names = c("a", "b"), row.names = c(NA, -1L),
class = "data.frame")
> dput(y, file = "y.R")
> new.y <- dget("y.R")
> new.y
a b
1 1 a
> x <- "foo"
> y <- data.frame(a = 1, b = "a")
> dump(c("x", "y"), file = "data.R")
> rm(x, y)
> source("data.R")
> y
a b
1 1 a
> x
[1] "foo"
(四)Connections: Interfaces to the Outside World
> str(file)
function (description = "", open = "", blocking = TRUE,
encoding = getOption("encoding"))
con <- file("foo.txt", "r")
data <- read.csv(con)
close(con)
data <- read.csv("foo.txt")
> con <- gzfile("words.gz")
> x <- readLines(con, 10) # 用readLines()讀取文件內容的前10行
> x
[1] "1080" "10-point" "10th" "11-point"
[5] "12-point" "16-point" "18-point" "1st"
[9] "2" "20-point"
## This might take time
con <- url("http://www.jhsph.edu", "r") # 用url()建立一個網站的聯繫
x <- readLines(con) # 用readLines()讀取網頁的元素
> head(x)
[1] "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">"
[2] ""
[3] "<html>"
[4] "<head>"
[5] "\t<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8