Integer recoding

Hi,
I'm new to R and I have a question about recoding variables from taken from our SQL database.
My starting point is preparing a file:

library(RODBC)
abc <- odbcConnect("sqldatabase")

my.data <- sqlQuery(abc, "SELECT * FROM sqldatatable")
my.local.data <- collect(my.data)
Belgium.data <- my.data[my.data$CountryID == 15, ]
Belgium.CurrentHY.data <- subset(Belgium.data, InterviewDate > "2018-04-01" & InterviewDate < "2018-09-30")

I can display it without any problems:

str(Belgium.CurrentHY.data)
$ CountryID      : int  15 15 15 15 15 15 15 15 15 15 ... $ InterviewDate  : POSIXct, format: "2018-04-25 08:12:00" "2018-04-26 13:05:00" "2018-04-04 17:28:00" "2018-04-10 12:12:00" ...
$ A2 : int 9 10 10 8 10 9 10 10 9 10 ...
$ B1 : int 10 10 8 7 10 8 9 10 8 10 ...
$ C1 : int 10 10 9 8 10 9 10 9 9 9 ...

so Belgium.CurrentHY.data exists

Now, I would like to recode variable A2 (values from 1 to 10) into A2TB (values 9-10 as 1 and values 1-8 as 2).
How can I do that?

One way is with dplyr::recode().

a1 <- as.integer(c(9, 10, 10, 8, 10, 9, 10, 10, 9, 10))
dplyr::recode(a1, `9` = 1L, `10` = 1L)
#>  [1] 1 1 1 8 1 1 1 1 1 1

Created on 2018-11-06 by the reprex package (v0.2.1.9000)

Note a couple of R idioms here: in order to use a number as a name, you have to surround it with backticks (``), to ensure that a numeral is parsed as an integer, you can add L afterward (see details here).

There are several ways to do this in base R, but I’d probably use ifelse():

Belgium.CurrentHY.data$A2B <- ifelse(Belgium.CurrentHY.data$A2 < 9, 1, 2)

(dplyr also offers an if_else() function, which works similarly but is more type safe, making it stricter/less flexible but more predictable — it won’t let you try to partially replace your integer values with character values, for example)

1 Like

Yeah, use this! Sorry, I somehow missed the 1-8 part! (>ლ)

Either way, lots of options for recoding integers, several are in this SO thread:

1 Like

Excellent! Thank you very much :slight_smile:

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