-1

here is a minimal example of my code:

infile    <- read.table("testdaten_studie2.csv", header=TRUE, stringsAsFactors = FALSE, sep=";",dec=",", na = -77)
infile    <- subset(infile, Chiffre == "LP030482")

bdi.sub   <- subset(infile, select = c(Base_BDI_v1:Base_BDI_v21))
bdi.mean  <- apply(bdi.sub,1,mean,na.rm = TRUE)

bdi.sub %<>% 
  mutate_at(paste0('Base_BDI_v', c(1:21)), recode, '1'='0', '2'='1', '3'='2', '4'='3', "NA"="NA")

If i run it this way, the bdi.sub returns NA. When i try:

bdi.sub$Base_BDI_v1 <- recode(bdi.sub$Base_BDI_v1, "1=0; 2=1; 3=2; 4=3; NA=NA")
bdi.sub$Base_BDI_v2 <- recode(bdi.sub$Base_BDI_v2, "1=0; 2=1; 3=2; 4=3; NA=NA")
bdi.sub$Base_BDI_v3 <- recode(bdi.sub$Base_BDI_v3, "1=0; 2=1; 3=2; 4=3; NA=NA")
bdi.sub$Base_BDI_v4 <- recode(bdi.sub$Base_BDI_v4, "1=0; 2=1; 3=2; 4=3; NA=NA")
bdi.sub$Base_BDI_v5 <- recode(bdi.sub$Base_BDI_v5, "1=0; 2=1; 3=2; 4=3; NA=NA")
bdi.sub$Base_BDI_v6 <- recode(bdi.sub$Base_BDI_v6, "1=0; 2=1; 3=2; 4=3; NA=NA")
bdi.sub$Base_BDI_v7 <- recode(bdi.sub$Base_BDI_v7, "1=0; 2=1; 3=2; 4=3; NA=NA")
bdi.sub$Base_BDI_v8 <- recode(bdi.sub$Base_BDI_v8, "1=0; 2=1; 3=2; 4=3; NA=NA")
bdi.sub$Base_BDI_v9 <- recode(bdi.sub$Base_BDI_v9, "1=0; 2=1; 3=2; 4=3; NA=NA")
bdi.sub$Base_BDI_v10 <- recode(bdi.sub$Base_BDI_v10, "1=0; 2=1; 3=2; 4=3; NA=NA")
bdi.sub$Base_BDI_v11 <- recode(bdi.sub$Base_BDI_v10, "1=0; 2=1; 3=2; 4=3; NA=NA")
bdi.sub$Base_BDI_v12 <- recode(bdi.sub$Base_BDI_v12, "1=0; 2=1; 3=2; 4=3; NA=NA")
bdi.sub$Base_BDI_v13 <- recode(bdi.sub$Base_BDI_v13, "1=0; 2=1; 3=2; 4=3; NA=NA")
bdi.sub$Base_BDI_v14 <- recode(bdi.sub$Base_BDI_v14, "1=0; 2=1; 3=2; 4=3; NA=NA")
bdi.sub$Base_BDI_v15 <- recode(bdi.sub$Base_BDI_v15, "1=0; 2=1; 3=2; 4=3; NA=NA")
bdi.sub$Base_BDI_v16 <- recode(bdi.sub$Base_BDI_v16, "1=0; 2=1; 3=2; 4=3; NA=NA")
bdi.sub$Base_BDI_v17 <- recode(bdi.sub$Base_BDI_v17, "1=0; 2=1; 3=2; 4=3; NA=NA")
bdi.sub$Base_BDI_v18 <- recode(bdi.sub$Base_BDI_v18, "1=0; 2=1; 3=2; 4=3; NA=NA")
bdi.sub$Base_BDI_v19 <- recode(bdi.sub$Base_BDI_v19, "1=0; 2=1; 3=2; 4=3; NA=NA")
bdi.sub$Base_BDI_v20 <- recode(bdi.sub$Base_BDI_v20, "1=0; 2=1; 3=2; 4=3; NA=NA")
bdi.sub$Base_BDI_v21 <- recode(bdi.sub$Base_BDI_v21, "1=0; 2=1; 3=2; 4=3; NA=NA")

heres the bdi.sub dataset:

structure(list(Base_BDI_v1 = c(NA, 1L, 4L, 4L, NA, 1L), Base_BDI_v2 = c(NA, 
1L, 3L, 1L, NA, 1L), Base_BDI_v3 = c(NA, 1L, 3L, 1L, NA, 4L), 
    Base_BDI_v4 = c(NA, 1L, 2L, 4L, NA, 3L), Base_BDI_v5 = c(NA, 
    1L, 3L, 4L, NA, 4L), Base_BDI_v6 = c(NA, 1L, 3L, 1L, NA, 
    4L), Base_BDI_v7 = c(NA, 1L, 4L, 4L, NA, 2L), Base_BDI_v8 = c(NA, 
    1L, 3L, 4L, NA, 2L), Base_BDI_v9 = c(NA, 1L, 1L, 1L, NA, 
    4L), Base_BDI_v10 = c(NA, 0L, 4L, 3L, NA, 3L), Base_BDI_v11 = c(NA, 
    0L, 4L, 4L, NA, 3L), Base_BDI_v12 = c(NA, 0L, 2L, 1L, NA, 
    3L), Base_BDI_v13 = c(NA, 0L, 1L, 2L, NA, 2L), Base_BDI_v14 = c(NA, 
    0L, 2L, 4L, NA, 1L), Base_BDI_v15 = c(NA, 2L, 3L, 1L, NA, 
    3L), Base_BDI_v16 = c(NA, 3L, 7L, 4L, NA, 6L), Base_BDI_v17 = c(NA, 
    2L, 3L, 4L, NA, 1L), Base_BDI_v18 = c(NA, 1L, 1L, 6L, NA, 
    6L), Base_BDI_v19 = c(NA, 1L, 3L, 3L, NA, 1L), Base_BDI_v20 = c(NA, 
    2L, 3L, 3L, NA, 3L), Base_BDI_v21 = c(NA, 1L, 3L, 1L, NA, 
    4L)), row.names = c(NA, 6L), class = "data.frame")

