如何使用R创建因子

ffx8fchx  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(138)

bulk.all Dataframe 是两个 Dataframe 的组合:bulk.normalbulk.kirp。在group.factor中,我想创建一个因子,如果数据来自bulk.normal,则标签为normal,而如果数据来自bulk.kirp Dataframe ,则标签为KIRP

bulk.normal <- bulk.normal[rownames(bulk.normal) %in% rownames(bulk.kirp),]
bulk.kirp <- bulk.kirp[rownames(bulk.kirp) %in% rownames(bulk.normal),]

bulk.all <- cbind(bulk.kirp, bulk.normal)

bulk.kirp$group <- "KIRP"
bulk.normal$group <- "normal"
group <- cbind(bulk.kirp$group, bulk.normal$group)
group.factor <- as.factor(x=character(), levels=c("KIRP", "normal"), labels=levels, exclude=NA, ordered=is.ordered(bulk.all), nmax=NA)

回溯:

Error in as.factor(x = character(), levels = c("KIRP", "normal"), labels = levels,  : 
  unused arguments (levels = c("KIRP", "normal"), labels = levels, exclude = NA, ordered = is.ordered(bulk.all), nmax = NA)

数据:

> dput(bulk.kirp[1:5,1:5])
structure(list(TCGA.2K.A9WE.01A = c(7.65342121905285, 6.35598354101006, 
14.3511850042327, 10.3737643425674, 10.0819596419255), TCGA.2Z.A9J1.01A = c(5.09389393824392, 
6.93597002271109, 12.4136523086721, 11.1918237390263, 10.1912122382252
), TCGA.2Z.A9J3.01A = c(4.70168212029528, 7.54694769203808, 10.1689338100564, 
9.96839262629172, 9.87305770150294), TCGA.2Z.A9J5.01A = c(7.99645936536463, 
6.89258167250936, 13.6832285748428, 10.3714563849361, 10.4176870383992
), TCGA.2Z.A9J6.01A = c(5.13719199914349, 6.92859654071157, 12.0367193976262, 
10.8202555636581, 10.3262700402849)), row.names = c("A1BG", "A2LD1", 
"A2M", "A4GALT", "AAAS"), class = "data.frame")

> dput(bulk.normal[1:5,1:5])
structure(list(TCGA.BQ.7051.11A = c(57.4382, 1928.9092, 16482.7822, 
1150.8932, 619.0802), TCGA.DZ.6132.11A = c(70.6982, 173.5022, 
36833.3219, 1489.626, 712.553), TCGA.CZ.4864.11A = c(12.9899, 
2416.7342, 31651.9804, 837.9322, 575.9693), TCGA.KN.8426.11A = c(47.5866, 
279.0018, 35180.2554, 644.7446, 684.0869), TCGA.CZ.5982.11A = c(11.257, 
3387.5714, 19067.2504, 720.4503, 513.6544)), row.names = c("A1BG", 
"A2LD1", "A2M", "A4GALT", "AAAS"), class = "data.frame")

预期输出:
2个水平的因子
| TCGA.2K.A9WE.01A | TCGA.2Z.A9J1.01A | TCGA.2Z.A9J3.01A | TCGA.2Z.A9J5.01A | TCGA.2Z.A9J6.01A | TCGA.BQ.7051.11A | TCGA.DZ.6132.11A | TCGA.CZ.4864.11A | TCGA.KN.8426.11A | TCGA.CZ.5982.11A |
| --------------|--------------|--------------|--------------|--------------|--------------|--------------|--------------|--------------|--------------|
| KIRP|KIRP|KIRP|KIRP|KIRP|正常|正常|正常|正常|正常|

uurv41yg

uurv41yg1#

您的问题和期望结果似乎不匹配。不清楚您如何想要因子向量,但希望它们跨列。
它认为这是你正在寻找的。

#create the labels
labels <- rep(c("KIRP", "normal"), c(ncol(bulk.kirp), ncol(bulk.normal)))
answer <- data.frame(t(labels))
   
#create the title
titles <- c(names(bulk.kirp), names(bulk.normal))
names(answer) <- titles

answer

  TCGA.2K.A9WE.01A TCGA.2Z.A9J1.01A TCGA.2Z.A9J3.01A TCGA.2Z.A9J5.01A TCGA.2Z.A9J6.01A TCGA.BQ.7051.11A TCGA.DZ.6132.11A TCGA.CZ.4864.11A TCGA.KN.8426.11A TCGA.CZ.5982.11A
1             KIRP             KIRP             KIRP             KIRP             KIRP           normal           normal           normal           normal           normal

相关问题