我有一个非常大的数据框(nrow = ~273,000)我将其作为一个例子如下:每一行都是一个蛋白质名称,并且有不同数量的列,其中列出了亚细胞结构,其中它们可以在人体细胞中找到。 1)我想删除每一行的重复条目,我正在努力解决这个问题(下面的代码)。 2)然后,我希望能够计算每个基因可以找到多少列(亚细胞结构)。
背景:我从Uniprot得到这些数据并尽可能地使用正则表达式进行清理,但仍有一些情况下存在重复条目的行(例如FMR1列出染色体2x,细胞质3x和质膜2x - 此外它们之间有一些空白列)
dput(df1)
structure(list(FMR1 = structure(c(41L, 3L, 17L, 63L, 16L, 24L,
35L, 33L, 52L, 6L, 49L, 5L, 71L, 72L, 42L, 58L, 22L, 20L, 19L,
80L, 9L, 51L, 66L, 64L, 23L, 14L, 60L, 45L, 28L, 54L, 7L, 30L,
29L, 44L, 53L, 8L, 69L, 79L, 10L, 11L, 26L, 37L, 39L, 40L, 82L,
73L, 18L, 21L, 27L, 47L, 4L, 46L, 1L, 13L, 36L, 70L, 74L, 67L,
78L, 77L, 61L, 62L, 31L, 56L, 34L, 57L, 25L, 81L, 75L, 59L, 2L,
65L, 55L, 38L, 50L, 68L, 32L, 12L, 43L, 15L, 48L, 76L), .Label = c("AAMP",
"ADCY10 SAC", "AIMP1 EMAP2 SCYE1", "ANTXR2 CMG2", "APBB1 FE65 RIR",
"APC DP2", "APLP1", "ARHGAP26 GRAF KIAA0621 OPHN1L", "ARL4A ARL4",
"ATP6V0D1 ATP6D VPATPD", "ATP6V1D ATP6M VATD", "AZIN2 ADC KIAA1945 ODCP",
"CACNB2 CACNLB2 MYSB", "CAMK2D CAMKD", "CDCA8 PESCRG3", "CDK1 CDC2 CDC28A CDKN1 P34CDC2",
"CEMIP KIAA1199", "CIB1 CIB KIP PRKDCIP", "CLTA", "CLTB", "CMTM8 CKLFSF8",
"DMD", "DSP", "ECT2", "EHD2 PAST2", "ENTPD2 CD39L1", "ERBB2 HER2 MLN19 NEU NGL",
"EVPL", "FCHO1 KIAA0290", "FCHO2", "FGR SRC2", "GPER1 CEPR CMKRL2 DRY12 GPER GPR30",
"HDAC6 KIAA0901 JM21", "ITCH", "ITGB1BP1 ICAP1", "KCTD7", "KIFC3",
"MFN1", "MISP C19orf21", "MYOT TTID", "NGDN C14orf120", "NISCH IRAS KIAA0975",
"NR1D1 EAR1 HREV THRAL", "PGM5 PGMRP", "PKP4", "PLA2G6 PLPLA9",
"PNKD KIAA1184 MR1 TAHCCP2 FKSG19 UNQ2491/PRO5778", "POP7 RPP20",
"PPL KIAA0568", "PRDX3 AOP1", "PTOV1 ACID2 PP642 UNQ6127/PRO20092",
"PTPN23 KIAA1471", "PTPRE", "PTPRR ECPTP PTPRQ", "RAB13 GIG4",
"RAB23 HSPC137", "RAB29 RAB7L1", "RAB30", "RAB38", "RAB40AL RLGP",
"RAB8A MEL RAB8", "RAB9A RAB9", "RACGAP1 KIAA1478 MGCRACGAP",
"RAP1B OK/SW-cl", "RGS8", "RPSA LAMBR LAMR1", "SGIP1", "SHMT2",
"SHROOM3 KIAA1481 SHRML MSTP013", "SLC28A3 CNT3", "SNTA1 SNT1",
"SNTB1 SNT2B1", "SNX11", "SNX12", "STOM BND7 EPB72", "TEX10 L18 Nbla10363",
"TNFRSF8 CD30 D1S166E", "TNS4 CTEN PP14434", "TRIM72 MG53", "USP6 HRP1 TRE2",
"VCL", "YES1 YES"), class = "factor"), Nucleus = structure(c(3L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
2L), .Label = c("Mitochondrion ", "Nucleus", "Nucleus ", "Plasma membrane",
"Plasma membrane "), class = "factor"), Chromosome = structure(c(1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L), .Label = c("Chromosome", "Cytoplasm", "Cytoplasm "), class = "factor"),
Chromosome.1 = structure(c(4L, 5L, 7L, 5L, 14L, 12L, 20L,
18L, 5L, 20L, 20L, 2L, 1L, 1L, 8L, 10L, 19L, 1L, 1L, 8L,
16L, 16L, 17L, 19L, 20L, 21L, 15L, 13L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 9L, 10L,
16L, 16L, 16L, 22L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 11L,
7L, 14L, 9L, 17L, 11L, 9L, 2L, 6L, 6L, 17L, 18L, 10L, 1L,
1L, 17L, 19L, 19L, 1L, 3L, 5L, 1L), .Label = c("", " ", "Chromosome",
"Cytoplasm ", "Cytoplasmic vesicle", "Cytoplasmic vesicle ",
"Endoplasmic reticulum", "Endosome", "Endosome ", "Golgi apparatus",
"Golgi apparatus ", "Midbody", "Midbody ", "Mitochondrion",
"Mitochondrion ", "Nucleus", "Nucleus ", "Perikaryon ",
"Plasma membrane", "Plasma membrane ", "Sarcoplasmic reticulum ",
"Secreted"), class = "factor"), Cytoplasm = structure(c(1L,
15L, 13L, 10L, 1L, 13L, 1L, 1L, 5L, 2L, 11L, 1L, 1L, 1L,
5L, 8L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 14L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 5L, 9L, 2L, 3L, 6L, 7L, 2L, 1L, 2L, 4L, 11L, 12L,
5L, 1L, 1L, 1L, 7L, 3L, 1L, 2L, 2L, 2L), .Label = c("", " ",
"Cytoplasmic vesicle", "Endoplasmic reticulum", "Endosome",
"Endosome ", "Golgi apparatus", "Golgi apparatus ", "Golgi appartus",
"Midbody", "Mitochondrion ", "Nucleus ", "Plasma membrane",
"Plasma membrane ", "Secreted "), class = "factor"), Cytoplasm.1 = structure(c(1L,
4L, 7L, 7L, 1L, 1L, 1L, 1L, 5L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
6L, 3L, 2L, 1L, 1L, 1L), .Label = c("", " ", "Endoplasmic reticulum",
"Endoplasmic reticulum ", "Endosome", "Mitochondrion", "Plasma membrane"
), class = "factor"), Cytoplasmic.vesicle = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 1L, 1L, 1L, 1L), .Label = c("", "Golgi apparatus"
), class = "factor"), Perikaryon = structure(c(2L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L,
1L, 1L, 1L, 1L), .Label = c("", " ", "Golgi apparatus"), class = "factor"),
X = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L), .Label = c("",
"Cytoplasmic granule"), class = "factor"), X.1 = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 1L, 1L, 1L, 1L), .Label = c("", "Perikaryon"), class = "factor"),
X.2 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA), X.3 = c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA), Plasma.membrane = c(NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA), Plasma.membrane.1 = c(NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
)), .Names = c("FMR1", "Nucleus", "Chromosome", "Chromosome.1",
"Cytoplasm", "Cytoplasm.1", "Cytoplasmic.vesicle", "Perikaryon",
"X", "X.1", "X.2", "X.3", "Plasma.membrane", "Plasma.membrane.1"
), class = "data.frame", row.names = c(NA, -82L))
我尝试只为每一行获取唯一的列而没有运气,例如:
unique(df1) # Original data with repeats removed
dplyr::distinct(df1) # Retain only unique/distinct rows from an input tb
我认为问题是上面的函数正在查找相同的行名,这不是我想要的。我希望每行都有不同的列。我正在考虑使用melt
函数,但由于每行都有奇数列,所以这不会起作用。
我希望输出看起来像newDF
structure(list(FMR1 = structure(c(7L, 1L, 3L, 9L, 2L, 4L, 6L,
5L, 8L), .Label = c("AIMP1 EMAP2 SCYE1", "CDK1 CDC2 CDC28A CDKN1 P34CDC2",
"CEMIP KIAA1199", "ECT2", "HDAC6 KIAA0901 JM21", "ITGB1BP1 ICAP1",
"NGDN C14orf120", "PTPN23 KIAA1471", "RACGAP1 KIAA1478 MGCRACGAP"
), class = "factor"), Nucleus = structure(c(2L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = c("Nucleus", "Nucleus "), class = "factor"),
Chromosome = structure(c(1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L), .Label = c("Chromosome", "Cytoplasm"), class = "factor"),
Cytoplasmic.vesicle = structure(c(1L, 8L, 2L, 4L, 5L, 4L,
7L, 6L, 3L), .Label = c("Cytoplasm ", "Endoplasmic reticulum",
"Endosome", "Midbody", "Mitochondrion", "Perikaryon ", "Plasma membrane ",
"Secreted "), class = "factor"), Perikaryon = structure(c(1L,
2L, 3L, 3L, 1L, 3L, 1L, 1L, 1L), .Label = c("", "Endoplasmic reticulum ",
"Plasma membrane"), class = "factor"), Plasma.membrane = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA)), .Names = c("FMR1", "Nucleus",
"Chromosome", "Cytoplasmic.vesicle", "Perikaryon", "Plasma.membrane"
), class = "data.frame", row.names = c(NA, -9L))
从这里我想得到一个rowSums(df1)
所以我想把每个术语强制一个数字(例如细胞质囊泡= 1,核心= 1,内质网= 1等)但是遇到了这个虚拟数据集的问题。
df2 <- as.numeric(newDF)
Error: (list) object cannot be coerced to type 'double'
df2 <- as.numeric(newDF[,2:n])
Error in 2:n : NA/NaN argument
感谢您的帮助。
编辑我想计算newDF
中每一行中有多少独特列的计数如下:
FMR1 5
NGDN C14orf120 3
AIMP1 EMAP2 SCYE1 4
CEMIP KIAA1199 4
RACGAP1 KIAA1478 MGCRACGAP 4
CDK1 CDC2 CDC28A CDKN1 P34CDC2 3
ECT2 4
ITGB1BP1 ICAP1 3
HDAC6 KIAA0901 JM21 3
PTPN23 KIAA1471 3
答案 0 :(得分:2)
这可能是一种方法。由于您的预期结果是字符向量,因此无法将最终输出可视化。然而,您说您要检查每种蛋白质在数据中出现的列数。我希望我得到的结果就是你所追求的目标。
首先,我将所有列都转换为字符。然后,我使用gather()
将数据转换为长格式数据。对于每个亚细胞结构组(即亚细胞),我添加了行索引(例如,1表示原始数据中的第1行),并修剪空白区域。然后,删除蛋白质中NA
的所有行。删除""
和" "
的所有行。现在整理完成了。对于每一行(即row.index
),删除重复的蛋白质类型。
取消组合数据,最后计算每种蛋白质出现的列数(即,细胞质结构)。基本上,您想要计算此时每种蛋白质在数据集中出现的次数。
根据您的样本数据,我得到以下结果。但我不确定这是否是你想要的。 (我现在正在睡觉。所以我几个小时都帮不了你。如果有人可以跳进来,请这样做。)
mutate_all(mydf, as.character) %>%
gather(key = subcellular, value = protein) %>%
group_by(subcellular) %>%
mutate(row.index = 1:n(),
protein = trimws(protein)) %>%
filter(!is.na(protein)) %>%
filter(!protein %in% c("", " ")) %>%
group_by(row.index) %>%
filter(!duplicated(protein)) %>%
ungroup %>%
count(protein, sort = TRUE)
# protein n
# <chr> <int>
# 1 Cytoplasm 82
# 2 Plasma membrane 70
# 3 Nucleus 25
# 4 Endosome 9
# 5 Mitochondrion 9
# 6 Cytoplasmic vesicle 8
# 7 Golgi apparatus 7
# 8 Endoplasmic reticulum 5
# 9 Midbody 3
#10 Perikaryon 3
# ... with 87 more rows
鉴于jjl的评论,我做了以下事情。我没有计算每种蛋白质出现的列数,而是计算每行有多少蛋白质名称。
mutate_all(mydf, as.character) %>%
gather(key = subcellular, value = protein) %>%
group_by(subcellular) %>%
mutate(row.index = 1:n(),
protein = trimws(protein)) %>%
filter(!is.na(protein)) %>%
filter(!protein %in% c("", " ")) %>%
group_by(row.index) %>%
filter(!duplicated(protein)) %>%
ungroup %>%
count(row.index)
# row.index n
# <int> <int>
# 1 1 4
# 2 2 6
# 3 3 5
# 4 4 6
# 5 5 4
# 6 6 5
# 7 7 4
# 8 8 4
# 9 9 5
#10 10 3
# ... with 72 more rows
如果要删除第1列(即FMR1
),可以通过过滤该列来实现。在我最后使用filter(subcellular != "FMR1")
之前,我已将count()
添加到我的代码中。
mutate_all(mydf, as.character) %>%
gather(key = subcellular, value = protein) %>%
group_by(subcellular) %>%
mutate(row.index = 1:n(),
protein = trimws(protein)) %>%
filter(!is.na(protein)) %>%
filter(!protein %in% c("", " ")) %>%
group_by(row.index) %>%
filter(!duplicated(protein)) %>%
ungroup %>%
filter(subcellular != "FMR1") %>%
count(row.index)
# A tibble: 9 x 2
# row.index n
# <int> <int>
#1 1 3
#2 2 4
#3 3 4
#4 4 4
#5 5 3
#6 6 4
#7 7 3
#8 8 3
#9 9 3