Search code examples
rdatabasecharacterpanel

Convert from character to numeric


I'm trying to convert a column of my dataset from character to numeric. That's my attempt

# Generate 'key' as the combo of 'countrycode' and 'year'
new_data$key = paste(new_data$countrycode, sprintf("%02.0f", new_data$year), sep = "")

Here, as per the description above, I created for my panel dataset a new variable, key, as the combination of countrycode (e.g. USA) and year (e.g.2010), and the outcome is USA2010. Thus, the new variable is a character. I'd like to have this variable as numeric and still keep the label as if it was a character. I tried with:

# Generate 'key_id'
new_data$key_id = as.numeric(factor(new_data$key))

However, key_id is just a sequence of numbers. For instance, the corresponding key_id for USA2010 is 10500 instead of USA2010.

I tried with

names(new_data)[names(new_data) == "key"] <- "key_id"

but it does not work


Solution

  • I think you just want new_data$key_id = as.factor(new_data$key)

    Factors in R are encoded as numeric but have associated "levels" which will be the original strings (that's probably an inaccurate description, it's just how I think of them - see here for a more reliable explanation).