我有一个列表,其中包含对调查的多个回复。我试图按每个响应中的单词数对列表中的元素进行排序。我在网上找到的所有东西都使用nchar,它按字符数而不是单词排序。
这是我到目前为止所做的:
gala.vector <- unlist(strsplit, gala.list, " ")
gala.list.sorted <- gala.list[order(gala.vector, decreasing=TRUE)]
但是我被告知参数“gala.vector不是向量并且收到错误消息
答案 0 :(得分:0)
我们可以split
每个list
元素('gala.list'),unlist
it('lst1')中的句子,使用lengths
来获取每个list
元素的'长度'和order
就可以命令'gala.list'
lst1 <- lapply(gala.list, function(x) unlist(strsplit(x, " ")))
gala.list[order(lengths(lst1), decreasing = TRUE)]
gala.list <- list("something else to do", "three words only",
"using dput")
答案 1 :(得分:0)
可重复的示例和功能解决方案:
w <- c("this has four words",
"this one has five words",
"two words")
word_count <- function(x) {
x <- lapply(x, function(y) trimws(strsplit(y, " ")[[1]]))
x <- lapply(x, function(y) y[y != ""])
unlist(lapply(x, length))
}
> word_count(w)
# [1] 4 5 2
sort_by_wc <- function(x, decreasing = TRUE) {
seq <- word_count(x)
x[order(seq, decreasing = decreasing)]
}
> sort_by_wc(w)
# [1] "this one has five words"
# [2] "this has four words"
# [3] "two words"