R語言實戰 - 基本資料管理(3)

來源:互聯網
上載者:User

標籤:argument   merge   log   set   end   eve   ror   ...   實戰   

8. 資料排序

> leadership$age[1] 32 45 25 39 NA> newdata <- leadership[order(leadership$age),]> newdata  manager   testDate country gender age item1 item2 item3 item4 item53       3 2008-10-01      UK      F  25     3     5     5     5     21       1 2008-10-24      US      M  32     5     4     5     5     54       4 2008-10-12      UK      M  39     3     3     4    NA    NA2       2 2008-10-28      US      F  45     3     5     2     5     55       5 2009-05-01      UK      F  NA     2     2     1     2     1  stringAsFactors agecat3           FALSE  Young1           FALSE  Young4           FALSE  Young2           FALSE  Young5           FALSE   <NA>> > > attach(leadership)The following objects are masked _by_ .GlobalEnv:    age, country, gender, manager> newdata <- leadership[order(gender, age),]> detach(leadership)> newdata  manager   testDate country gender age item1 item2 item3 item4 item53       3 2008-10-01      UK      F  25     3     5     5     5     22       2 2008-10-28      US      F  45     3     5     2     5     55       5 2009-05-01      UK      F  NA     2     2     1     2     11       1 2008-10-24      US      M  32     5     4     5     5     54       4 2008-10-12      UK      M  39     3     3     4    NA    NA  stringAsFactors agecat3           FALSE  Young2           FALSE  Young5           FALSE   <NA>1           FALSE  Young4           FALSE  Young> > attach(leadership)The following objects are masked _by_ .GlobalEnv:    age, country, gender, manager> newdata <- leadership[order(gender, -age),]> detach(leadership)> newdata  manager   testDate country gender age item1 item2 item3 item4 item55       5 2009-05-01      UK      F  NA     2     2     1     2     12       2 2008-10-28      US      F  45     3     5     2     5     53       3 2008-10-01      UK      F  25     3     5     5     5     24       4 2008-10-12      UK      M  39     3     3     4    NA    NA1       1 2008-10-24      US      M  32     5     4     5     5     5  stringAsFactors agecat5           FALSE   <NA>2           FALSE  Young3           FALSE  Young4           FALSE  Young1           FALSE  Young> 

9. 資料集的合并

9.1 添加列

> patientID <- c(1, 2, 3, 4)> age <- c(25, 34, 28, 52)> status <- c("poor", "improved", "excellent", "poor")> gender <- c("F", "M", "M", "F")> dataframeA <- data.frame(patientID, gender)> dataframeA  patientID gender1         1      F2         2      M3         3      M4         4      F> dataframeB <- data.frame(patientID, age, status)> dataframeB  patientID age    status1         1  25      poor2         2  34  improved3         3  28 excellent4         4  52      poor> total <- merge(dataframeA, dataframeB, by="ID")Error in fix.by(by.x, x) : ‘by‘ must specify a uniquely valid column> total <- merge(dataframeA, dataframeB, by="patientID")> total  patientID gender age    status1         1      F  25      poor2         2      M  34  improved3         3      M  28 excellent4         4      F  52      poor> total <- merge(dataframeA, dataframeB, by=c("gender", "age"))Error in fix.by(by.x, x) : ‘by‘ must specify a uniquely valid column> total <- merge(dataframeA, dataframeB, by=c("patientID", "age"))Error in fix.by(by.x, x) : ‘by‘ must specify a uniquely valid column> > total <- cbind(dataframeA, dataframeB)> total  patientID gender patientID age    status1         1      F         1  25      poor2         2      M         2  34  improved3         3      M         3  28 excellent4         4      F         4  52      poor> 

9.2 添加行

> total <- rbind(dataframeA, dataframeB)Error in rbind(deparse.level, ...) :   numbers of columns of arguments do not match

10. 資料集取子集

10.1 選入(保留)變數

10.2 剔除(丟棄)變數

10.3 選入觀測

10.4 subset() 函數

10.5 隨機抽樣

 

R語言實戰 - 基本資料管理(3)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.