I have tried running the code below, however it does not work as the arguments are not all of equal length.
sentence= "I like tea and I love coffee and biscuits"
words = function(x) {
txt = unlist(strsplit(x,' '))
wl = list()
for(i in seq_along(txt)) {
wrd = txt[i]
wl[[wrd]] = c(wl[[wrd]], i)
}
class(wl) <- "wordclass"
return(wl)
}
summary.wordclass <- function(y) {
cat("the frequency of words",names(sort(table(y), decreasing=TRUE)),"\n")
}
wordfreq=words(sentence)
summary(wordfreq)
I want to get an output like
[1] "I" "and" "like" "tea" "love" "coffee"
However, I am getting the error
Error in table(y) : all arguments must have the same length
If anyone could help that would be great!
would
names(sort(table(unlist(strsplit(sentence," "))),decreasing=T))
work for you ?
the output is
[1] "and" "I" "biscuits" "coffee" "like" "love" "tea"
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다