Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error in strsplit when trying to separate by a comma

Tags:

r

strsplit

I have the vector

length
# [1] 15,34, 12,24, 225,
# Levels: 12,24, 15,34, 225,

and I want to separate them by the comma to eventually make a list of these values

Tried:

strsplit(length, ",") 

but keep getting the error message

Error in strsplit(length, ",") : non-character argument
like image 944
Nazrath10R Avatar asked Dec 14 '22 19:12

Nazrath10R


1 Answers

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"
like image 98
A5C1D2H2I1M1N2O1R2T1 Avatar answered Jan 31 '23 19:01

A5C1D2H2I1M1N2O1R2T1