2014年8月8日 星期五

Coursera 上課筆記 - R Programming - 第一週 - Data Types and Subsetting

Course note for R programming - Data Types and Subsetting
R programming 第一週的筆記之一! 

不是什麼正式的學習筆記,可以未來查看我學過什麼+複習XD


基本操作指令:

getwd() #get working directory
setwd("D:/Coursera/R programming/R code") #set working dirctory
setwd("D:/Coursera/R programming/Programming Assignment 1 Air Pollution")
setwd("D:/Coursera/R programming/rprog-data-ProgAssignment3-data")

ls() #看目前R有什麼變數
dir() #看資料夾有什麼東西
source("test.R") #load檔案

搜尋資料:
?+function名字   #?rnorm
help.search("rnorm") #不一定要完整的函式名,可隨意搜尋, 或是使用 ??rnorm

args("rnorm") #函式參數資訊
直接打函式名,會跳出資訊

str(function) #會跳出function的基本資訊
class(object) #回傳類別形態

Data Types and Subsetting:

用 c(1, 3) or vector("logical", 4) 作vector
c可以把東西連起來成一個vector
c("csdf", "sfds")
c(4, 5)

summary() #可以有很多資訊跳出來
mean() #算平均數
names() #可以把vectors裡面的元素都加上名字
ex: names(x) <- c("foo", "bar", "jio")

建二維vectors: matrix
ma = matrix(1:6, nrow=3, ncol=2)
也可以有名字
mc = matrix(1:6, nrow=2, ncol=2)
dimnames(mc) <- list(c("a", "b"), c(1, 5))

m = cbind(ma, mb)是結合matrix
還可以用rbind, 結合matrix以row為基準
nrow(m) [1] 3
ncol(m) [1] 3
dim(m)  [1] 3 3

t(m) 可以transpose matrix, 行列交換
diag(m) 可以取對角線

用 %*% 可以作matrix相乘
可以直接對matrix 作運算、或邏輯比較

list 可以放不同class的元素到 [] 中
x = list(1, "a", TRUE)
也可以放名字
x = list(a = 1, b = 2, c = 3)

lm()  #Fitting Linear Models
glm() #Fitting Generalized Linear Models


factor #儲存lable(levels), 可自己設定, 放data用
x = factor(c('yes', 'no', 'yes'))
table(x) #計算不同的level有幾個
unclass(x) #把level轉成數字印出


x =factor(c('yes', 'no', 'yes', 'yes'), levels = c('yes', 'no'))
跟電腦說 base/first level 是'yes'
要是不設定,電腦會依照字母排序(就會是'no'變成 first level)


rnorm() : standard normal distribution
rep(NA, 1000) : Replicate Elements of Vectors and Lists, 這邊是重複1000個NA
sample(x, size): 在x禮面取出size個元素


NaN 代表不是一個數字 ex: 0/0
Inf 代表無極限 , Inf-Inf = NaN

is.na()測是是否NA
is.nan()測是否NaN

NaN可以包Na
Na不是NaN


frame是特殊的list,可以放不同的class type, 但是要有相同的長度。

matrix 只有同一種class   #ex: cbind(patients,my_matrix)
frame可以放不同class    #ex: my_data <- data.frame(patients, my_matrix)
(可以用這個方式指定row的名稱)

指定名字給不同column
cnames <- c("patient", "age", "weight", "bp", "rating", "test")
colnames(my_data) <- cnames

frame 可用read.table() or read.csv()
也可轉成matrix, 用data.matrix() (會強制轉換成同一type)
ex: x <- data.frame(foo= 1:4, bar = c(T,F, T, T))


[type] 回傳同個type出來,而非list type
[[]] 用再取出list/data frame的單個資料
$用再取出list/data frame"有名字"的資料

> x[1:4]
[1] 1 2 3 4
> x[x>3]
[1] 4 5
> u = x>3
> u
[1] FALSE FALSE FALSE  TRUE  TRUE
> x[u]
[1] 4 5


x= matrix(1:6, 2, 3)
> x[1, 2, drop=FALSE]
     [,1]
[1,]    3
drop可以丟掉一個dimention

> x <- list(foo = 1:4, 0.6)
> x[1] 得到一個list, 包含一個sequence
$foo
[1] 1 2 3 4
> x[[1]] 只有一個sequence
[1] 1 2 3 4


complete.cases()可用來移除 NA

x <- c(1, 2, NA, 4, NA, 5)
y <- c("a", "b", NA, "d", NA, "f")
good <- complete.cases(x, y)

complete.cases() 也可以用在frame

paste("Hello", "world!", collapse = " ")
[1] "Hello world!"
paste(1:3, c("X", "Y", "X"), collapse = "")
[1] "1 X2 Y3 X"
paste(1:3, c("X", "Y", "X"), sep = "")
[1] "1X" "2Y" "3X"

R裡面可以使用負數的index, 會從後面倒回去。
R accepts negative integer indexes. Whereas x[c(2, 10)]
| gives us ONLY the 2nd and 10th elements of x, x[c(-2, -10)]
| gives us all elements of x EXCEPT for the 2nd and 10 elements.
| Try x[c(-2, -10)] now to see this.

x[-c(2, 10)] = x[c(-2, -10)]


如何製造空的vector?
How to create a vector of zero length in R?

numeric()
logical()
character()
integer()
double()
raw()
complex()
vector('numeric')
vector('character')
vector('integer')
vector('double')
vector('raw')
vector('complex')
All return 0 length vectors of the appropriate atomic modes.

# the following will also return objects with length 0
list()
expression()
vector('list')
vector('expression')



If you want to use (copy, paste or quote) my original article, please contact me through email. (autek.roy@gmail.com) If there is any mistake or comment, please let me know. :D

如要使用(複製貼上或轉載)作者原創文章, 請來信跟我聯絡。(autek.roy@gmail.com) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )

沒有留言:

張貼留言

請留下您的任何想法或建議!
Please leave any thought or comment!