R語言data manipulation學(xué)習(xí)筆記之創(chuàng)建變量、重命名、數(shù)據(jù)融合

數(shù)據(jù)分析中數(shù)據(jù)處理也就是data manipulation是十分繁瑣的,為此我將在博客里特意建一個(gè)分類:Data Manipulation。本文將講講如何在R語言中創(chuàng)建變量、重命名以及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

接下來我們需要查看數(shù)據(jù)集的結(jié)構(gòu),用str()函數(shù)查看

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這里是是數(shù)值型,我們需要更改為因子型,方便后期處理

companiesData$fy <- factor(companiesData$fy, ordered = TRUE)

現(xiàn)在數(shù)據(jù)已經(jīng)整理過好了,下面我們來添加變量,比如我們可以看看各個(gè)公司的利潤率

companiesData$margin <- (companiesData$profit/companiesData$revenue)*100
#查看數(shù)據(jù)
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

小數(shù)點(diǎn)位數(shù)太多了,這里我們保留一位

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

這樣我們就創(chuàng)建了一個(gè)新的變量margin,當(dāng)然也可以刪除變量,只要復(fù)制需要?jiǎng)h除的變量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函數(shù),用于創(chuàng)建變量,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函數(shù),它要求進(jìn)行融合的兩個(gè)數(shù)據(jù)集需要有共同的變量即id,使用格式如下:

finaldt <- merge(dataset1, dataset2, by="id")

這里我們?cè)賱?chuàng)建一個(gè)數(shù)據(jù)集用于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

數(shù)據(jù)集data2與數(shù)據(jù)集companiesData具有共同的變量company(id)

#merge the two dataset
newdata <- merge(companiesData, data2, by="company")

這樣就得到一個(gè)完整的數(shù)據(jù)集了,當(dāng)然添加行、列還有兩個(gè)很有用的函數(shù):rbind()以及cbind(),這里就不介紹了 最后講一下重命名,其實(shí)很簡單

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

聯(lián)系方式:

wechat: yt056410
Email: tyan@zju.edu.cn
QQ: 1051927088
GitHub: https://github.com/YTLogos
簡書: http://www.itdecent.cn/u/bd001545cf0b
博客: https://ytlogos.github.io/

個(gè)人簡介:

嚴(yán)濤
浙江大學(xué)作物遺傳育種在讀研究生(生物信息學(xué)方向)
偽碼農(nóng),R語言愛好者,愛開源

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容