[R] Coercing Output from mget() into Proper Data Frame

ntyson at clovermail.net ntyson at clovermail.net
Thu Jun 9 21:48:42 CEST 2011


Hello R-philes:

I have the following function that gets the output of mget() and  
converts it to a data frame to return.  What I am finding is that the  
dimensions are wrong.  Basically, I get:

  bridesmaid wed  u see  m gt lt like love X.0 dress pagetrack one go X3 get
1         56  35 27  30 24 20 20   23   28  17    25        16  16 28 15  26

Instead, I want something like:

[1] bridesmaid 56

In other words, I want the word in the first column and the frequency  
in the second column.

Any help would be very much appreciated.

Regards,

Na'im

library(Rstem)

# make a data frame of stems and their frequencies
stem_freq_list <- function(freqFile) {
     stem_dict <- new.env(parent=emptyenv(), hash=TRUE)
     freq_dist <- read.csv(freqFile,header=TRUE)
     words <- as.character(freq_dist[,1])
     freqs <- as.numeric(freq_dist[,2])
     stems <- wordStem(words, language="english")
     uniq_stems <- c()

     # make a hash table of stems and their frequencies
     for (i in 1:length(words)) {
         word <- words[i]; stem <- stems[i]; freq <- freqs[i]
         if (exists(stem, envir=stem_dict)) {
             cnt <- get(stem, envir=stem_dict)
             cnt <- cnt + freqs[i]
             assign(stem,cnt,envir=stem_dict)
         } else {
             assign(stem, freq, envir=stem_dict)
             uniq_stems <- append(uniq_stems, stem)
         }
     }

     # return data frame of stems and their frequencies
     stem_freqs_list <- mget(uniq_stems,stem_dict)
     stem_freqs <- do.call(rbind,stem_freqs_list)
     return(stem_freqs_list)
}



More information about the R-help mailing list