數據分析中數據處理也就是data manipulation是十分繁瑣的,為此我將在博客里特意建一個分類:Data Manipulation。本文將講講如何在R語言中創建變量、重命名以及merge。
create a dataset
fy <- c(2010,2011,2012,2010,2011,2012,2010,2011,2012)
company <- c("Apple","Apple","Apple","Google","Google","Google","Microsoft","Microsoft","Microsoft")
revenue <- c(65225,108249,156508,29321,37905,50175,62484,69943,73723)
profit <- c(14013,25922,41733,8505,9737,10737,18760,23150,16978)
companiesData <- data.frame(fy, company, revenue, profit)
head(companiesData)
## fy company revenue profit
## 1 2010 Apple 65225 14013
## 2 2011 Apple 108249 25922
## 3 2012 Apple 156508 41733
## 4 2010 Google 29321 8505
## 5 2011 Google 37905 9737
## 6 2012 Google 50175 10737
接下來我們需要查看數據集的結構,用str()函數查看
str(companiesData)
## 'data.frame': 9 obs. of 4 variables:
## $ fy : num 2010 2011 2012 2010 2011 ...
## $ company: Factor w/ 3 levels "Apple","Google",..: 1 1 1 2 2 2 3 3 3
## $ revenue: num 65225 108249 156508 29321 37905 ...
## $ profit : num 14013 25922 41733 8505 9737 ...
可以看到年份fy這里是是數值型,我們需要更改為因子型,方便后期處理
companiesData$fy <- factor(companiesData$fy, ordered = TRUE)
現在數據已經整理過好了,下面我們來添加變量,比如我們可以看看各個公司的利潤率
companiesData$margin <- (companiesData$profit/companiesData$revenue)*100
#查看數據
head(companiesData)
## fy company revenue profit margin
## 1 2010 Apple 65225 14013 21.48409
## 2 2011 Apple 108249 25922 23.94664
## 3 2012 Apple 156508 41733 26.66509
## 4 2010 Google 29321 8505 29.00651
## 5 2011 Google 37905 9737 25.68790
## 6 2012 Google 50175 10737 21.39910
小數點位數太多了,這里我們保留一位
companiesData$margin <- round(companiesData$margin, 1)
head(companiesData)
## fy company revenue profit margin
## 1 2010 Apple 65225 14013 21.5
## 2 2011 Apple 108249 25922 23.9
## 3 2012 Apple 156508 41733 26.7
## 4 2010 Google 29321 8505 29.0
## 5 2011 Google 37905 9737 25.7
## 6 2012 Google 50175 10737 21.4
這樣我們就創建了一個新的變量margin,當然也可以刪除變量,只要復制需要刪除的變量NULL就行了。
#delete variable margin
companiesData$margin <- NULL
head(companiesData)
## fy company revenue profit
## 1 2010 Apple 65225 14013
## 2 2011 Apple 108249 25922
## 3 2012 Apple 156508 41733
## 4 2010 Google 29321 8505
## 5 2011 Google 37905 9737
## 6 2012 Google 50175 10737
再順便介紹一下transform函數,用于創建變量,transform的格式如下
dataFrame <- transform(dataFrame, newColumn = oldColumn1 + oldColumn2)
companiesData <- transform(companiesData, margin=round((profit/revenue)*100), 1)
head(companiesData)
## fy company revenue profit margin X1
## 1 2010 Apple 65225 14013 21 1
## 2 2011 Apple 108249 25922 24 1
## 3 2012 Apple 156508 41733 27 1
## 4 2010 Google 29321 8505 29 1
## 5 2011 Google 37905 9737 26 1
## 6 2012 Google 50175 10737 21 1
接下來講一下merge,主要是merge函數,它要求進行融合的兩個數據集需要有共同的變量即id,使用格式如下:
finaldt <- merge(dataset1, dataset2, by="id")
這里我們再創建一個數據集用于merge
#creat another dataset
company <- c("Apple","Google","Microsoft")
ava1 <- c(1,2,3)
data2 <- data.frame(company, ava1)
head(data2)
## company ava1
## 1 Apple 1
## 2 Google 2
## 3 Microsoft 3
數據集data2與數據集companiesData具有共同的變量company(id)
#merge the two dataset
newdata <- merge(companiesData, data2, by="company")
這樣就得到一個完整的數據集了,當然添加行、列還有兩個很有用的函數:rbind()以及cbind(),這里就不介紹了 最后講一下重命名,其實很簡單
companiesData$company <- c("A", "A", "A", "G", "G", "G", "M", "M", "M")
head(companiesData)
## fy company revenue profit margin X1
## 1 2010 A 65225 14013 21 1
## 2 2011 A 108249 25922 24 1
## 3 2012 A 156508 41733 27 1
## 4 2010 G 29321 8505 29 1
## 5 2011 G 37905 9737 26 1
## 6 2012 G 50175 10737 21 1
#rename the colname
colnames(companiesData) <- c("Year", "Com", "Rev", "Pro", "Mar")
seessioninfo
sessionInfo()
## R version 3.4.0 (2017-04-21)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 8.1 x64 (build 9600)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=Chinese (Simplified)_China.936
## [2] LC_CTYPE=Chinese (Simplified)_China.936
## [3] LC_MONETARY=Chinese (Simplified)_China.936
## [4] LC_NUMERIC=C
## [5] LC_TIME=Chinese (Simplified)_China.936
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## loaded via a namespace (and not attached):
## [1] compiler_3.4.0 backports_1.1.0 magrittr_1.5 rprojroot_1.2
## [5] tools_3.4.0 htmltools_0.3.6 yaml_2.1.14 Rcpp_0.12.11
## [9] stringi_1.1.5 rmarkdown_1.5 knitr_1.16 stringr_1.2.0
## [13] digest_0.6.12 evaluate_0.10
聯系方式:
wechat: yt056410
Email: tyan@zju.edu.cn
QQ: 1051927088
GitHub: https://github.com/YTLogos
簡書: http://www.lxweimin.com/u/bd001545cf0b
博客: https://ytlogos.github.io/
個人簡介:
嚴濤
浙江大學作物遺傳育種在讀研究生(生物信息學方向)
偽碼農,R語言愛好者,愛開源