一、常用包的安裝以及工作環(huán)境初始配置
此處代碼都是引自果子老師的《學(xué)習(xí)R語(yǔ)言镀赌,從這一課開(kāi)始》,還是留存?zhèn)溆脼楹谩?/p>
if(length(getOption("CRAN"))==0) options(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/") if(!require("tidyr")) install.packages("tidyr",update = F,ask = F) if(!require("dplyr")) install.packages("dplyr",update = F,ask = F) if(!require("ggplot2")) install.packages("ggplot2",update = F,ask = F) if(!require("data.table")) install.packages("data.table",update = F,ask = F) if(!require("ggrepel")) install.packages("ggrepel",update = F,ask = F) if(!require("devtools")) install.packages("devtools",update = F,ask = F) if(!require("pheatmap")) install.packages("pheatmap",update = F,ask = F) if(!require("ggfortify")) install.packages("ggfortify",update = F,ask = F) if(!require("stringr")) install.packages("stringr",update = F,ask = F) if(!require("survival")) install.packages("survival",update = F,ask = F) if(!require("survminer")) install.packages("survminer",update = F,ask = F) if(!require("glmnet")) install.packages("glmnet",update = F,ask = F) if(!require("ggpubr")) install.packages("ggpubr",update = F,ask = F) if(!require("tibble")) install.packages("tibble",update = F,ask = F) if(!require("cowplot")) install.packages("cowplot",update = F,ask = F) if(!require("timeROC")) install.packages("timeROC",update = F,ask = F) if(!require("survivalROC")) install.packages("survivalROC",update = F,ask = F) if(!require("randomForest")) install.packages("randomForest",update = F,ask = F) if(!require("Hmisc")) install.packages("Hmisc",update = F,ask = F) if(!require("jsonlite")) install.packages("jsonlite",update = F,ask = F) if(!require("ggstatsplot")) install.packages("ggstatsplot",update = F,ask = F)
if(!require("BiocManager")) install.packages("BiocManager",update = F,ask = F) if(length(getOption("BioC_mirror"))==0) options> (BioC_mirror="https://mirrors.ustc.edu.cn/bioc/") if(!require("limma")) BiocManager::install("limma",update = F,ask = F) if(!require("Biobase")) BiocManager::install("Biobase",update = F,ask = F) if(!require("caret")) install.packages("caret",update = F,ask = F) if(!require("IRanges")) BiocManager::install("IRanges",update = F,ask = F) if(!require("DO.db")) BiocManager::install("DO.db",update = F,ask = F) if(!require("GO.db")) BiocManager::install("GO.db",update = F,ask = F) if(!require("fgsea")) BiocManager::install("fgsea",update = F,ask = F) if(!require("clusterProfiler")) BiocManager::install("clusterProfiler",update = F,ask = F) if(!require("GEOquery")) BiocManager::install("GEOquery",update = F,ask = F) if(!require("hugene10sttranscriptcluster.db")) BiocManager::install("hugene10sttranscriptcluster.db",update = F,ask = F) if(!require("DOSE")) BiocManager::install("DOSE",update = F,ask = F) if(!require("RTCGA.clinical")) BiocManager::install("RTCGA.clinical",update = F,ask = F) if(!require("bladderbatch")) BiocManager::install("bladderbatch",update = F,ask = F) if(!require("sva")) BiocManager::install("sva",update = F,ask = F) if(!require("DESeq2")) BiocManager::install("DESeq2",update = F,ask = F) if(!require("edgeR")) BiocManager::install("edgeR",update = F,ask = F)
以上包安裝完畢之后等恐,先查看工作路徑绵咱,后根據(jù)需要設(shè)置自己的工作路徑,如下命令:
getwd()
setwd()
##get/set workdictionary
二恩尾、向量與數(shù)據(jù)框
R中的數(shù)據(jù)類(lèi)型有五種弛说,分別是向量(Vector),矩陣(Matrix)翰意,數(shù)組(Array)木人,數(shù)據(jù)框(Data Frame),列表(List)冀偶;而元素的類(lèi)型醒第,有數(shù)值型、字符型进鸠、邏輯型、因子型等客年。向量是一維的單一類(lèi)型的元素集合蒲列,矩陣是二維的單一類(lèi)型的元素集合,數(shù)組是多維的單一類(lèi)型的元素集合搀罢,數(shù)據(jù)框是一個(gè)二維或多維的元素集合蝗岖,相當(dāng)于excel表格,列表是一個(gè)虛擬的大容器榔至,什么都可以裝抵赢。
- 向量
向量的提取方法,是在向量后加中括號(hào)唧取,其用法引自老師小潔的教程:
x -< rep(1:100, 2) x[4] x[-4] x[2:4] x[-2:4] x[c(1, 5)] x[x == 10] x[x < 0] x[x %% 2 ==0] x[x %in% c(1, 2, 5)]
- 數(shù)據(jù)框
讀文件和寫(xiě)文件
常用的命令是read.csv
,read.table
,read.xlsx
,write.csv
,write.table
,write.xlsx
铅鲤。提取元素,相比向量枫弟,除了[]邢享,多了$,$相當(dāng)于數(shù)據(jù)框中的一個(gè)向量的名稱(chēng)淡诗,也就是其中的某一列骇塘,在數(shù)據(jù)框里,列是變量韩容,行是觀測(cè)對(duì)象款违。
以下引自老師小潔的文章
x[4,] x[,4]/x[4] x[4,4] x[1:4] x[1:4, 1:4] x[c(1, 4)] x[c(1, 4), c(1, 4)] x$name
- attach&with的用法
- attach和detach要配對(duì)使用,否則后續(xù)可能遇到未知錯(cuò)誤群凶。
- attach相當(dāng)于把一個(gè)數(shù)據(jù)暫時(shí)賦值為全局環(huán)境插爹,如果不用detach的命令,attach將持續(xù)有用,而with僅僅是在括號(hào)里的代碼對(duì)數(shù)據(jù)進(jìn)行一個(gè)短暫的全局賦值赠尾,出了with代碼力穗,自動(dòng)失效,而代碼里如果存在賦值的變量气嫁,變量不予保留睛廊,要保留,需使用
<<-
杉编。