赞
踩
要经常检查代码生成的结果,警惕不报错的错误
向量:一维
表格:二维
列表:三维
判断数据结构:class()或is族函数判断,或根据其生成的函数来判断
二维,每列只允许一种数据类型
data.frame()
变量名称只起到提示作用,不起决定作用
df1 <- data.frame(gene = paste0("gene",1:4),
change = rep(c("up","down"),each = 2),
score = c(5,3,-2,-4))
df1
## gene change score
## 1 gene1 up 5
## 2 gene2 up 3
## 3 gene3 down -2
## 4 gene4 down -4
df2 <- read.csv("gene.csv") #读取文件
dim()
维度(dimension)
nrow()
行;rownames()
行名
ncol()
列;colnames()
列名
取某一列:$
取数据框中的一列,按Tab
键自动补齐
df1$score
## [1] 5 3 -2 -4
按坐标:[行号,列号]
df1[2,2]
## [1] "up"
df1[2,]
## gene change score
## 2 gene2 up 3
df1[,2]
## [1] "up" "up" "down" "down"
df1[c(1,3),1:2]
## gene change
## 1 gene1 up
## 3 gene3 down
按名字:[行名,列名]
## 按名字
df1[,"gene"]
## [1] "gene1" "gene2" "gene3" "gene4"
df1[,c('gene','change')] ##可以按名字同时取多列,使代码变得易读
## gene change
## 1 gene1 up
## 2 gene2 up
## 3 gene3 down
## 4 gene4 down
修改:将需要修改的内容提取后进行重新赋值
df1[3,3] <- 5 df1 ## gene change score ## 1 gene1 up 5 ## 2 gene2 up 3 ## 3 gene3 down 5 ## 4 gene4 down -4 df1$score <- c(12,23,50,2) df1 ## gene change score ## 1 gene1 up 12 ## 2 gene2 up 23 ## 3 gene3 down 50 ## 4 gene4 down 2
新建:$
后接尚未存在的列名,即是新建列
df1$p.value <- c(0.01,0.02,0.07,0.05)
df1
## gene change score p.value
## 1 gene1 up 12 0.01
## 2 gene2 up 23 0.02
## 3 gene3 down 50 0.07
## 4 gene4 down 2 0.05
修改列名或行名
将行名和列名视为向量去做修改
#改行名和列名
rownames(df1) <- c("r1","r2","r3","r4")
#只修改某一行/列的名
colnames(df1)[2] <- "CHANGE"
两个数据框根据有交集的一列合并(取交集)
test1 <- data.frame(name = c('jimmy','nicker','Damon','Sophie'), blood_type = c("A","B","O","AB")) test1 ## name blood_type ## 1 jimmy A ## 2 nicker B ## 3 Damon O ## 4 Sophie AB test2 <- data.frame(name = c('Damon','jimmy','nicker','tony'), group = c("group1","group1","group2","group2"), vision = c(4.2,4.3,4.9,4.5)) test2 ## name group vision ## 1 Damon group1 4.2 ## 2 jimmy group1 4.3 ## 3 nicker group2 4.9 ## 4 tony group2 4.5 merge(test1,test2,by="name") ## name blood_type group vision ## 1 Damon O group1 4.2 ## 2 jimmy A group1 4.3 ## 3 nicker B group2 4.9 test3 <- data.frame(NAME = c('Damon','jimmy','nicker','tony'), weight = c(140,145,110,138)) test3 ## NAME weight ## 1 Damon 140 ## 2 jimmy 145 ## 3 nicker 110 ## 4 tony 138 merge(x=test1,x=test3,by.x = "name",by.y = "NAME") ## name blood_type weight ## 1 Damon O 140 ## 2 jimmy A 145 ## 3 nicker B 110
取合集:查看帮助文档
df1 <- data.frame(gene = paste0("gene",1:4),
change = rep(c("up","down"),each = 2),
score = c(5,3,-2,-4))
k = df1$score>0;k
## [1] TRUE TRUE FALSE FALSE
df1[k,]
## gene change score
## 1 gene1 up 5
## 2 gene2 up 3
df1[k,1]
## [1] "gene1" "gene2"
df1$gene[k]
## [1] "gene1" "gene2"
df1$gene[df1$score>0]
## [1] "gene1" "gene2"
二维,只允许一种数据类型
新建:matrix(), nrow/ncol设置行数或列数
m <- matrix(1:9, nrow = 3)
colnames(m) <- c("a","b","c") #加列名
m
## a b c
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
取子集与数据框相同
m[2,]
## a b c
## 2 5 8
m[,1]
## [1] 1 2 3
m[2,3]
## c
## 8
m[2:3,1:2]
## a b
## [1,] 2 5
## [2,] 3 6
转置:t()
以1 5 9为中轴转置
m
## a b c
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
t(m)
## [,1] [,2] [,3]
## a 1 2 3
## b 4 5 6
## c 7 8 9
转换:所有矩阵可以转换成数据框as.data.frame()
as.data.frame(m)
## a b c
## 1 1 4 7
## 2 2 5 8
## 3 3 6 9
as.data.frame(m)
后m还是矩阵热图函数heatmap
需求矩阵数据类型
pheatmap::pheatmap(m)
pheatmap::pheatmap(m,cluster_cols = F,cluster_rows = F)
新建:list()
,列表的每个元素可以是任何数据结构和类型
x <- list(m1 = matrix(1:9, nrow = 3),
m2 = matrix(2:9, nrow = 2))
x
## $m1
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
##
## $m2
## [,1] [,2] [,3] [,4]
## [1,] 2 4 6 8
## [2,] 3 5 7 9
取子集:[[]]
和$
[[]]
中接元素的序号或名字
x[[1]]
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
$
后接元素名字,可以tab补齐
x$m1
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
names()
列表的元素有名字,向量的元素也可以有名字
scores = c(100,59,73,95,45)
names(scores) = c("jimmy","nicker","Damon","Sophie","tony")
scores
## jimmy nicker Damon Sophie tony
## 100 59 73 95 45
#应用!
scores["jimmy"]
## jimmy
## 100
scores[c("jimmy","nicker")]
## jimmy nicker
## 100 59
names(scores)[scores>60]
## [1] "jimmy" "Damon" "Sophie"
rm(x)
rm(df1,df2)
rm(list = ls())
最常用Ctrl+L
tibble::column_to_rownames()
可以将第一列变成行名,然后删除第一列[[]]
和$
取列和取列表元素相同以上来自生信技能树课程!给小洁老师比心~
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。