[英]How to split a character vector based on length of a list
I have a character vector like this: 我有这样的角色矢量:
a<-c("tanaman","cabai","banget","hama","sakit","tanaman","koramil","nogosari",
"melaks","ecek","hama","tanaman","padi","ppl","ds","rambun")
And I want to split character vector into list based on length of a list like below : 我想根据列表的长度将字符向量拆分为列表,如下所示:
split.char<-list(c("tanaman", "cabai"),c("banget", "hama",
"penyakit", "tanaman"),c("koramil", "nogosari", "melaks", "pengecekan", "hama",
"tanaman" , "padi", "ppl", "ds", "rambun"))
I'm trying to use sapply(split.char, length)
for defining length of list split.char
我正在尝试使用
sapply(split.char, length)
来定义列表split.char
长度
Length <- sapply(split.char, length)
for(i in Length){
split(a, Length(i))
}
But I didn't get the desired output and I constantly get this warnings message: 但我没有得到所需的输出,我不断收到此警告消息:
1: In split.default(ok, Length) : data length is not a multiple of split variable
2: In split.default(ok, Length) : data length is not a multiple of split variable
3: In split.default(ok, Length) : data length is not a multiple of split variable
You could try the following: 您可以尝试以下方法:
split(x=a, f=rep(seq_along(Length), Length))
f
has to be of the same length as x
(if it is of length one or a divider of x
it would be recycled). f
必须与x
具有相同的长度(如果它的长度为1或x
的分隔符将被再循环)。
You could use relist
which will give similar structure for 'a' as the 'split.char' (including the length
) 您可以使用
relist
,它将为'a'提供与'split.char'类似的结构(包括length
)
relist(a, skeleton=split.char)
#[[1]]
#[1] "tanaman" "cabai"
#[[2]]
#[1] "banget" "hama" "sakit" "tanaman"
#[[3]]
#[1] "koramil" "nogosari" "melaks" "ecek" "hama" "tanaman"
#[7] "padi" "ppl" "ds" "rambun"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.