Table of Contents
The error “non-character argument” in the function strsplit(unitspec, ” “) occurs when the argument passed to the function is not a character type. This function is designed to split a string into separate elements based on a specified delimiter, and it requires the input to be a character type. If the argument is not a character type, the function will not be able to perform the splitting operation and will return an error. To resolve this error, the input argument must be converted to a character type before being passed to the strsplit function.
Fix: error in strsplit(unitspec, ” “) : non-character argument
One error you may encounter in R is:
Error in strsplit(df$my_column, split = "1") : non-character argument
This error usually occurs when you attempt to use the strsplit() function in R to split up a string, yet the object you’re working with is not a string.
This tutorial shares exactly how to fix this error.
How to Reproduce the Error
Suppose we have the following data frame in R:
#create data frame df <- data.frame(team=c('A', 'B', 'C'), points=c(91910, 14015, 120215)) #view data frame df team points 1 A 91910 2 B 14015 3 C 120215
Now suppose we attempt to use the strsplit() function to split the values in the “points” column based on where the number 1 occurs:
#attempt to split values in points column
strsplit(df$points, split="1")
Error in strsplit(df$points, split = "1") : non-character argument
We receive an error because the variable “points” is not a character.
We can confirm this by checking the class of this variable:
#display class of "points" variable
class(df$points)[1] "numeric"
We can see that this variable has a class of numeric.
How to Fix the Error
The way to fix this error is to use as.character() to convert the “points” variable to a character before attempting to use the strsplit() function:
#split values in points column based on where 1 appears
strsplit(as.character(df$points), split="1")
[[1]]
[1] "9" "9" "0"
[[2]]
[1] "" "40" "5"
[[3]]
[1] "" "202" "5" This time we’re able to successfully split each value in the “points” column because we first used the as.character() function to convert “points” to a character.
The following tutorials explain how to troubleshoot other common errors in R:
Cite this article
stats writer (2024). Why am I getting the error “non-character argument” in strsplit(unitspec, ” “)?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/why-am-i-getting-the-error-non-character-argument-in-strsplitunitspec/
stats writer. "Why am I getting the error “non-character argument” in strsplit(unitspec, ” “)?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/why-am-i-getting-the-error-non-character-argument-in-strsplitunitspec/.
stats writer. "Why am I getting the error “non-character argument” in strsplit(unitspec, ” “)?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/why-am-i-getting-the-error-non-character-argument-in-strsplitunitspec/.
stats writer (2024) 'Why am I getting the error “non-character argument” in strsplit(unitspec, ” “)?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/why-am-i-getting-the-error-non-character-argument-in-strsplitunitspec/.
[1] stats writer, "Why am I getting the error “non-character argument” in strsplit(unitspec, ” “)?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. Why am I getting the error “non-character argument” in strsplit(unitspec, ” “)?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
