0

I have a column imported from an excel file (colA) and I want to convert the strings of that column to integers. For example a colA: ZC ZC AN ZC AN .... How can I convert "ZC" to 1 and "AN" to 2. I need to do it with something that iterates through the list because it's a 100K-row column.

Gabriel
  • 9
  • 2
  • 1
    You want them to be numbered in the order they first appear? `your_data$colA = as.integer(factor(your_data$colA, levels = unique(your_data$colA)))` – Gregor Thomas Apr 26 '21 at 01:56
  • @GregorThomas No, the numbers are the "values" of the strings. Nothing to do with the order they appear. There are more strings in the column, like "IR" = 6 and "UK" = 0. I just want to itterate through every cell of the column and use its numerical value later. Convert a column with strings to a column with their respective values. I hope that's more clear now. – Gabriel Apr 26 '21 at 02:07
  • 1
    So, how do you know that `ZC` is 1, `AN` is 2, `IR` is 6, and `UK` is 0? Do you have another table with the mapping between strings and values? [You might be interested in this recent question of mine](https://stackoverflow.com/questions/67081496/canonical-tidyverse-method-to-update-some-values-of-a-vector-from-a-look-up-tabl) which has quite a few methods for updating a column based on another table. – Gregor Thomas Apr 26 '21 at 02:10

0 Answers0