R中的數據重塑是關于改變數據組織成行和列的方式.大多數情況下,R中的數據處理是通過將輸入數據作為數據幀來完成的.從數據幀的行和列中提取數據很容易,但有些情況下我們需要的數據幀格式與我們收到它的格式不同. R有許多功能可以在數據框中拆分,合并和更改行到列,反之亦然.
在數據框中連接列和行
我們可以使用 cbind()函數連接多個向量來創建數據框.我們也可以使用 rbind()函數合并兩個數據幀.
# Create vector objects.city <- c("Tampa","Seattle","Hartford","Denver")state <- c("FL","WA","CT","CO")zipcode <- c(33602,98104,06161,80294)# Combine above three vectors into one data frame.addresses <- cbind(city,state,zipcode)# Print a header.cat("# # # # The First data frame\n") # Print the data frame.print(addresses)# Create another data frame with similar columnsnew.address <- data.frame( city = c("Lowry","Charlotte"), state = c("CO","FL"), zipcode = c("80230","33949"), stringsAsFactors = FALSE)# Print a header.cat("# # # The Second data frame\n") # Print the data frame.print(new.address)# Combine rows form both the data frames.all.addresses <- rbind(addresses,new.address)# Print a header.cat("# # # The combined data frame\n") # Print the result.print(all.addresses)
當我們執行上面的代碼時,它會產生以下結果 :
# # # # The First data frame city state zipcode[1,] "Tampa" "FL" "33602"[2,] "Seattle" "WA" "98104"[3,] "Hartford" "CT" "6161" [4,] "Denver" "CO" "80294"# # # The Second data frame city state zipcode1 Lowry CO 802302 Charlotte FL 33949# # # The combined data frame city state zipcode1 Tampa FL 336022 Seattle WA 981043 Hartford CT 61614 Denver CO 802945 Lowry CO 802306 Charlotte FL 33949
合并數據框
我們可以使用合并合并兩個數據框( )功能.數據框必須具有相同的列名稱才能進行合并.
在下面的示例中,我們認為Pima Indian Women中的糖尿病數據集可在庫名稱"MASS"中找到.我們基于血壓("bp")和體重指數("bmi")的值合并兩個數據集.在選擇這兩列進行合并時,這兩個變量的值在兩個數據集中匹配的記錄被組合在一起形成一個數據框.
library(MASS)merged.Pima <- merge(x = Pima.te, y = Pima.tr, by.x = c("bp", "bmi"), by.y = c("bp", "bmi"))print(merged.Pima)nrow(merged.Pima)
當我們執行上面的代碼時,它產生以下結果 :
bp bmi npreg.x glu.x skin.x ped.x age.x type.x npreg.y glu.y skin.y ped.y1 60 33.8 1 117 23 0.466 27 No 2 125 20 0.0882 64 29.7 2 75 24 0.370 33 No 2 100 23 0.3683 64 31.2 5 189 33 0.583 29 Yes 3 158 13 0.2954 64 33.2 4 117 27 0.230 24 No 1 96 27 0.2895 66 38.1 3 115 39 0.150 28 No 1 114 36 0.2896 68 38.5 2 100 25 0.324 26 No 7 129 49 0.4397 70 27.4 1 116 28 0.204 21 No 0 124 20 0.2548 70 33.1 4 91 32 0.446 22 No 9 123 44 0.3749 70 35.4 9 124 33 0.282 34 No 6 134 23 0.54210 72 25.6 1 157 21 0.123 24 No 4 99 17 0.29411 72 37.7 5 95 33 0.370 27 No 6 103 32 0.32412 74 25.9 9 134 33 0.460 81 No 8 126 38 0.16213 74 25.9 1 95 21 0.673 36 No 8 126 38 0.16214 78 27.6 5 88 30 0.258 37 No 6 125 31 0.56515 78 27.6 10 122 31 0.512 45 No 6 125 31 0.56516 78 39.4 2 112 50 0.175 24 No 4 112 40 0.23617 88 34.5 1 117 24 0.403 40 Yes 4 127 11 0.598 age.y type.y1 31 No2 21 No3 24 No4 21 No5 21 No6 43 Yes7 36 Yes8 40 No9 29 Yes10 28 No11 55 No12 39 No13 39 No14 49 Yes15 49 Yes16 38 No17 28 No[1] 17
融化和鑄造
R編程最有趣的一個方面是以多個步驟改變數據的形狀以獲得所需的形狀.用于執行此操作的函數稱為 melt()和 cast().
我們認為稱為船舶的數據集存在于庫名為"MASS".
library(MASS)print(ships)
當我們執行上面的代碼時,它產生以下結果 :
type year period service incidents1 A 60 60 127 02 A 60 75 63 03 A 65 60 1095 34 A 65 75 1095 45 A 70 60 1512 6..........................8 A 75 75 2244 119 B 60 60 44882 3910 B 60 75 17176 2911 B 65 60 28609 58........................17 C 60 60 1179 118 C 60 75 552 119 C 65 60 781 0........................
融化數據
現在我們將數據融合以組織它,將除type和year之外的所有列轉換為多行.
molten.ships <- melt(ships, id = c("type","year"))print(molten.ships)
當我們執行上面的代碼時,它產生以下結果 :
type year variable value1 A 60 period 602 A 60 period 753 A 65 period 604 A 65 period 75........................9 B 60 period 6010 B 60 period 7511 B 65 period 6012 B 65 period 7513 B 70 period 60......................41 A 60 service 12742 A 60 service 6343 A 65 service 1095......................70 D 70 service 120871 D 75 service 072 D 75 service 205173 E 60 service 4574 E 60 service 075 E 65 service 789......................101 C 70 incidents 6102 C 70 incidents 2103 C 75 incidents 0104 C 75 incidents 1105 D 60 incidents 0106 D 60 incidents 0......................
施放熔融數據
我們可以將熔融數據轉換為新形式,其中每年創建每種類型船舶的總量.它是使用 cast()函數完成的.
recasted.ship <- cast(molten.ships, type+year~variable,sum)print(recasted.ship)
當我們執行上面的代碼時,它產生以下結果 :
type year period service incidents1 A 60 135 190 02 A 65 135 2190 73 A 70 135 4865 244 A 75 135 2244 115 B 60 135 62058 686 B 65 135 48979 1117 B 70 135 20163 568 B 75 135 7117 189 C 60 135 1731 210 C 65 135 1457 111 C 70 135 2731 812 C 75 135 274 113 D 60 135 356 014 D 65 135 480 015 D 70 135 1557 1316 D 75 135 2051 417 E 60 135 45 018 E 65 135 1226 1419 E 70 135 3318 1720 E 75 135 542 1
免責聲明:以上內容(如有圖片或視頻亦包括在內)有轉載其他網站資源,如有侵權請聯系刪除