R编程 - 按字数对列表进行排序

时间:2016-09-20 14:34:26

标签: r sorting vector

我有一个列表,其中包含对调查的多个回复。我试图按每个响应中的单词数对列表中的元素进行排序。我在网上找到的所有东西都使用nchar,它按字符数而不是单词排序。

这是我到目前为止所做的:

gala.vector <- unlist(strsplit, gala.list, " ")

用空格分割矢量,将其分隔为单词

gala.list.sorted <- gala.list[order(gala.vector, decreasing=TRUE)]

但是我被告知参数“gala.vector不是向量并且收到错误消息

2 个答案:

答案 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" 
相关问题