How to split character vectors based on the length of the list

I have this character vector:

a<-c("tanaman","cabai","banget","hama","sakit","tanaman","koramil","nogosari","melaks","ecek","padi","ppl","ds","rambun")

I want to split the character vector into lists according to the length of the list, as shown below:

split.char<-list(c("tanaman","cabai"),c("banget","penyakit","tanaman"),c("koramil","pengecekan","rambun"))

I'm trying to define the list split. Char using sapply (split. Char, length) Length of char

Length <- sapply(split.char,length)
for(i in Length){
  split(a,Length(i))
 }

But I don't get the required output. I keep receiving this warning 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

Solution

You can try the following methods:

split(x=a,f=rep(seq_along(Length),Length))

F must have the same length as X (if its length is 1 or X, the separator will be recycled)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>