Within a dataframe (call it data
), I have a variable (call it var
) that takes on values such as "John Smith", "Adam Olson", "Peter Bradley", etc.
sapply(data, mode)
indicates var
is numeric. And when I do as.numeric(var)
, R gives out numbers (1, 2, 3,... as expected).
When I split the variable into two by using stringr::str_split_fixed
(as explained in http://rbyexamples.blogspot.com/2015/07/r-for-stata-users-part-3.html Task #14), and I call the variables firstname
and lastname
, R tells me that the variables are character. Hence, I can't use as.numeric
If I read How to convert a data frame column to numeric type? correctly, transform
won't work. Thus, given the way I've split var
, there's no way to convert the variable into a numeric.
Is there a way of splitting the variable such that it can be converted into numeric more easily?