You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于subtype行中以‘1’开头的值检索数据列(data.table实现)

筛选数据框中subtype行以“1”开头的列(data.table实现)

需求

提取数据框中subtype行的值以“1”开头的所有列。

实现代码

使用data.table的%like%运算符匹配以“1”开头的字符串,完成列筛选:

library(data.table)

# 若原始数据是data.frame,先转为data.table(可选,提升性能)
setDT(df)

# 筛选目标列
df.1 <- df[, .SD[df["subtype", ] %like% "^1"], with = FALSE]

# 如需转回data.frame格式
# setDF(df.1)

测试数据

以下是可用于验证的测试数据集:

df <- structure(list(TCGA.2Z.A9J1.01A = c("1a", "5.13553076559331", 
"5.25437106109279", "0", "6.94770472476424", "0", "12.413916703553", 
"11.192440344074", "3.1585955515009", "0"), TCGA.2Z.A9J3.01A = c("2a", 
"4.75608390849743", "3.47688712558277", "3.69415587836022", "7.55464180068354", 
"0", "10.1701864662239", "9.9698319970815", "5.3250949389428", 
"0"), TCGA.2Z.A9J6.01A = c("2b", "5.17761487414502", "6.65033806253549", 
"0.584770128557275", "6.94039112615504", "0", "12.0370627256659", 
"10.8210532515207", "5.53845435576078", "0"), TCGA.2Z.A9J8.01A = c("1b", 
"3.73015082384196", "5.42861476811507", "0", "7.92705080275384", 
"0.41565051441473", "11.9979504885468", "9.91529337032864", "5.14529839229733", 
"0"), TCGA.2Z.A9JD.01A = c("2a", "3.30975827142464", "4.33563331445214", 
"0", "8.67290934153811", "0", "10.9513709097776", "10.8023152138843", 
"1.84534953815009", "0"), TCGA.2Z.A9JG.01A = c("2c", "7.28850529763777", 
"3.84460660023553", "3.00626211318484", "5.81778440061722", "0", 
"12.0868373610296", "10.3281796591297", "3.6562103201826", "0"
), TCGA.2Z.A9JJ.01A = c("1c", "6.28339584818368", "0.807272479985472", 
"4.53120676686483", "6.40832124805635", "0", "12.8200019062179", 
"11.792050825269", "2.35741124701605", "0"), TCGA.2Z.A9JL.01A = c("1c", 
"7.03644948442735", "3.73611776175452", "1.34522603534256", "6.5525507570889", 
"0.597983993789946", "12.9645888051835", "11.0094140803487", 
"7.40469964993127", "0"), TCGA.2Z.A9JN.01A = c("1b", "4.17567628766862", 
"0", "0", "7.2921559549592", "0", "12.2914311784018", "10.8173916627083", 
"0.514097004894233", "0"), TCGA.2Z.A9JP.01A = c("1d", "6.3033314339384", 
"1.11789447981141", "1.74010724619747", "7.46591652705797", "0", 
"13.3435017632051", "11.2086677724798", "1.74010724619747", "0"
)), row.names = c("subtype", "A1BG", "A1CF", "A2BP1", "A2LD1", 
"A2ML1", "A2M", "A4GALT", "A4GNT", "AAA1"), class = "data.frame")

运行代码后,df.1会保留所有subtype行值以“1”开头的列,即:TCGA.2Z.A9J1.01A、TCGA.2Z.A9J8.01A、TCGA.2Z.A9JJ.01A、TCGA.2Z.A9JL.01A、TCGA.2Z.A9JN.01A、TCGA.2Z.A9JP.01A。

内容的提问来源于stack exchange,提问作者Anon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 15:06:27