factor(x = character(), levels, labels = levels,
exclude = NA, ordered = is.ordered(x), nmax = NA)
-
x
a vector of data, usually taking a small number of distinct values. -
levels
an optional vector of the unique values (as character strings) that x might have taken. The default is the unique set of values taken by as.character(x), sorted into increasing order of x. Note that this set can be specified as smaller than sort(unique(x)). -
labels
either an optional character vector of labels for the levels (in the same order as levels after removing those in exclude), or a character string of length 1. Duplicated values in labels can be used to map different values of x to the same factor level. -
exclude
a vector of values to be excluded when forming the set of levels. This may be factor with the same level set as x or should be a character.
rt3 <- readRDS("/mnt/d/TCGA/TCGA_clinical_ALL.rds")
print(rt3[1:4,1:3])
sample _PATIENT cancer type abbreviation
1 TCGA-OR-A5J1-01 TCGA-OR-A5J1 ACC
2 TCGA-OR-A5J2-01 TCGA-OR-A5J2 ACC
3 TCGA-OR-A5J3-01 TCGA-OR-A5J3 ACC
4 TCGA-OR-A5J5-01 TCGA-OR-A5J5 ACC
unique(rt3$'cancer type abbreviation')
- 'ACC'
- 'BLCA'
- 'BRCA'
- 'CESC'
- 'CHOL'
- 'COAD'
- 'DLBC'
- 'ESCA'
- 'GBM'
- 'HNSC'
- 'KICH'
- 'KIRC'
- 'KIRP'
- 'LAML'
- 'LGG'
- 'LIHC'
- 'LUAD'
- 'LUSC'
- 'MESO'
- 'OV'
- 'PAAD'
- 'PCPG'
- 'PRAD'
- 'READ'
- 'SARC'
- 'SKCM'
- 'STAD'
- 'TGCT'
- 'THCA'
- 'THYM'
- 'UCEC'
- 'UCS'
- 'UVM'
本来是字符型变量的
typeof(rt3$'cancer type abbreviation')
‘character’
作为因子后成为数值变量了(每个数值代表一个等级)
rt3$'cancer type abbreviation' <- factor(rt3$'cancer type abbreviation')
typeof(rt3$'cancer type abbreviation')
‘integer’
unique(rt3$'cancer type abbreviation')
- ACC
- BLCA
- BRCA
- CESC
- CHOL
- COAD
- DLBC
- ESCA
- GBM
- HNSC
- KICH
- KIRC
- KIRP
- LAML
- LGG
- LIHC
- LUAD
- LUSC
- MESO
- OV
- PAAD
- PCPG
- PRAD
- READ
- SARC
- SKCM
- STAD
- TGCT
- THCA
- THYM
- UCEC
- UCS
- UVM
- 'ACC'
- 'BLCA'
- 'BRCA'
- 'CESC'
- 'CHOL'
- 'COAD'
- 'DLBC'
- 'ESCA'
- 'GBM'
- 'HNSC'
- 'KICH'
- 'KIRC'
- 'KIRP'
- 'LAML'
- 'LGG'
- 'LIHC'
- 'LUAD'
- 'LUSC'
- 'MESO'
- 'OV'
- 'PAAD'
- 'PCPG'
- 'PRAD'
- 'READ'
- 'SARC'
- 'SKCM'
- 'STAD'
- 'TGCT'
- 'THCA'
- 'THYM'
- 'UCEC'
- 'UCS'
- 'UVM'
因子将其分为了很多组,现在通过levels和labels修改分组
rt3$'cancer type abbreviation' <- factor(rt3$'cancer type abbreviation',
levels=c('ACC','BLCA','BRCA','CESC','CHOL','COAD','DLBC','ESCA','GBM','HNSC','KICH','KIRC','KIRP','LAML','LGG','LIHC','LUAD','LUSC','MESO','OV','PAAD','PCPG','PRAD','READ','SARC','SKCM','STAD','TGCT','THCA','THYM','UCEC','UCS','UVM'),
labels=c('ACC','BLCA','BRCA','BRCA','BRCA','BRCA','BRCA','BRCA','BRCA','BRCA','BRCA','BRCA','BRCA','BRCA','BRCA','BRCA','BRCA','BRCA','BRCA','BRCA','BRCA','BRCA','BRCA','BRCA','BRCA','BRCA','BRCA','BRCA','BRCA','BRCA','BRCA','BRCA','BRCA'))
typeof(rt3$'cancer type abbreviation')
‘integer’
unique(rt3$'cancer type abbreviation')
- ACC
- BLCA
- BRCA
- 'ACC'
- 'BLCA'
- 'BRCA'
factor 转化为as.numeric存在的陷阱
a <- as.numeric(rt3$'cancer type abbreviation')
unique(a)
- 1
- 2
- 3
b <- factor(c(2:8),levels=c("2","3","4","5","6","7","8"),labels=c("1","2","3","4","5","6","7"))
b
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- '1'
- '2'
- '3'
- '4'
- '5'
- '6'
- '7'
as.numeric(b)
- 1
- 2
- 3
- 4
- 5
- 6
- 7