2016-12-12 3 views
0

私は、データフレーム内のある行の各単語が所定の時間に発生する回数を数えようとしています。私はこの結果を取得しようとしていますテーブルオブジェクトを元のデータフレームに戻して結合しますか?

library(stringr) 

df <- data.frame("Corpus" = c("this is some text", 
           "here is some more text text", 
           "more food for everyone", 
           "less for no one", 
           "something text here is some more text", 
           "everyone should go home", 
           "more random text", 
           "random text more more more", 
           "plenty of random text", 
           "the final piece of random everyone text"), 

       "Class" = c("X", "Y", "Y", "Y", "Y", 
          "Y", "Y", "Z", 
          "Z", "Z"), 

       "OpenTime" = c("12/01/2016 10:45:00", "11/07/2016 10:32:00", 
           "11/15/2015 01:45:00", "08/23/2012 1:23:00", 
           "12/17/2016 11:45:00", "12/16/2016 9:47:00", 
           "04/11/2015 04:23:00", "11/27/2016 12:12:00", 
           "08/25/2015 10:46:00", "09/27/2016 10:46:00")) 

:ここに私のデータフレームである

Class OpenTime    Word Frequency 
X  12/01/2016 10:45:00 this 1 
X  12/01/2016 10:45:00 is  1 
X  12/01/2016 10:45:00 some 1 
X  12/01/2016 10:45:00 text 1 
Y  11/07/2016 10:32:00 here 1 
Y  11/07/2016 10:32:00 is  1 
Y  11/07/2016 10:32:00 some 1 
Y  11/07/2016 10:32:00 more 1 
Y  11/07/2016 10:32:00 text 2 
... 

私はすべてのdplyrgroupbyでこれを行うのが大好きですが、私はまだ持っていないその仕事へ。代わりに、これは私が試したものです:

splits <- strsplit(as.character(df$Corpus), split = " ") 

counts <- lapply(splits, table) 

counts.melted <- lapply(counts, melt) 

これは私が欲しい転置ビューできます:

> counts.melted 
[[1]] 
    Var1 value 
1 is  1 
2 some  1 
3 text  1 
4 this  1 

[[2]] 
    Var1 value 
1 here  1 
2 is  1 
3 more  1 
4 some  1 
5 text  1 
... 

をしかし、どのように私が生成するために、元のデータとバック溶融したベクトルのリストを結び付けることができます希望の出力は?私はrepを使って、各行にある単語の数だけClassの値を繰り返してみましたが、ほとんど成功しませんでした。 forループでこれをすべて行うのは簡単ですが、多くの場合lapplyのようなベクトル化されたメソッドを使用します。

out.df <- data.frame("RRN" = NULL, "OpenTime" = NULL, 
       "Word" = NULL, "Frequency" = NULL) 

答えて

0

将来的にここに来る人にとっては、ほとんどの問題を私の問題にベクトル化できました。残念ながら、私はまだforループの代わりにlapplyを使用する方法を探していますが、これは私が欲しいものです:

# split each row in the corpus column on spaces 
splits <- strsplit(as.character(df$Corpus), split = " ") 

# count the number of times each word in a row appears in that row 
counts <- lapply(splits, table) 

# melt that table to make things more palatable 
counts.melted <- lapply(counts, melt) 

# the result data frame to which we'll append our results 
out.df <- data.frame("Class" = c(), "OpenTime" = c(), 
        "Word" = c(), "Frequency" = c()) 

# it would be better to vectorize this, using something like lapply 
for(idx in 1:length(counts.melted)){ 

    # coerce the melted table at that index to a data frame 
    count.df <- as.data.frame(counts.melted[idx]) 

    # change the column names 
    names(count.df) <- c("Word", "Frequency") 

    # repeat the Classand time for that row to fill in those column 
    count.df[, 'Class'] <- rep(as.character(df[idx, "Class"]), nrow(count.df)) 
    count.df[, 'OpenTime'] <- rep(as.character(df[idx, "OpenTime"]), nrow(count.df)) 

    # append the results 
    out.df <- rbind(out.df, count.df) 
} 
関連する問題