简体   繁体   English

如何根据列表的长度拆分字符向量

[英]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.

相关问题 根据长度分割字符向量 - Split character vector based on length 如何基于位置的数字向量分割字符向量 - How to split a character vector based on a numeric vector for positions R:在特定字符处将长度n&gt; 1的字符向量拆分为n个向量的列表 - R: Split character vector of length n>1, at specific character, into a list of n vectors 如何拆分字符元素以创建基于 \n position 的向量 - How to split a character element to create a vector based on \n position 如何基于连续的唯一值将向量拆分为列表 - How to split vector into list based on consecutive unique values 如何根据r中另一个向量的值将向量列表拆分为子列表 - How to split a list of vectors into sublist based on a values of another vector in r 如何根据字符长度从列表中选择值? - How do I select values from a list based on character length? 如何根据字符变量将R dataframe拆分成列表 - How to split R dataframe into list based on character variable 我如何解决以下错误?输入必须是任意长度的字符向量或字符向量列表,每个字符向量的长度为1 - How do I solve the following error?Input must be a character vector of any length or a list of character vectors, each of which has a length of 1 如何解决:输入必须是任意长度的字符向量或字符向量列表,每个字符向量的长度为1 - How do I solve : Input must be a character vector of any length or a list of character vectors, each of which has a length of 1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM