Error in assign a numeric value

Hi!
I want to transform a string in a number, this is my code:

test <- starwars
home <- test$homeworld
home <- as.numeric(home )

but I have this error:
Error: 'list' object cannot be coerced to type 'double'

My goal is to assign a number to every homeworld. Obviously the lines with the same homeword will have the same number assigned.

How can i fix my code?

test <- data.frame(homeworld = c(4000, 30000, 0),
work = c("relaxwork", "stresswork", "lazy"),
starhours = c(30, 60, 0))

home <- test$homeworld
class(test$homeworld)

#THAT´S NUMERIC, OK!

...........x........x...........

#But if you want to transform to factor:

test$homeworld <- as.factor(as.numeric(test$homeworld))
class(test$homeworld)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.