R语言数据重塑
R語言數據重塑
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 columns
new.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 zipcode
1???? Lowry??? CO?? 80230
2 Charlotte??? FL?? 33949
# # # The combined data frame
?????? city state zipcode
1???? Tampa??? FL?? 33602
2?? Seattle??? WA?? 98104
3? Hartford??? CT??? 6161
4??? Denver??? CO?? 80294
5???? Lowry??? CO?? 80230
6 Charlotte??? FL?? 33949
合并數據幀
我們可以通過使用 merge()函數合并兩個數據幀。該數據幀必須在其上合并發生相同的列名。
在下面的例子中,我們考慮對皮馬印第安人婦女的糖尿病在可用的數據集庫名稱 "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.y
1? 60 33.8?????? 1?? 117???? 23 0.466??? 27???? No?????? 2?? 125???? 20 0.088
2? 64 29.7?????? 2??? 75???? 24 0.370??? 33???? No?????? 2?? 100???? 23 0.368
3? 64 31.2?????? 5?? 189???? 33 0.583??? 29??? Yes?????? 3?? 158???? 13 0.295
4? 64 33.2?????? 4?? 117???? 27 0.230??? 24???? No?????? 1??? 96???? 27 0.289
5? 66 38.1?????? 3?? 115???? 39 0.150??? 28???? No?????? 1?? 114???? 36 0.289
6? 68 38.5?????? 2?? 100???? 25 0.324??? 26???? No?????? 7?? 129???? 49 0.439
7? 70 27.4?????? 1?? 116???? 28 0.204??? 21???? No?????? 0?? 124???? 20 0.254
8? 70 33.1?????? 4??? 91???? 32 0.446??? 22?
轉載于:https://www.cnblogs.com/amengduo/p/9587020.html
總結
- 上一篇: Orace 12.2 ORA-12012
- 下一篇: Web框架之Django篇