Hello, I am a newbie in all programming stuff. I feel like I have a super simple problem, but I cannot understand what's wrong. So, please excuse my insufficient knowledge.
This is my function:
best <- function(x) {
x <- as.character(x)
print(paste(x, "is the best"))
}
best(Eiffel65)
I thought that using as.character() function would be helpful. But I receive this error:
Error in best(Eiffel65) : object 'Eiffel65' not found
I can obtain the expected output by using quotes:
best("Eiffel65")
[1] "Eiffel65 is the best"
It is not a great big problem, but I just want to know.
If you use a quoted piece of text, it is interpreted as a string, if it is unquoted though, R thinks it is a variable and this starts looking for a declared variable with that name and passes on its value to the function. Since Eiffel65 is not defined, R throws an error.
Here is a showcase when you do declare the variable Eiffel65 :
Eiffel65 = "other text"
best <- function(x) {
x <- as.character(x)
print(paste(x, "is the best"))
}
best(Eiffel65)
#> [1] "other text is the best"