2016-12-07 5 views
-2

Ich arbeite an Text Mining-Projekt und ich habe eine spärliche Matrix in R mit tm-Paket erstellt. Die Daten sind in unten genannten Format:Wie bekomme ich die Worthäufigkeit und entsprechende Wörter in R

Sample Data format

ich es im folgenden Format wollen: Resultant Data Format

mit Daten Gerangel Hilfe brauchen.

+1

Willkommen bei Stackoverflow. Bitte werfen Sie einen Blick auf diese Tipps, wie Sie ein [minimales, vollständiges und überprüfbares Beispiel] (http://stackoverflow.com/help/mcve) erstellen können, sowie auf diesen Post zu [ein großartiges Beispiel in R erstellen] (http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Vielleicht sind auch die folgenden Tipps zu [eine gute Frage stellen] (http://stackoverflow.com/help/how-to-ask) lohnenswert. – lmo

Antwort

0

Eine Idee mit dplyr und tidyr,

library(dplyr) 
library(tidyr) 
df %>% 
group_by(C1, C2, C3) %>% 
summarise_each(funs(sum)) %>% 
gather(word, freq, not:great) 

#Source: local data frame [24 x 5] 
#Groups: C1, C2 [4] 

#  C1  C2 C3 word freq 
# <dbl> <fctr> <dbl> <chr> <dbl> 
#1  1  a  1 not  0 
#2  1  a  2 not  1 
#3  2  b  3 not  2 
#4  2  d  2 not  0 
#5  3  c  1 not  1 
#6  3  c  2 not  0 
#7  1  a  1 cant  1 
#8  1  a  2 cant  0 
#9  2  b  3 cant  0 
#10  2  d  2 cant  0 

DATA

dput(df) 
structure(list(C1 = c(1, 2, 3, 2, 3, 2, 1), C2 = structure(c(1L, 
2L, 3L, 2L, 3L, 4L, 1L), .Label = c("a", "b", "c", "d"), class = "factor"), 
    C3 = c(2, 3, 2, 3, 1, 2, 1), not = c(1, 1, 0, 1, 1, 0, 0), 
    cant = c(0, 0, 0, 0, 1, 0, 1), able = c(1, 0, 0, 0, 0, 0, 
    0), great = c(0, 0, 0, 0, 0, 1, 1)), .Names = c("C1", "C2", 
"C3", "not", "cant", "able", "great"), row.names = c(NA, -7L), class = "data.frame") 
Verwandte Themen