首页 >> 改装 >> R语言学习 - 框架概念和矩阵操作

R语言学习 - 框架概念和矩阵操作

2025-08-17 12:16:29

8407356 -0.91251181 0.17997973 0.50452258[19] 0.25961316 -0.58052230 1.00000000 2.00000000 3.00000000 4.00000000[25] 5.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000[31] 2.00000000 10.00000000 11.00000000 13.00000000 4.00000000 -1.26491106[37] -0.63245553 0.00000000 0.63245553 1.26491106> a <- matrix(a, ncol=5, byrow=T)> a [,1] [,2] [,3] [,4] [,5][1,] -0.4125356 0.1219293 -0.4763589 -0.9717165 1.09162243[2,] 1.8778966 -0.1171794 2.9295352 1.3383662 -0.03269026[3,] 0.8754092 0.1300574 0.1190069 0.7666394 0.28407356[4,] -0.9125118 0.1799797 0.5045226 0.2596132 -0.58052230[5,] 1.0000000 2.0000000 3.0000000 4.0000000 5.00000000[6,] 0.0000000 0.0000000 0.0000000 0.0000000 0.00000000[7,] 2.0000000 10.0000000 11.0000000 13.0000000 4.00000000[8,] -1.2649111 -0.6324555 0.0000000 0.6324555 1.26491106# 求行的延和> rowSums(a)[1] -0.6470593 5.9959284 2.1751865 -0.5489186 15.0000000 0.0000000 40.0000000[8] 0.0000000## 提醒检查括弧的配对> a <- a[rowSums(abs(a)!=0,]错误: 意外的']' in "a <- a[rowSums(abs(a)!=0,]"# 消除全部为0的行> a <- a[rowSums(abs(a))!=0,]# 另外一种方式消除全部为0的行> #a[rowSums(a==0) a [,1] [,2] [,3] [,4] [,5][1,] -0.4125356 0.1219293 -0.4763589 -0.9717165 1.09162243[2,] 1.8778966 -0.1171794 2.9295352 1.3383662 -0.03269026[3,] 0.8754092 0.1300574 0.1190069 0.7666394 0.28407356[4,] -0.9125118 0.1799797 0.5045226 0.2596132 -0.58052230[5,] 1.0000000 2.0000000 3.0000000 4.0000000 5.00000000[6,] 2.0000000 10.0000000 11.0000000 13.0000000 4.00000000[7,] -1.2649111 -0.6324555 0.0000000 0.6324555 1.26491106# 乘积运算,R默认针对整个统计总共据进行相似运算# 所有系总共都等于2> a * 2 [,1] [,2] [,3] [,4] [,5][1,] -0.8250711 0.2438586 -0.9527178 -1.9434331 2.18324487[2,] 3.7557931 -0.2343587 5.8590704 2.6767324 -0.06538051[3,] 1.7508184 0.2601149 0.2380137 1.5332788 0.56814712[4,] -1.8250236 0.3599595 1.0090452 0.5192263 -1.16104460[5,] 2.0000000 4.0000000 6.0000000 8.0000000 10.00000000[6,] 4.0000000 20.0000000 22.0000000 26.0000000 8.00000000[7,] -2.5298221 -1.2649111 0.0000000 1.2649111 2.52982213# 所有系总共取意味著系总共,再取对总共 (取对总共当年一般延一个总共避免对0或负系总共取对总共)> log2(abs(a)+1) [,1] [,2] [,3] [,4] [,5][1,] 0.4982872 0.1659818 0.5620435 0.9794522 1.0646224[2,] 1.5250147 0.1598608 1.9743587 1.2255009 0.0464076[3,] 0.9072054 0.1763961 0.1622189 0.8210076 0.3607278[4,] 0.9354687 0.2387621 0.5893058 0.3329807 0.6604014[5,] 1.0000000 1.5849625 2.0000000 2.3219281 2.5849625[6,] 1.5849625 3.4594316 3.5849625 3.8073549 2.3219281[7,] 1.1794544 0.7070437 0.0000000 0.7070437 1.1794544# 取出最大系总共、最小系总共、至多、罗列总共> max(a)[1] 13> min(a)[1] -1.264911> nrow(a)[1] 7> ncol(a)[1] 5# 增延一罗列或四人# cbind: column bind> cbind(a, 1:7) [,1] [,2] [,3] [,4] [,5] [,6][1,] -0.4125356 0.1219293 -0.4763589 -0.9717165 1.09162243 1[2,] 1.8778966 -0.1171794 2.9295352 1.3383662 -0.03269026 2[3,] 0.8754092 0.1300574 0.1190069 0.7666394 0.28407356 3[4,] -0.9125118 0.1799797 0.5045226 0.2596132 -0.58052230 4[5,] 1.0000000 2.0000000 3.0000000 4.0000000 5.00000000 5[6,] 2.0000000 10.0000000 11.0000000 13.0000000 4.00000000 6[7,] -1.2649111 -0.6324555 0.0000000 0.6324555 1.26491106 7> cbind(a, seven=1:7) seven[1,] -0.4125356 0.1219293 -0.4763589 -0.9717165 1.09162243 1[2,] 1.8778966 -0.1171794 2.9295352 1.3383662 -0.03269026 2[3,] 0.8754092 0.1300574 0.1190069 0.7666394 0.28407356 3[4,] -0.9125118 0.1799797 0.5045226 0.2596132 -0.58052230 4[5,] 1.0000000 2.0000000 3.0000000 4.0000000 5.00000000 5[6,] 2.0000000 10.0000000 11.0000000 13.0000000 4.00000000 6[7,] -1.2649111 -0.6324555 0.0000000 0.6324555 1.26491106 7# rbind: row bind> rbind(a,1:5) [,1] [,2] [,3] [,4] [,5][1,] -0.4125356 0.1219293 -0.4763589 -0.9717165 1.09162243[2,] 1.8778966 -0.1171794 2.9295352 1.3383662 -0.03269026[3,] 0.8754092 0.1300574 0.1190069 0.7666394 0.28407356[4,] -0.9125118 0.1799797 0.5045226 0.2596132 -0.58052230[5,] 1.0000000 2.0000000 3.0000000 4.0000000 5.00000000[6,] 2.0000000 10.0000000 11.0000000 13.0000000 4.00000000[7,] -1.2649111 -0.6324555 0.0000000 0.6324555 1.26491106[8,] 1.0000000 2.0000000 3.0000000 4.0000000 5.00000000# 计数每四人的mad (中系总共意味著误差,一般并不认为比方差的鲁棒性更强,相当多所致所致系总共的影响,更能凸显统计总共据间的差异)> apply(a,1,mad)[1] 0.7923976 2.0327283 0.2447279 0.4811672 1.4826000 4.4478000 0.9376786# 计数每四人的var (方差)# apply声称对统计总共据(第一个实例)的每四人 (第二个实例赋系总共为1) 或每一罗列 (2)操作方法# 最后返回一个表罗列出> apply(a,1,var)[1] 0.6160264 1.6811161 0.1298913 0.3659391 2.5000000 22.5000000 1.0000000# 计数每一罗列的平均系总共> apply(a,2,mean)[1] 0.4519068 1.6689045 2.4395294 2.7179083 1.5753421# 取出中系总共意味著误差大于0.5的行> b = a[apply(a,1,mad)>0.5,]> b [,1] [,2] [,3] [,4] [,5][1,] -0.4125356 0.1219293 -0.4763589 -0.9717165 1.09162243[2,] 1.8778966 -0.1171794 2.9295352 1.3383662 -0.03269026[3,] 1.0000000 2.0000000 3.0000000 4.0000000 5.00000000[4,] 2.0000000 10.0000000 11.0000000 13.0000000 4.00000000[5,] -1.2649111 -0.6324555 0.0000000 0.6324555 1.26491106# 乘积按照mad的个总共上标排罗列> c = b[order(apply(b,1,mad), decreasing=T),]> c [,1] [,2] [,3] [,4] [,5][1,] 2.0000000 10.0000000 11.0000000 13.0000000 4.00000000[2,] 1.8778966 -0.1171794 2.9295352 1.3383662 -0.03269026[3,] 1.0000000 2.0000000 3.0000000 4.0000000 5.00000000[4,] -1.2649111 -0.6324555 0.0000000 0.6324555 1.26491106[5,] -0.4125356 0.1219293 -0.4763589 -0.9717165 1.09162243> rownames(c) <- paste('Gene', letters[1:5], sep="_")> colnames(c) <- toupper(letters[1:5])> c A B C D EGene_a 2.0000000 10.0000000 11.0000000 13.0000000 4.00000000Gene_b 1.8778966 -0.1171794 2.9295352 1.3383662 -0.03269026Gene_c 1.0000000 2.0000000 3.0000000 4.0000000 5.00000000Gene_d -1.2649111 -0.6324555 0.0000000 0.6324555 1.26491106Gene_e -0.4125356 0.1219293 -0.4763589 -0.9717165 1.09162243# 乘积转置> expr = t(c)> expr Gene_a Gene_b Gene_c Gene_d Gene_eA 2 1.87789657 1 -1.2649111 -0.4125356B 10 -0.11717937 2 -0.6324555 0.1219293C 11 2.92953522 3 0.0000000 -0.4763589D 13 1.33836620 4 0.6324555 -0.9717165E 4 -0.03269026 5 1.2649111 1.0916224# 乘积系总共的替换> expr2 = expr> expr2[expr2<0] = 0> expr2 Gene_a Gene_b Gene_c Gene_d Gene_eA 2 1.877897 1 0.0000000 0.0000000B 10 0.000000 2 0.0000000 0.1219293C 11 2.929535 3 0.0000000 0.0000000D 13 1.338366 4 0.6324555 0.0000000E 4 0.000000 5 1.2649111 1.0916224# 乘积中只针对某一罗列替换# expr2是个乘积不是统计总共据支架,不能使用罗列拼法索引> expr2[expr2$Gene_b<1, "Gene_b"] <- 1Error in expr2$Gene_b : $ operator is invalid for atomic vectors# str是一个最为常用、好用的核对参数讯息的工具,尤其是对特别十分复杂的参数,# 可以看清其层级结构,便于抽取统计总共据> str(expr2) num [1:5, 1:5] 2 10 11 13 4 ... - attr(*, "dimnames")=List of 2 ..$ : chr [1:5] "A" "B" "C" "D" ... ..$ : chr [1:5] "Gene_a" "Gene_b" "Gene_c" "Gene_d" ...# 转换成为统计目录,再进行除此以外的操作方法> expr2 <- as.data.frame(expr2)> str(expr2)'data.frame': 5 obs. of 5 variables: $ Gene_a: num 2 10 11 13 4 $ Gene_b: num 1.88 1 2.93 1.34 1 $ Gene_c: num 1 2 3 4 5 $ Gene_d: num 0 0 0 0.632 1.265 $ Gene_e: num 0 0.122 0 0 1.092> expr2[expr2$Gene_b<1, "Gene_b"] <- 1> expr2> expr2 Gene_a Gene_b Gene_c Gene_d Gene_eA 2 1.877897 1 0.0000000 0.0000000B 10 1.000000 2 0.0000000 0.1219293C 11 2.929535 3 0.0000000 0.0000000D 13 1.338366 4 0.6324555 0.0000000E 4 1.000000 5 1.2649111 1.0916224R中乘积审核并入# 读入容器讯息> sampleInfo = "Samp;Group;Genotype+ A;Control;WT+ B;Control;WT+ D;Treatment;Mutant+ C;Treatment;Mutant+ E;Treatment;WT+ F;Treatment;WT"> phenoData = read.table(text=sampleInfo,sep=";", header=T, row.names=1, quote="")> phenoData Group GenotypeA Control WTB Control WTD Treatment MutantC Treatment MutantE Treatment WTF Treatment WT# 把容器讯息按照表型乘积中的容器讯息排序,并只留存有表型讯息的容器# match() returns a vector of the positions of (first) matches of its first argument in its second.> phenoData[match(rownames(expr), rownames(phenoData)),] Group GenotypeA Control WTB Control WTC Treatment MutantD Treatment MutantE Treatment WT# ‘%in%’ is a more intuitive interface as a binary operator, which returns a logical vector indicating if there is a match or not for its left operand.# 提醒顺序,%in%比match能够理解一些> phenoData = phenoData[rownames(phenoData) %in% rownames(expr),]> phenoData Group GenotypeA Control WTB Control WTC Treatment MutantD Treatment MutantE Treatment WT# 并入乘积# by=0 声称按照行的拼法排序# by=columnname 声称按照共有的某一罗列排序# 并入后多出了新的一罗列Row.names> merge_data = merge(expr, phenoData, by=0, all.x=T)> merge_data Row.names Gene_a Gene_b Gene_c Gene_d Gene_e Group Genotype1 A 2 1.87789657 1 -1.2649111 -0.4125356 Control WT2 B 10 -0.11717937 2 -0.6324555 0.1219293 Control WT3 C 11 2.92953522 3 0.0000000 -0.4763589 Treatment Mutant4 D 13 1.33836620 4 0.6324555 -0.9717165 Treatment Mutant5 E 4 -0.03269026 5 1.2649111 1.0916224 Treatment WT> rownames(merge_data) <- merge_data$Row.names> merge_data Row.names Gene_a Gene_b Gene_c Gene_d Gene_e Group GenotypeA A 2 1.87789657 1 -1.2649111 -0.4125356 Control WTB B 10 -0.11717937 2 -0.6324555 0.1219293 Control WTC C 11 2.92953522 3 0.0000000 -0.4763589 Treatment MutantD D 13 1.33836620 4 0.6324555 -0.9717165 Treatment MutantE E 4 -0.03269026 5 1.2649111 1.0916224 Treatment WT# 消除一罗列;-1声称消除第一罗列> merge_data = merge_data[,-1]> merge_data Gene_a Gene_b Gene_c Gene_d Gene_e Group GenotypeA 2 1.87789657 1 -1.2649111 -0.4125356 Control WTB 10 -0.11717937 2 -0.6324555 0.1219293 Control WTC 11 2.92953522 3 0.0000000 -0.4763589 Treatment MutantD 13 1.33836620 4 0.6324555 -0.9717165 Treatment MutantE 4 -0.03269026 5 1.2649111 1.0916224 Treatment WT# 抽取出所有的总共系总共罗列> merge_data[sapply(merge_data, is.numeric)] Gene_a Gene_b Gene_c Gene_d Gene_eA 2 1.87789657 1 -1.2649111 -0.4125356B 10 -0.11717937 2 -0.6324555 0.1219293C 11 2.92953522 3 0.0000000 -0.4763589D 13 1.33836620 4 0.6324555 -0.9717165E 4 -0.03269026 5 1.2649111 1.0916224str的应用

