require(ngram)
require(stringr)
res<-readLines("text1.txt")
wordlength=0
for(j in 1:length(res) ){
temp<-sapply(strsplit(res[j]," "), length)
if (temp>=wordlength) {
wordlength=temp
}
}
rm("temp")
data<-data.frame
for(i in 1:length(res) ){
x<-res[i]
ng<-ngram(x,n=2)
temp<-babble(ng,genlen=500,seed=123)
data[i]<-ngram(temp,n=2)
}
get.ngrams(ngram(bab[1,],n=2))
babng<-matrix(nrow=length(res),wordlength)
I'm trying to save my ngram data in a data frame from this loop, I've also tried to save it in a matrix but this error is showing:
"object of type 'closure' is not subsettable"
I want to get the frequency distribution of the every 2gram element from the babbler. Sorry for my messy coding. I'm new to R.
In your code, you call:
which assigns the function
data.frame
to the variabledata
. Later, you calldata[i]<-ngram(temp,n=2)
which is causing the error, because the function assigned to the variabledata
cannot be sub-setted using the subset operator[
. You probably want to create a data.frame object and assign it to the variabledata
by calling the functiondata.frame
via: