我有矢量图
length
# [1] 15,34, 12,24, 225,
# Levels: 12,24, 15,34, 225,
我想用逗号分隔它们以最终列出这些值
试过:
strsplit(length, ",")
但不断收到错误消息
Error in strsplit(length, ",") : non-character argument
您的“长度”对象是factor
:
如错误消息所示,strsplit
需要一个字符向量作为输入。
尝试:
strsplit(as.character(length), ",")
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"
您也可以使用:(x
来自@Ananda Mahto 的帖子)
library(stringr)
str_split(x, ",")
#[[1]]
# [1] "1" "2"
#[[2]]
#[1] "3" "4"
#[[3]]
#[1] "5" "6"
或者
str_extract_all(x, "[0-9]+")
或者
library(stringi)
stri_extract_all_regex(x, "[0-9]+")