str: Compactly display the internal structure of an R object, a diagnostic function and an alternative to ‘summary (and to some extent, ‘dput’). Ideally, only one line for each ‘basic’ structure is displayed. It is especially well suited to compactly display the (abbreviated) contents of (possibly nested) lists. The idea is to give reasonable output for any R object. It calls ‘args’ for (non-primitive) function objects.

str用来告诉结果的看成方式,对于不少Bioconductor的纸制,或者十分复杂的R函总共的驱动,都是一堆表罗列出的嵌套,str(complex_result)会驱动每个表罗列出的拼法,方便使用抽取相异的讯息。

# str的一个应用举例来说> str(list(a = "A", L = as.list(1:100)), list.len = 9)List of 2 $ a: chr "A" $ L:List of 100 ..$ : int 1 ..$ : int 2 ..$ : int 3 ..$ : int 4 ..$ : int 5 ..$ : int 6 ..$ : int 7 ..$ : int 8 ..$ : int 9 .. [list output truncated]# 利用str核对pca的结果,具体的PCA应用核对> pca_result <- prcomp(expr)> pca_resultStandard deviations:[1] 4.769900e+00 1.790861e+00 1.072560e+00 1.578391e-01 2.752128e-16Rotation: PC1 PC2 PC3 PC4 PC5Gene_a 0.99422750 -0.02965529 0.078809521 0.01444655 0.06490461Gene_b 0.04824368 -0.44384942 -0.885305329 0.03127940 0.12619948Gene_c 0.08258192 0.81118590 -0.451360828 0.05440417 -0.35842886Gene_d -0.01936958 0.30237826 -0.079325524 -0.66399283 0.67897952Gene_e -0.04460135 0.22948437 -0.002097256 0.74496081 0.62480128> str(pca_result)List of 5 $ sdev : num [1:5] 4.77 1.79 1.07 1.58e-01 2.75e-16 $ rotation: num [1:5, 1:5] 0.9942 0.0482 0.0826 -0.0194 -0.0446 ... ..- attr(*, "dimnames")=List of 2 .. ..$ : chr [1:5] "Gene_a" "Gene_b" "Gene_c" "Gene_d" ... .. ..$ : chr [1:5] "PC1" "PC2" "PC3" "PC4" ... $ center : Named num [1:5] 8 1.229 3 0.379 0.243 ..- attr(*, "names")= chr [1:5] "Gene_a" "Gene_b" "Gene_c" "Gene_d" ... $ scale : logi FALSE $ x : num [1:5, 1:5] -6.08 1.86 3.08 5.06 -3.93 ... ..- attr(*, "dimnames")=List of 2 .. ..$ : chr [1:5] "A" "B" "C" "D" ... .. ..$ : chr [1:5] "PC1" "PC2" "PC3" "PC4" ... - attr(*, "class")= chr "prcomp"# 取出每个主化学物质解释的差异> pca_result$sdev[1] 4.769900e+00 1.790861e+00 1.072560e+00 1.578391e-01 2.752128e-16R的纸制管理# 什么时候需要装配纸制> library('unExistedPackage')Error in library("unExistedPackage") : 不存在叫‘unExistedPackage’这个拼法的程辑纸制# 装配纸制> install.packages("package_name")# 指明装配来源> install.packages("package_name", repo="")# 装配Bioconductor的纸制> source('')> biocLite('BiocInstaller')> biocLite(c("RUVSeq","pcaMethods"))# 装配Github的R纸制> install.packages("devtools")> devtools::install_github("JustinaZ/pcaReduce")# 手动装配, 首先下载纸制的配置文件(压缩版就可),然后在终端设备试运行下面的命令。ct@ehbio:~$ R CMD INSTALL package.tar.gz# 移除纸制>remove.packages("package_name")# 核对所有装配的纸制>library()# 核对特定装配纸制的新版本> installed.packages()[c("DESeq2"), c("Package", "Version")] Package Version "DESeq2" "1.14.1" > # 核对默认装配纸制的位置>.libPaths()# 调用装配的纸制>library(package_name)#devtools::install_github("hms-dbmi/scde", build_vignettes = FALSE)#install.packages(c("mvoutlier","ROCR"))#biocLite(c("RUVSeq","pcaMethods","SC3","TSCAN","monocle","MultiAssayExperiment","SummarizedExperiment"))#devtools::install_github("satijalab/seurat")。

