分類
R

R語言 資料處理技巧(1)

#資料處理技巧

#解構資料框

解構為單一變數值: 取出內建資料iris 第一筆觀測值得第五個變數 (Species)的值

iris[1,5]
iris[1,"Species"]

#解構為向量:取出iris的某一個變數
iris$Sepal.Length
iris[ , "Sepal.Length"]

#使用data.frame()建立資料框: 輸入5個向量結合成有5個變數的資料框
team_name<- c("Bull", "Warrior")
wins<- c(72,73)
losses<- c(10,9)
is_champion<- c(T,F)
season<- c("1995-96", "2015-16")
great_nba_teams<- data.frame(team_name, wins, losses, is_champion, season)

#透過指定[m,n]中索引值取出前六個觀測值與其中三個變數
iris[1:6, c("Sepal.Length", "Petal.Length", "Species")]
head(iris,6)
#利用邏輯值判斷部分的資料框
iris[iris$Petal.Length >=6, c("Sepal.Length", "Petal.Length", "Species")]

#假如需要加入更多邏輯值判斷,可以使用 & (and) 或是 | (or) 這樣的運算子連結判斷條件
filter<- (iris$Petal.Length >=6) & (iris$Sepal.Length >=7.5) #判斷那些觀察值符合邏輯判斷條件
iris[filter, c("Sepal.Length", "Petal.Length", "Species")] #filter告訴要列出哪幾個觀察值,後面則是指定要列出那些變數的觀察值

#&與&& |與||的差別

連結兩個單變數的判斷條件,不論是&與|或是 &&與||皆不會影響結果

1>2 && 1<2
1>2 || 1<2
1>2 & 1<2
1>2 | 1<2
#連結多個變數(如向量)判斷條件,使用&和|,而使用&&和||卻只會納入第一個索引值判斷
c(1,2,3)>c(3,2,1) & c(1,2,3)<c(3,2,1)
c(1,2,3)>c(3,2,1) | c(1,2,3)<c(3,2,1)
c(1,2,3)>c(3,2,1) && c(1,2,3)<c(3,2,1)
c(1,2,3)>c(3,2,1) || c(1,2,3)<c(3,2,1)

#新增與刪除一個變數
team_name<- c("Bull", "Warrior")
wins<- c(72,73)
losses<- c(10,9)
is_champion<- c(T,F)
great_nba_teams<- data.frame(team_name, wins, losses, is_champion) #原本的資料框
season<- c("1995-96", "2015-16") #預新增的變數
great_nba_teams$season<-season
great_nba_teams

#新增一個衍生的變數
team_name<- c("Bull", "Warrior")
wins<- c(72,73)
losses<- c(10,9)
is_champion<- c(T,F)
great_nba_teams<- data.frame(team_name, wins, losses, is_champion) #原本的資料框
great_nba_teams$winning_percentage<- great_nba_teams$wins/(great_nba_teams$wins + great_nba_teams$losses)
great_nba_teams

#刪除變數: 將預刪除的變數指派為NULL
team_name<- c("Bull", "Warrior")
wins<- c(72,73)
losses<- c(10,9)
is_champion<- c(T,F)
great_nba_teams<- data.frame(team_name, wins, losses, is_champion) #原本的資料框
great_nba_teams$is_champion<- NULL
great_nba_teams

#新增與刪除觀測值
team_name<- c("Bull", "Warrior")
wins<- c(72,73)
losses<- c(10,9)
is_champion<- c(T,F)
great_nba_teams<- data.frame(team_name, wins, losses, is_champion, stringsAsFactors = F) #原本的資料框,但需先將屬於"因素向量"的變數轉換回文字,在使用rbind(),row bind縮寫,否則會因新增因素向量的層級而造成錯誤。
laker<- c("Lakers", 69,13,T)
great_nba_teams<- rbind(great_nba_teams,laker)
great_nba_teams

#刪除觀測值
team_name<- c("Bull", "Warrior")
wins<- c(72,73)
losses<- c(10,9)
is_champion<- c(T,F)
great_nba_teams<- data.frame(team_name, wins, losses, is_champion)
great_nba_teams<- great_nba_teams[-2,] #刪除第二列的觀測值
great_nba_teams

