前輩們都說,要理解一個程式語言,第一步就是先了解資料結構,因為每個程式語言的資料都用其特殊的資料結構方式來儲存。而 R 最具特色的,就是它的基本資料單位是「向量(Vector)」~
1.1 資料型態
常用的 R 資料型態有:
integer – 整數
numeric – 實數
logic – TRUE / FALSE 布林變數
character – 文字字串,通常用 “” 包起來
—
A. integer 和 numeric 一起看的話,囊括了整數(沒有小數點)與浮點數(有小數點)這兩種數值。R 預設的數字型態是 numeric,如果沒有特別指定成 integer (可用 as.integer()
函式),就會是 numeric
> class(12)
[1] "numeric"
> class(as.integer(12))
[1] "integer"
B. 布林代數(Boolean) – 是用來作邏輯上的判斷,在 R 中英文大小寫是有差的,logic 資料型態都要大寫,不可寫 True,但可以簡化成 T
> boolT <- TRUE
> str(boolT) #探究 boolT的型態
logi TRUE
C. 字串 – 無論是文字還是數字,用雙引號 “” 括起來的值會被定義為字串格式。字串是不能進行數值的加減乘除等運算的哦~
> a <- "Denny"
> class (a)
[1] "character"
1.2 資料結構
R 語言的資料結構挺多樣的,二煎拌個人偏愛的分成一到多維,以下詳述
—
A. 一維:向量 (vector)、因子 (factor)
向量 – 在 R 裡面,向量是儲存一維資料的變數類型,用函式 c() (combine)
建立起向量,但要記得裡面的元素都要是同一種的資料屬性哦~
如果放入向量裡的元素資料型態不同,會被轉成同一種,以強到弱排序:character > numeric > integer > logical
> threetimes <- c(3, 6, 9, 12, 15)
> namelist <- c("Denny", "Derick", "Mark")
> threetimes
[1] 3 6 9 12 15
> namelist
[1] "Denny" "Derick" "Mark"
因子 – 有前輩說像是經過分級之後的向量,同時具有字串與整數的特性,會儲存 (1) 所有不重複的數值作為標籤 (labels),會被當成字串 (character),(2) 每一個元素對應到的標籤。
因子在 R 語言的建模中非常好用,可以用在統計上的迴歸分析與實際設計等
> m <- c(3, 3, 2, 1, 4, 1, 3, 2, 2)
> factor(m, labels = c("甲", "乙", "丙", "丁")) # 可自訂 Level 的名稱
[1] 丙 丙 乙 甲 丁 甲 丙 乙 乙
Levels: 甲 乙 丙 丁
B. 二維:矩陣 (matrix)、資料框架 (data.frame)
矩陣 – 其實就是我們高中時(遠目)學的矩陣,R 定義矩陣的方式很單純,就是用 matrix()
函式
> x <- matrix(c(1:6), nrow=3, ncol=4)
> x
[,1] [,2] [,3] [,4]
[1,] 1 4 1 4
[2,] 2 5 2 5
[3,] 3 6 3 6
資料框架 – 很像資料表、大量資料集,也有前輩說,如果 Excel用來分析計算的資料單位是Sheet,那R所對應的就是 data frame~我們可使用函式 data.frame()
來創建新的資料框架,它具備:
Rows(列):通常代表的是個體 Individuals / 觀察對象 Observations
Columns(欄位):通常是變數 Variables
> StudentDF <- data.frame(name = c("Meredis", "George", "Eze"),
age = c(25, 26, 24),
gender = c("F", "M", "F"))
> StudentDF
name age gender
1 Meredis 25 F
2 George 26 M
3 Eze 24 F
C. 多維:陣列 (array)、列表 (list)
陣列 – 可以看作是多維度的向量變數類型,跟向量一樣,所有陣列裡元素的資料型態必須是一致的哦~
> a <- c("Yellow", "Red", "Blue")
> b <- c("Green", "Black", "Purple")
> rbind(a, b)
[,1] [,2] [,3]
a "Yellow" "Red" "Blue"
b "Green" "Black" "Purple"
列表 – 向量、因子、陣列都只能儲存同一種資料型態的元素,使用上彈性略顯不足,而列表list 的彈性就大出許多,類似 Ruby 的 Hash~在列表中,元素可分屬不同資料型態,我們可使用 list()
函式來建立~
> list_1 <- list(gender="M", age=18, emotion=c("confused", "exciting"), sin, 33)
> list_1
$gender
[1] "M"
$age
[1] 18
$emotion
[1] "confused" "exciting"
[[4]]
function (x) .Primitive("sin")
[[5]]
[1] 33
Usually they do not interfere with a LASIK patient s vision and no additional treatment is needed buy priligy usa
how to buy priligy in usa reviews In contrast, individual tissue responses differed while death of islets of Ins2 Cre; Mos iCsp3 mice and death of epidermis in Krt14 CreER; Mos iCsp3 mice were accompanied by cell replacement, regrowth of hair in the latter was limited
The number of the oldest old, those 85 years of age and older, is expected to increase from 6 dapoxetina comprar online If you want to challenge it then how might you best go about this
L theanine, a water soluble, non protein amino acid commonly found in green tea and some mushrooms, has been widely studied for its ability to encourage wakeful relaxation without sedation generic name for cytotec levitra fda metformin recall october 2020 At around the same time, BMC was fighting with the IRS overits transfer pricing practices
Thanks for sharing. I read many of your blog posts, cool, your blog is very good.
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?