【正文】
1 2 3 4 九、數(shù)據(jù)列表 ? 數(shù)據(jù)列表 (list)是一組數(shù)據(jù)元素的集合 ,這些數(shù)據(jù)元素可以是不同的數(shù)據(jù)結(jié)構(gòu) (dim屬性 ,數(shù)據(jù)類(lèi)型等 ) ? list()函數(shù)可以組合任意對(duì)象 FamilyInfolist(hostname=joe,wife=rose,=3,=c(1,3,7)) FamilyInfo $hostname [1] joe $wife [1] rose $ [1] 3 $ [1] 1 3 7 FamilyInfo$wife 通過(guò)元素的名字來(lái)訪問(wèn) [1] rose“ FamilyInfo[3] 通過(guò)元素的編號(hào)來(lái)訪問(wèn) $ [1] 3 FamilyInfo[[3]] [1] 3 例子 數(shù)據(jù)列表操作 ? 長(zhǎng)度擴(kuò)展 length(FamilyInfo) [1] 4 FamilyInfo[5]list(address=c(10th F, HongXing Building, , JainYe Road)) FamilyInfo $hostname [1] joe $wife [1] rose $ [1] 3 $ [1] 1 3 7 [[5]] [1] 10th F, HongXing Building, , JainYe Road 數(shù)據(jù)列表合并 FamilyBirthdaylist(hostbirthday=1979/10/08,wifebirthday=1973/07/09) c(FamilyInfo,FamilyBirthday)Family 函數(shù) c()可以連接數(shù)據(jù)列表 Family $hostname [1] joe $wife [1] rose $ [1] 3 $ [1] 1 3 7 [[5]] [1] 10th F, HongXing Building, , JainYe Road $hostbirthday [1] 1979/10/08 $wifebirthday [1] 1973/07/09 數(shù)據(jù)框( data frame)是一個(gè)屬于 類(lèi)的列表。 矩陣,列表和數(shù)據(jù)框?yàn)樾碌臄?shù)據(jù)框提供了盡可能多的變量,因?yàn)樗鼈兏髯該碛辛?,元素或者變?。 在數(shù)據(jù)框中以變量形式出現(xiàn)的向量結(jié)構(gòu)必須長(zhǎng)度一致,矩陣結(jié)構(gòu)必須有一樣的行數(shù) . 數(shù)據(jù)框常常會(huì)被看作是一個(gè)由不同模式和屬性的列構(gòu)成的矩陣。 十、數(shù)據(jù)框 創(chuàng)建數(shù)據(jù)框 L3 LETTERS[1:3] d (cbind(x=1, y=1:10), fac=sample(L3, 10, replace=TRUE)) d x y fac 1 1 1 C 2 1 2 A 3 1 3 B 4 1 4 C 5 1 5 A 6 1 6 B 7 1 7 B 8 1 8 B 9 1 9 B 10 1 10 A d[1] x 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 d[[1]] [1] 1 1 1 1 1 1 1 1 1 1 mode(d[1]) [1] list mode(d[[1]]) [1] numeric length(d[1]) [1] 1 length(d[[1]]) [1] 10 d[[2]][3] [1] 3 d (cbind(x=1, y=1:10), + fac=sample(L3, 10, replace=TRUE), + HT=sample(c(H,T),10,replace=T)) d x y fac HT 1 1 1 B T 2 1 2 A H 3 1 3 B H 4 1 4 A T 5 1 5 C T 6 1 6 A T 7 1 7 B H 8 1 8 C T 9 1 9 C H 10 1 10 A T rbind(class=sapply(d, class), mode=sapply(d, mode)) x y fac HT class numeric numeric factor factor mode numeric numeric numeric numeric d (cbind(x=1, y=1:10), fac=I(sample(L3, 10, replace=TRUE)),HT=sample(c(H,T),10,replace=T)) d x y fac HT 1 1 1 A H 2 1 2 A T 3 1 3 C T 4 1 4 A T 5 1 5 B H 6 1 6 C H 7 1 7 C T 8 1 8 A H 9 1 9 A T 10 1 10 C H rbind(class=sapply(d, class), mode=sapply(d, mode)) x y fac HT class numeric numeric AsIs factor mode numeric numeric character numeric d[[3]] [1] A A C A B C C A A C d[[4]] [1] H T T T H H T H T H Levels: H T 函數(shù) I(): Change the class of an object to indicate that it should be treated ?as is? 十一、數(shù)據(jù)運(yùn)算 練習(xí) 熟練掌握數(shù)據(jù)運(yùn)算的有關(guān)符號(hào)和函數(shù) 十二、數(shù)據(jù)的選擇與操作 十三、高級(jí)數(shù)據(jù)處理 ? 對(duì)于向量 ,可以用函數(shù)直接對(duì)其進(jìn)行計(jì)算 ? 對(duì)于數(shù)組和矩陣 ,如果需要對(duì)其中一維 (或若干維 )進(jìn)行某種計(jì)算時(shí) ,則可以用 apply()函數(shù)自定義數(shù)組的操作 . ? apply(X,MARGIN,FUN,…) X給定數(shù)組, MARGIN指定維數(shù), FUN自定義計(jì)算函數(shù)。return(d)。retur