#重新命名變數: names()
team_name<- c("Bull", "Warrior")
wins<- c(72,73)
losses<- c(10,9)
is_champion<- c(T,F)
great_nba_teams<- data.frame(team_name, wins, losses, is_champion)
names(great_nba_teams)[1:2]<- c("team","win")
great_nba_teams

#調整變數位置
team_name<- c("Bull", "Warrior")
wins<- c(72,73)
losses<- c(10,9)
is_champion<- c(T,F)
great_nba_teams<- data.frame(team_name, wins, losses, is_champion)
great_nba_teams<- great_nba_teams[ , c("is_champion", "wins", "losses","team_name")]
great_nba_teams<- data.frame( is_champion, wins, losses,team_name) #簡單點這樣即可,但如果原來的變數資料不是自己輸入的還是只能使用上面的方式

#對類別資料重新編碼
team_name<- c("Bull", "Warrior")
wins<- c(72,73)
losses<- c(10,9)
is_champion<- c(T,F)
great_nba_teams<- data.frame(team_name, wins, losses, is_champion)
great_nba_teams$is_champion[great_nba_teams$is_champion == T]<- "Y"
great_nba_teams$is_champion[great_nba_teams$is_champion == F]<- "N"
great_nba_teams

#將數值資料重新編碼為類別資料: 使用cut()選定要重新編碼的數值資料(height),以break()指定判斷的切點(0-130,130-140,140-Inf),Inf是無限大,並用label()給與切點區間標籤。
name<- c("Doraemon", "Nobita Nobi","Shizuka", "Giant", "Suneo", "Dorami")
height<- c(129.3, 140, 143, 145, 135, 100)
cartoon<- data.frame(name, height)
cartoon$height_category<- cut(cartoon$height, breaks = c(0,130,140,Inf), labels = c("<130","130-140",">140"))
cartoon

#合併data frame

垂直合併: rbind (column bind)

cars_upper<- cars[1:25, ]
cars_bottom<- cars[26:50, ]
rbind(cars_upper,cars_bottom)

#水平合併: cbind (column bind)
cars_left<- cars[ ,1]
cars_right<- cars[ ,2]
cbind(cars_left,cars_right)

#依照某一變數進行合併: merge()
name<- c( "Nobita Nobi","Shizuka", "Giant", "Suneo", "Dorami")
height<- c( 140, 143, 145, 135, 100)
left_df<- data.frame(name,height)
name<- c("Doraemon", "Nobita Nobi","Shizuka","Suneo")
color<- c("blue" ,"yellow", "pink", "purple")
right_df<- data.frame(name, color)
merge(left_df,right_df, all= T) #merge()只會取交集,all =T的話就可以列出所有觀測值,缺少的資料以NA表示。all.x和all.y=T分別是保留右邊跟左邊的資料。

#如果變數名稱都不相同時,merge()會出現笛卡爾連接(Cartesian Join)
name<- c( "Nobita Nobi","Shizuka", "Giant", "Suneo", "Dorami")
height<- c( 140, 143, 145, 135, 100)
left_df<- data.frame(name,height)
name_2<- c("Doraemon", "Nobita Nobi","Shizuka","Suneo")
color<- c("blue" ,"yellow", "pink", "purple")
right_df<- data.frame(name_2, color)
merge(left_df,right_df) 
#加上by.x= "name" , by.y="name_2" left_df依照"name"而right_df依照"name_2"即可解決
merge(left_df, right_df, by.x= "name" ,by.y="name_2") 

#練習題: 在favorite_bands_df 新增一個變數 formed_years,這個變數以系統日期計算樂團成立幾周年
favorite_bands_df<- data.frame(band= c("Beyond", "Beatles"), lead_vocal= c("Wong", "John"), formed= c(1983, 1960))
favorite_bands_df$formed_years<- as.numeric(format(Sys.time(), "%Y")) -favorite_bands_df$formed

 

提供R script參考( 格式為 CP950)

參考書籍: 輕鬆學習R語言:從基礎到應用,掌握資料科學的關鍵能力

 

發表迴響