4 split函数将data.frame转换成list的方法
2019-04-14 本文已影响0人
陈宇乔
将data.frame转换成list的方法
d <- data.frame(gender=c("M","M","F","M","F","F"),age=c(47,59,21,32,33,24),income=c(55000,88000,32450,76500,123000,45650), over25=rep(c(1,1,0), times=2))
d
# gender age income over25
# 1 M 47 55000 1
# 2 M 59 88000 1
# 3 F 21 32450 0
# 4 M 32 76500 1
# 5 F 33 123000 1
# 6 F 24 45650 0
a<- split(d$income, list(d$gender,d$over25)) #将income按照gender、over25分组
class(a)
## 含有两种类标准,变成了list of 4
a[1]
a[2]
a[3]
a[4]
a$F.0
a$M.0
a$F.1
a$M.1
##############
b<- split(d$income, list(d$gender))
# 变成了list of 2
实战:GSVA分析的时候需要一个matrix 和一个gene_sets list格式:我们可以将dataframe变成list
rm(list = ls())
library(readxl)
a <- read_excel("./IO panel 395 gene & pancancer immune gene overlap.xlsx")
View(a)
b<- split(a$Gene,list(a$`Cell type`,a$Immunity))
结果