让r输出每个数组中有多少个1、2、3等字母的单词



我需要创建一个数组,其中包含一个单词出现的次数为1,2,3、……等等,字母出现在每个句子中。也就是带的数组4行(每个句子一列)和9列(1信,2字母等)

这是我目前为止写的:

text <- c("Take a vector below", 
"Assume that you start summation from the first element adding element by element", 
"Your task is to find the maximal number of elements you can take",
"while the total summation stays below some given threshold")
words <- strsplit(text, split = " ") 

要获取每个句子的单词长度,请尝试:

nwords <- sapply(words, length)
sentence <- rep(1:4, nwords)
wordlength <- unlist(sapply(words, nchar))
table(wordlength, sentence)
#       sentence
# wordlength 1 2 3 4
#          1 1 0 0 0
#          2 0 1 3 0
#          3 0 2 3 1
#          4 1 2 4 1
#          5 1 2 0 5
#          6 1 2 1 0
#          7 0 3 1 0
#          8 0 0 1 0
#          9 0 1 0 2

相关内容

最新更新