R Ulangi data.frame dan dapatkan jumlah variabel [duplikat]

Saya memiliki data.frame dengan dua kolom, pengidentifikasi unik dan hasil. Saya perlu mengulang data.frame dan menghitung berapa banyak Pengidentifikasi unik yang ada dan menghitung hasil uniknya. Kolom hasil dapat memiliki tiga kemungkinan hasil, Positif, Negatif, atau Ambigu. Jadi misalnya jika ada 10 pengidentifikasi “RVP PCR” saya perlu membuat baris dengan empat kolom lagi, "Hitungan", "Positif", "Negatif", "Ambigu" dan di kolom tersebut harus ada hitungan berapa kali itu terjadi. Jadi dalam contoh dengan 10 Pengidentifikasi ”RVP PCR”, baris keluaran harus menampilkan Pengidentifikasi kemudian hitungan 10, 7 Negatif, 1 Positif dan 2 Ambigu. Bagaimana Anda mencapai ini dengan R ?

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")

person Nodedeveloper101    schedule 25.02.2016    source sumber


Jawaban (4)


library(dplyr)
library(tidyr)
foo %>%
  group_by(identifier, result) %>%
  summarise(n = n()) %>%
  spread(key = result, value = n, drop = FALSE, fill = 0) %>%
  mutate(Total = Ambiguous + Negative + Positive) %>%
  filter(Total > 0)

Hasil

Source: local data frame [7 x 5]
Groups: identifier [7]

  identifier Ambiguous Negative Positive Total
      (fctr)     (dbl)    (dbl)    (dbl) (dbl)
1    CWP-KOH         0        2        0     2
2 MPNEUMOPCR         0        0        2     2
3 PARAFLUPCR         0        3        1     4
4    PCP PCR         0        0        1     1
5  RESPMINI          0        4        0     4
6      RSPFA         0        7        1     8
7    RVP PCR         0       28        2    30
person Thierry    schedule 25.02.2016
comment
Saya dapat melihat banyak pengertian di bagian fill = 0 untuk kasus khusus ini. Tidak memiliki hitungan (0) secara konseptual berbeda dengan tidak memiliki catatan (NA). - person PavoDive; 25.02.2016
comment
Inilah yang saya cari, Terima kasih! @Thierry - person Nodedeveloper101; 25.02.2016

Saya tidak begitu yakin dengan hasil yang diharapkan, tetapi Anda dapat membentuk ulang data Anda:

library(reshape2)

dcast(foo, identifier~result, fun.aggregate= length)

Ini menghasilkan:

  identifier Negative Positive
1    CWP-KOH        2        0
2 MPNEUMOPCR        0        2
3 PARAFLUPCR        3        1
4    PCP PCR        0        1
5  RESPMINI         4        0
6      RSPFA        7        1
7    RVP PCR       28        2

######## EDIT UNTUK MENAMBAHKAN #############

Dengan data yang Anda berikan, tidak mungkin "RVP PCR" akan memberikan hasil yang Anda nyatakan.

person PavoDive    schedule 25.02.2016

Data dalam format panjang. Ubah menjadi lebar terlebih dahulu menggunakan perintah dcast dari perpustakaan reshape2. Tambahkan kolom dan ambil jumlah semua baris.

library(reshape2)    
widedata<-dcast(foo,identifier~result)
widedata$Count<-0 #adds column for Count
widedata$Count<-rowSums (widedata[,2:4], na.rm = FALSE, dims = 1) #[,2:4] since the data will have a column for ambiguous as well.
person Shreyas Joshi    schedule 25.02.2016

Tanpa paket tambahan yang dapat Anda lakukan:

xtabs(~ identifier + result, data=droplevels(foo))

Ini memberikan hasil ini:

> xtabs(~ identifier + result, data=droplevels(foo))
            result
identifier   Negative Positive
  CWP-KOH           2        0
  MPNEUMOPCR        0        2
  PARAFLUPCR        3        1
  PCP PCR           0        1
  RESPMINI          4        0
  RSPFA             7        1
  RVP PCR          28        2

Jika Anda menginginkan kerangka data:

as.data.frame(unclass(xtabs(~ identifier + result, data=droplevels(foo))))

Jika ingin hasilnya dalam format panjang, Anda juga bisa melakukan:

foo$count <- 1
aggregate(count ~ identifier+result, data=foo, FUN=length)
person jogo    schedule 25.02.2016
comment
Solusi dasar yang bagus, namun mungkin ada gunanya hasilnya menjadi bingkai data? - person PavoDive; 25.02.2016
comment
@PavoDive Saya mengedit jawaban saya untuk memasukkan varian kerangka data. Jika Anda menyukai solusi dasar saya, Anda dapat memilihnya. - person jogo; 27.02.2016