如何根据列表的长度拆分字符向量
发布时间:2020-12-15 04:27:39 所属栏目:Java 来源:网络整理
导读:我有这样的角色矢量: a-c("tanaman","cabai","banget","hama","sakit","tanaman","koramil","nogosari","melaks","ecek","padi","ppl","ds","rambun") 我想根据列表的长度将字符向量拆分为列表,如下所示: split.char-list(c("tanaman","cabai"),c("banget"
我有这样的角色矢量:
a<-c("tanaman","cabai","banget","hama","sakit","tanaman","koramil","nogosari","melaks","ecek","padi","ppl","ds","rambun") 我想根据列表的长度将字符向量拆分为列表,如下所示: split.char<-list(c("tanaman","cabai"),c("banget","penyakit","tanaman"),c("koramil","pengecekan","rambun")) 我正在尝试使用sapply(split.char,length)来定义列表split.char的长度 Length <- sapply(split.char,length) for(i in Length){ split(a,Length(i)) } 但我没有得到所需的输出,我不断收到此警告消息: 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 解决方法
您可以尝试以下方法:
split(x=a,f=rep(seq_along(Length),Length)) f必须与x具有相同的长度(如果它的长度为1或x的分隔符将被再循环). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |