当行为空时,将列表的元素分组到子列表中

时间:2016-01-19 13:56:04

标签: r

我想使用列表中存在的""表示的空白行,以便将其间的所有行分组到子列表中。

例如,我有一个很长的列表,如下所示:

    > data
 [1] "data science"
 [2] "big data"
 [3] "machine learning"
 [4] "BI"
 [5] "analytics"
 [6] ""
 [7] "SAS" 
 [8] "R"
 [9] "Python"
[10] "Spark"
[11] ""
[12] "Hive"
[13] "PIG"
[14] "IMPALA"
....

我想要这样的事情:

 > output
 [[1]] [1] "data science" "big data" "machine learning" "BI" "analytics"
 [[2]] [1] "SAS" "R" "Python" "Spark"
 [[3]] [1] "Hive" "PIG" "IMPALA"

我的输出中的索引可能是错误的,但总的来说这就是我想要的。

也许split的某些人会这样做。

1 个答案:

答案 0 :(得分:1)

split可以帮助您,这是正确的。如果你cumsum是一个逻辑向量,它会将原始向量分成几组。然后你必须删除第一个元素,因为它是“”。这就是taillapply所做的事情:

set.seed(201)
x <- sample(letters, 20, replace = T)
x[c(6,12)] <- ""

> lapply(split(x, cumsum(x == "")), tail, -1)
$`0`
[1] "p" "p" "q" "r"

$`1`
[1] "v" "n" "g" "l" "t"

$`2`
[1] "p" "p" "n" "e" "t" "c" "j" "m"