0

im trying to recode a lot of variables (more than 53) in a loop, but it won't work. can someone please tell me, where my mistake is?

i give you a small exaple

data <-  read.csv("test.csv", header = TRUE, ";", na = -77)

data$var1 <- recode(data$var1, "1=0; 2=1; 3=2; 4=3; NA=NA")

here i have var 1-59 and several items that must be recoded in a different way.

i tried

for (i in 1:59){
get(paste0(data$var",i)) <- recode(paste0("data$var",i), "1=0; 2=1; 3=2; 4=3; NA=NA"
}

and

for (i in c(65, 73, 99){
get(paste0(data$var",i)) <- recode(paste0("data$var",i), "1=0; 2=0; 3=0; 4=1; NA=NA"
}

The Code will not work. Wheres my mistake? Can someone please give me a hind?

Thank you very much :) derlu

derlu
  • 25
  • 8

2 Answers2

1

You can use switch function to recode values and data.table to recode values in all columns in one go:

library(data.table)    

# function to recode values
myfun <- function(val){
    if(is.na(val)) return (NA)
    else switch(val, '1'= '0','2' = '1', '3'='2','4'='3')
}

# apply the function to the selected columns
col_names <- paste0('var', 1:59)
df[,(col_names) := lapply(.SD, function(x) unlist(sapply(x, myfun)) ), .SDcols = col_names]

print(df)
YOLO
  • 20,181
  • 5
  • 20
  • 40
  • me understand, Where the Profit is towards the example above? The Code seems much more complicated :) – derlu Jul 21 '18 at 09:58
  • Sorry. Advantage. – derlu Jul 21 '18 at 10:13
  • The advantage is this code is fast and it avoids using nested for loop. It is easier to learn: https://github.com/Rdatatable/data.table/wiki/Getting-started – YOLO Jul 21 '18 at 10:15
0

How about a more readable tidyverse solution?

library(dplyr)
library(magrittr)

data %<>% 
  mutate_at(c(1:59)    , recode, '1'='0', '2'='1', '3'='2', '4'='3') %>%
  mutate_at(c(65,73,99), recode, '1'='0', '2'='0', '3'='0', '4'='1')

If you prefer, you can also use character vectors as the first argument that you pass to mutate_at. Like so:

data %<>% 
  mutate_at(paste0('var', c(1:59))    , recode, '1'='0', '2'='1', '3'='2', '4'='3') %>%
  mutate_at(paste0('var', c(65,73,99)), recode, '1'='0', '2'='0', '3'='0', '4'='1')

The third option (not applicable in this case because there are too many columns) is to use vars:

data %>% 
  mutate_at(vars(var65, var73, var99), recode, '1'='0', '2'='0', '3'='0', '4'='1')
Vlad C.
  • 944
  • 7
  • 12
  • Seems very Interesting. But where do i define the Variable that Must be recoded? Cant See It in the Code. – derlu Jul 21 '18 at 11:36
  • Sorry, my bad. I just edited the answer to include referring to the variables by name, not only by position. – Vlad C. Jul 21 '18 at 12:08
  • This way i recode the 1st to 59th item of my dataset (that includes 397 items). But i want to recode the items var1:var59 – derlu Jul 21 '18 at 12:09
  • Thank you. This is what i needed. Cool. – derlu Jul 21 '18 at 12:09
  • it works fine for some items. for other get the message: _Error in UseMethod("recode") : no applicable method for 'recode' applied to an object of class "logical"_ – derlu Jul 21 '18 at 19:15
  • Is It an issue with the missing values or the not recoded values (value 5 should not be recoded) – derlu Jul 21 '18 at 21:44
  • Yes, that issue happens sometimes when a column only contains NAs in and therefore defaults to logical. Is your problem solved now? – Vlad C. Jul 21 '18 at 23:36
  • Im using It in a shiny Environment. Therefore i don’t get Outputs – derlu Jul 21 '18 at 23:40
  • Are there any NA-only columns in your dataset and if so, do you still want to include them? Did you try coercing them to factors? – Vlad C. Jul 21 '18 at 23:46
  • Yes there are NA-only columns in the dataset. I have to include them. – derlu Jul 21 '18 at 23:48
  • What do you mean with coercing? No i havnt – derlu Jul 21 '18 at 23:49
  • I mean to convert them to factors, for example by running `mutate_at(c(1:59, 65,73,99), as.factor)` before the other `mutate_at` statements – Vlad C. Jul 22 '18 at 05:07
  • I will try It and give you an Update – derlu Jul 22 '18 at 18:12
  • with converting them to factors resolves the problem. but now i cant do my normal statistical computations – derlu Jul 22 '18 at 20:10