2016-04-27 15 views
0

ich eine Spalte in meiner Datenrahmen haben wie folgtZählen Sie die Anzahl der Male (Frequenz) eine Zeichenfolge auftritt

Col1 
    ---------------------------------------------------------------------------- 
    Center for Animal Control, Division of Hypertension, Department of Medicine 
    Department of Surgery, Division of Primary Care, Center for Animal Control 
    Department of Internal Medicine, Division of Hypertension, Center for Animal Control 

Wie kann ich die Anzahl der Strings zählen kann, die auftreten, dass durch ein Komma getrennt ist, mit anderen Worten was ich versuche zu erreichen, ist etwas wie das unter

Affiliation       Freq 
    ------------------------------------------ 
    Center for Animal Control   3 
    Division of Hypertension   2 
    Department of Medicine    1 
    Department of Surgery    1 
    Division of Primary Care   1 
    Department of Internal Medicine  1 

Könnte jemand mir helfen, dies herauszufinden?

+2

Können Sie nach, was Sie bisher versucht haben, und was nicht funktioniert? – celdridge91190

Antwort

1

Hier ist ein Ansatz. Ersetzen Sie auch '\n' durch ein Komma, da Sie einige Zeilen in Ihrem Text haben.

df <- data.frame(col1 = rep("Center for Animal Control, Division of Hypertension, Department of Medicine, Department of Surgery, Division of Primary Care, Center for Animal Control, Department of Internal Medicine, Division of Hypertension, Center for Animal Control", 1), stringsAsFactors = FALSE) 
df$col1 <- gsub('\\n', ', ', df$col1) 
as.data.frame(table(unlist(strsplit(df$col1, ', ')))) 

Ausgabe als (auf Originaldaten) folgt:

       Var1 Freq 
1  Center for Animal Control 3 
2 Department of Internal Medicine 1 
3   Department of Medicine 1 
4   Department of Surgery 1 
5  Division of Hypertension 2 
6  Division of Primary Care 1 
+0

Was macht 'gsub ('\\ n', ',', df $ col1)? '\ n' als Zeilenumbruch benötigt kein zweites Escape wie' \\ n' – thelatemail

+0

Eigentlich ergibt das nicht das richtige Ergebnis - "Center for Animal Control" wird zweimal wiederholt. Sie müssen Leerzeichen auf beiden Seiten des Kommas berücksichtigen: 'data.frame (table (unlist (strsplit (as.zeichen (df $ col1)," \\ s *, \\ s * ")))) – thelatemail

+0

Bro, Ihr Code schlägt fehl, wenn col1 den Wert "Division of Hypertension, Department of Medicine, Center for Animal Control" hat. Wie in Ihrer Antwort wird ein separates Affiliation-Feld für "Center for Animal Control" erstellt. –

1

Annahme: Center for Animal Control, Division of Hypertension, Department of Medicine ist Wert für Zeile 1, Department of Surgery, Division of Primary Care, Center for Animal Control für Zeile 2 und so weiter.

df ist der Datenrahmen.

aff_val <- trimws(unlist(strsplit(df$col1,","))) 

ans <- data.frame(table(aff_val)) 

colnames(ans)[1] <- 'Affiliation' 
+0

Dies ist die gleiche Antwort wie unten. – Gopala

+0

@Gopala Bitte beachten Sie den Code sorgfältig. Es gibt absolut keine Notwendigkeit, die gsub Sache zu tun, wie es in Ihrer Antwort in Zeile 2 getan wird, wenn die von mir getroffene Annahme richtig ist. Auf diese Weise ist es anders. –

+0

In den Eingabedaten gibt es eine neue Zeile, die die Abteilungen (nicht Komma) trennt - Beispiel der Abteilung für Medizin und der Abteilung für Chirurgie. Sie werden nicht von "strsplit" auf einem Komma aufgenommen. – Gopala

0
library(stringr) 
a<-"Center for Animal Control, Division of Hypertension, Department of Medicine 
Department of Surgery, Division of Primary Care, Center for Animal Control 
Department of Internal Medicine, Division of Hypertension, Center for Animal Control" 
con<-textConnection(a) 
tbl<-read.table(con,sep=",") 
vec<-str_trim(unlist(tbl)) 
as.data.frame(table(vec)) 

Die Antwort ist

1  Center for Animal Control 3 
2 Department of Internal Medicine 1 
3   Department of Medicine 1 
4   Department of Surgery 1 
5  Division of Hypertension 2 
6  Division of Primary Care 1 
0
text = "Center for Animal Control, Division of Hypertension, Department of Medicine 
Department of Surgery, Division of Primary Care, Center for Animal Control 
Department of Internal Medicine, Division of Hypertension, Center for Animal Control" 

library(stringi) 
library(dplyr) 
library(tidyr) 

data_frame(text = text) %>% 
    mutate(line = text %>% stri_split_fixed("\n")) %>% 
    unnest(line) %>% 
    mutate(phrase = line %>% stri_split_fixed(", ")) %>% 
    unnest(phrase) %>% 
    count(phrase) 
1

Ich benutze scan und trimws für diese Textverarbeitungsaufgaben.

inp <- " Center for Animal Control, Division of Hypertension, Department of Medicine 
    Department of Surgery, Division of Primary Care, Center for Animal Control 
    Department of Internal Medicine, Division of Hypertension, Center for Animal Control" 

> table(trimws(scan(text=inp, what="", sep=","))) 
Read 9 items 

     Center for Animal Control Department of Internal Medicine 
           3        1 
     Department of Medicine   Department of Surgery 
           1        1 
     Division of Hypertension  Division of Primary Care 
           2        1 

auch wickeln as.data.frame Kann das Ergebnis um:

> as.data.frame(table( trimws(scan(text=inp, what="", sep=",")))) 
Read 9 items 
          Var1 Freq 
1  Center for Animal Control 3 
2 Department of Internal Medicine 1 
3   Department of Medicine 1 
4   Department of Surgery 1 
5  Division of Hypertension 2 
6  Division of Primary Care 1 
Verwandte Themen