我有一个列表,列出了对一项调查的几个回答。我尝试按照每个响应中的单词数对列表中的元素进行排序。我在网上找到的所有东西都使用nchar,它按字符数排序,而不是按单词排序。
这就是我到目前为止所知道的:
gala.vector <- unlist(strsplit, gala.list, " ")用空格拆分向量,在单词之间拆分
gala.list.sorted <- gala.list[order(gala.vector, decreasing=TRUE)]然而,我被告知参数"gala.vector不是一个向量,并且接收到一个错误消息
发布于 2016-09-20 22:38:13
我们可以在每个list元素中使用split语句( 'gala.list‘),使用unlist it ('lst1'),使用lengths获取每个list元素的'length’,并使用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") 发布于 2016-09-20 22:42:24
可重现的示例和函数解决方案:
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" https://stackoverflow.com/questions/39596925
复制相似问题