error in strsplit when trying to separate by a comma

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 15:44:40

Your "length" object is a factor:

As the error message indicates, strsplit expects a character vector as the input.

Try:

strsplit(as.character(length), ",") 

Demo

x <- factor(c("1,2", "3,4", "5,6"))
strsplit(x, ",")
# Error in strsplit(x, ",") : non-character argument
strsplit(as.character(x), ",")
# [[1]]
# [1] "1" "2"
# 
# [[2]]
# [1] "3" "4"
# 
# [[3]]
# [1] "5" "6"

You could also use: (x from @Ananda Mahto's post)

 library(stringr)
 str_split(x, ",")
 #[[1]]
 # [1] "1" "2"

 #[[2]]
 #[1] "3" "4"

 #[[3]]
 #[1] "5" "6"

Or

  str_extract_all(x, "[0-9]+")

Or

 library(stringi)
 stri_extract_all_regex(x, "[0-9]+")
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!