i can create the mean.



I’ve got a problem recoding several variables. Using

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')

Results in:

Error on useMethod (recode): no applicable Method for ‚recode’ applied to an object of class „logical“

There are a lot of NAs in the dataset that need to remain. Where’s my mistake?

derlu
  • 25
  • 8
  • It sounds like the problem is that one or more of those columns are logical values. Are you sure you have the column numbers right? When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jul 31 '18 at 21:25
  • `names(data)[sapply(data,is.logical)]` will give you the name of your logical columns – moodymudskipper Jul 31 '18 at 21:26
  • All columns of the variables are numeric except the NAs – derlu Jul 31 '18 at 21:27
  • 1
    usual request... give us data! in your case, `dput(head(your_data))` would help because it would also show the structure of your data – tjebo Jul 31 '18 at 21:55
  • That last comment is especially important since you said "All columns of the variables are numeric except the NAs". There are different kinds of NA's, so you comment suggests that you think there is an entire column of NA's, which therefore could be NA_logical. – IRTFM Aug 01 '18 at 01:23
  • i updated my question. – derlu Aug 01 '18 at 10:57
  • after your update I am confused, haha! Your code doesn't fit to your sample data, but when you change it - it works for me. – tjebo Aug 01 '18 at 22:24

2 Answers2

0

Had this as a comment, but realised it could probably serve as an answer too.

I guess the crux is here that you are creating a logical vector with your mutate_at call, with the selection using paste. This logical vector is passed as an argument to recode, but is of course not accepted, because it needs a non-logical vector as argument

See ?recode:

For logical vectors, use if_else()

I think you could simply replace recode with case_when and then it should work.

edit
after having got some of your data - your code actually works for me (after adapting it to your data)

data1 %>%  #my name for your data
  mutate_at(paste0('Base_BDI_v', 1:10),   #randomly chosen columns
            recode, '1'='0', '2'='1', '3'='2', '4'='3', 'NA' = 'NA') %>%
  mutate_at(paste0('Base_BDI_v', c(12,14,16)),
            recode, '1'='0', '2'='0', '3'='0', '4'='1', 'NA' = 'NA')

Be aware that you are getting character type columns with this approach!

str(d)
'data.frame':   6 obs. of  21 variables:
 $ Base_BDI_v1 : chr  NA "0" "0" "0" ...
 $ Base_BDI_v2 : chr  NA "0" "0" "0" ...
 $ Base_BDI_v3 : chr  NA "0" "0" "0" ...
 $ Base_BDI_v4 : chr  NA "0" "0" "0" ...
 $ Base_BDI_v5 : chr  NA "0" "0" "0" ...
 $ Base_BDI_v6 : chr  NA "0" "0" "0" ...
 $ Base_BDI_v7 : chr  NA "0" "0" "0" ...
 $ Base_BDI_v8 : chr  NA "0" "0" "0" ...
 $ Base_BDI_v9 : chr  NA "0" "0" "0" ...
 $ Base_BDI_v10: chr  NA NA "0" "0" ...
 $ Base_BDI_v11: int  NA 0 4 4 NA 3
 $ Base_BDI_v12: chr  NA NA "0" "0" ...
 $ Base_BDI_v13: int  NA 0 1 2 NA 2
 $ Base_BDI_v14: chr  NA NA "0" "0" ...
 $ Base_BDI_v15: int  NA 2 3 1 NA 3
 $ Base_BDI_v16: chr  NA "0" NA "0" ...
 $ Base_BDI_v17: int  NA 2 3 4 NA 1
 $ Base_BDI_v18: int  NA 1 1 6 NA 6
 $ Base_BDI_v19: int  NA 1 3 3 NA 1
 $ Base_BDI_v20: int  NA 2 3 3 NA 3
 $ Base_BDI_v21: int  NA 1 3 1 NA 4
tjebo
  • 21,977
  • 7
  • 58
  • 94
  • It’s amazing. It’s works fine. But when I use it inside a shiny environment it won’t work anymore. – derlu Aug 02 '18 at 21:22
  • this might be worth a new question. I recommend to make the data frame even simpler: Cut it down to 2 or 3 rows and 4 columns, ideally with simple placeholder name instead of your long name. and make the two mutate calls on only 2 cols each. and include a link to this question – tjebo Aug 02 '18 at 22:28
0

I would recommend just adding an extra line to your code.

data %>% 
    mutate_if(is.logical, as.character) %>%
    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')

Although I do not have you data, this worked for me on some tests.

If you are concerned about changing all the logical types to character, you could do Tjebo suggested and use case_when

data %>% 
    mutate_at(paste0('var', c(1:59)), funs(case_when(. == '1'~'0', . == '2'~'1', . == '3'~'2', . == '4'~'3'))) %>%
    mutate_at(paste0('var', c(65,73,99)), funs(case_when(. == '1'~'0', . == '2'~'0', . == '3'~'0', . == '4'~'1')))
AndS.
  • 7,748
  • 2
  • 12
  • 17