我有一个像这样的角色矢量:
split.char<-list(c("tanaman", "cabai"),c("banget", "hama",
"penyakit", "tanaman"),c("koramil", "nogosari", "melaks", "pengecekan", "hama",
"tanaman" , "padi", "ppl", "ds", "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
但我没有得到所需的输出,我不断得到这个警告信息:
network1:
type: Internal
答案 0 :(得分:4)
您可以尝试以下方法:
split(x=a, f=rep(seq_along(Length), Length))
f
必须与x
的长度相同(如果长度为1或x
的分隔符,则会被回收)。
答案 1 :(得分:3)
您可以使用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"