杭州看白癜风去哪个医院
岳阳看男科哪个医院比较好
新乡看妇科的医院哪家好
南京男科专科医院
南京看妇科去哪家医院
太极急支糖浆治咳嗽效果怎么样
干扰素
急支糖浆有什么作用
女性月经期饮食
甲流引起的咳嗽吃什么药

上一篇: 后端真的比外侧香吗?真实差距大吗?

下一篇: “流沙”专业,好就业!2022年电子信息类17个专业最好的大学推荐

相关阅读
库里17中3普尔32分勇士胜马刺,巴特勒伤退

战中都主场115-108击败小牛队。德雷蒙德-贝克复出,苏尔先是吃到两次触球,但维金斯连中都三分值得一提的是球队前所8分,特克也炮弹2记远投,尼尔远投,战中都21-10开局,维金斯

2025-10-23 00:16:25
封面专访|蓝城蓝越总裁杨岳:落子成渝双圈的四川南充,千年绸都吹出来“桃李春风”

内江桃李虹小城外镇多元文化以前等教育示范这两项,总投资50亿元,预估下同约3600亩,其中建设项目用地约600亩、农业用地约3000亩,承接集多样性颐养、多元文化旅游区、精品酒店、风情民宿、风情商业化

2025-10-23 00:16:25
王本中:普及教育既要有教无类又要因材施教 还要人尽其才

各种生命力,它们协同为校内透过服务项目的一个好的机会。我就说什么到说什么这几点,不当之处,望大家交流,昨天!。a href="http:lzbdf.999ask.com" ta

2025-10-23 00:16:25
河池金城江区:行业送温暖慰问暖警心

今年1月底10日是第二个“中国人民警察节”,当天,恒生银行河池分行到毕节市公安厅东阳江上区公所着手慰问社会活动,向全体公安民辅警表示感谢节日的祝福,并还给上慰问品。

2025-10-23 00:16:25
快手发力泛生活服务背后:私域流速正构建一种新型信任经济

原标题:慢一手发力泛家庭增值其实:私域流速正实例一种新型信赖政治经济 4月22日,在慢一手2021硅藻创作音乐筹备会议上,慢一手高级公司总裁、主旨运营负责人严强暗示,今年慢一手的主要能够将主

2025-10-23 00:16:25