我有一個data.frame,它有兩列,一個唯一的標識符和一個結果。我需要循環遍歷data.frame,並計算有多少個唯一標識符,以及唯一結果的數量。結果列可以有三個可能的結果,正面,負面或不明確。因此,例如,如果有10個「RVP PCR」標識符,我需要創建一個包含「Count」,「Positive」,「Negative」,「Ambiguous」四列的行,並且在這些列中應該計算多少次他們發生了。因此,在具有10個「RVP PCR」標識符的示例中,輸出行應該顯示標識符而不是計數10,7個負數,1個正數和2個模糊數。你如何用R來完成這個任務?R通過data.frame循環並獲取變量計數
str(foo)
>
'data.frame': 51 obs. of 2 variables:
$ identifier: Factor w/ 99 levels "ADENOPCR","ALB-BF",..: 51 51 56 56 57 57 57 57 18 18 ...
$ result : Factor w/ 3 levels "Ambiguous","Negative",..: 2 1 2 1 2 1 2 1 2 1 ...
dput(foo)
>
structure(list(identifier = structure(c(80L, 80L, 80L, 80L, 80L,
80L, 80L, 80L, 80L, 80L, 80L, 80L, 80L, 80L, 80L, 80L, 80L, 80L,
80L, 80L, 80L, 80L, 80L, 80L, 80L, 80L, 80L, 80L, 80L, 80L, 64L,
18L, 18L, 76L, 76L, 76L, 70L, 70L, 70L, 70L, 71L, 64L, 77L, 77L,
77L, 77L, 77L, 77L, 77L, 77L, 76L), .Label = c("ADENOPCR", "ALB-BF",
"ASPERAG", "ASPERAGB", "BDGLUCAN", "BLASTO", "BORD PCR", "BPERT",
"CMV QNT", "CMVPCR", "COCCI", "COCCI G/M", "COCCI PAN", "COCCI-PPT",
"CPNEUMOPCR", "CRP", "CRY BLD", "CWP-KOH", "DIFF CONF", "EBV PAN",
"EBV PAN 2", "EBV QNT", "EXCEPT", "EXCEPT TT", "FLUFAC", "FUNG PKG",
"FUNGSEQ", "GLU-FL", "HERP I", "HHV6PCR", "HISTO", "HISTO PPT",
"HISTOAG S", "HISTOGM U", "HMPVFA", "HMPVPCR", "HSVPCR", "LEGAG-U",
"LEGIONFA", "LEGIONPCR", "MA AFB", "MA FUNGAL", "MA MIC", "MA MTBPRIM",
"MC AFB", "MC AFBID", "MC AFBR", "MC BAL", "MC BLD", "MC CYST",
"MC FUNG", "MC FUNGID", "MC Legion", "MC LEGION", "MC MTD", "MC NOC",
"MC RESP", "MC STAPH", "MC Strep", "MC STREP", "MC VRE", "MC W",
"MICROSEQ", "MPNEUMOPCR", "MS CWP", "MTBRIF PCR", "MYCO-M", "NG REPORT",
"ORGSEQ", "PARAFLUPCR", "PCP PCR", "PNEUMO AB", "PNEUMST", "PNEUMST R",
"RESPMINI", "RESPMINI ", "RSPFA", "RSPFAC", "RSV", "RVP PCR",
"RVPPCR", "SPN AG", "TP-FL", "V CMVC", "V FLUC", "V HSVC", "V HSVCT",
"V RESPC", "V Urea", "V VIC", "V VIC R", "V VIRAL", "V VIRAL N",
"V VIRAL R", "V VZV", "VDRL CSF", "VZVFAC", "VZVPCR", "WNILE PCR"
), class = "factor"), result = structure(c(2L, 2L, 3L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 3L,
2L, 2L, 2L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Ambiguous",
"Negative", "Positive"), class = "factor")), .Names = c("identifier",
"result"), row.names = 1500:1550, class = "data.frame")
對於這個特定情況,我可以在'fill = 0'部分看到很多意義。沒有計數(0)在概念上不同於沒有記錄(NA)。 – PavoDive
這正是我正在尋找的,謝謝! @Thierry – Nodedeveloper101