0

How do I change a value in a column for a specific condition by a calculation? So I want to divide by 7 for some of the values in a particular column. It is not possible for me to share data or codes. But my question is almost exactly like this previous question Replace values in multiple columns by values of another column on condition someone answered the question (named camnesia) with this code:

library(dplyr)

df <- df %>%
  mutate(var2 = case_when(var1 %in% c('02','03') ~ '0',
                          TRUE ~ as.character(var2)),
         var3 = case_when(var1 %in% c('02','03') ~ '00',
                          TRUE ~ as.character(var3)),
         var4 = case_when(var1 %in% c('02','03') ~ '000',
                          TRUE ~ as.character(var4)))

I tried to run the code for my data like this:

library(dplyr)

df <- df %>%
  mutate(var1 = case_when(var2 %in% c('A') ~ var1/7,
                          TRUE ~ as.character(var1)))

I want to change the values in var1 that belongs to group 'A' (groups are defined in var2 column), by dividing the values with 7? How do I add that calculation to the above code? Because it does not work like this for me.

Thanks a lot in advance! :)

SAphi11
  • 29
  • 1
  • 7
  • 1
    When using `%>%`, `.` is a stand-in for the entire dataset. So `./7` is attempting to divide all of `df` by 7. You need to instead specify `var1 / 7`. And also ensure consistent output types as per @JanZ’s answer. – zephryl Dec 07 '22 at 13:52
  • 1
    Also note it’s a bit simpler to use `ifelse()` or `dplyr::if_else()` instead of `case_when()` when you have just two conditions. – zephryl Dec 07 '22 at 13:55
  • thanks for answering! :) How can I use if else in my case instead of case_when? I'm really new at R, so I don't know how I should replace case_when at all -.-' I have tried to specify by saying var1 / 7, that did not work for me – SAphi11 Dec 07 '22 at 16:56
  • 1
    It will be hard to help you more without a [reproducible example](https://stackoverflow.com/a/5963610/17303805). I understand you can't share your specific data, so please provide some example made-up data that demonstrates the same problem. Also specify what you mean by "it does not work" -- are you getting an error? What is it? Or are the results just not what you expect? What are they? You can [edit] your question to add this information. – zephryl Dec 07 '22 at 19:17
  • I’m sorry for that. I should have made up some data. I appreciate you telling me this as I’m very new to R and also new at stackoverflow. I will edit by question as soon as I can and add some made up data. But if you still want to know, the “it does not work” means that I’m getting this error “Error in ‘mutate()’: problem while computing ‘the whole code in my question’ caused by error in ‘var1/7’: non-numeric argument to binary operator”. Thanks a lot for trying to help! – SAphi11 Dec 08 '22 at 12:03
  • It’s okay!! Thank you for being so open to feedback. That error implies `var1` isn’t a number. You can check by running `class(df$var1)`. Have you tried @JanZ’s second code block, which includes `as.character(as.double(var1)/7)`? – zephryl Dec 08 '22 at 13:10
  • Thanks a lot! it Worked! :D I tried @JanZ at first and it made an error. I might have typed something wrong at the beginning. But thanks! – SAphi11 Dec 08 '22 at 13:41

1 Answers1

2

You need to have consistent type for column var1. In your code you mix numerical with text. You should encompass the division in as.character() This should work, assuming var1 was numerical:

library(dplyr)

df <- df %>%
  mutate(var1 = case_when(var2 %in% c('A') ~ as.character(var1/7),
                          TRUE ~ as.character(var1)))

If var1 was character at start, then:

library(dplyr)

df <- df %>%
  mutate(var1 = case_when(var2 %in% c('A') ~ as.character(as.double(var1)/7),
                          TRUE ~ as.character(var1)))
Jan Z
  • 171
  • 3
  • Thanks for answering! Unfortunately that did not work for me either. It says that there is something wrong the calculation ./7. I tried to remove the dot and run (/7). But that don't work either. var1 is numerical – SAphi11 Dec 07 '22 at 12:23
  • Could you please provide sample data? – Jan Z Dec 07 '22 at 12:24
  • 1
    My bad, replace the dot `.` by `var1`. – Jan Z Dec 07 '22 at 12:26
  • I'm sorry, I'm not allowed to share my data. I appreciate your help! But that didn't work for me either (replacing the dot by var1) – SAphi11 Dec 07 '22 at 16:57
  • Thanks a lot! It worked for me by running the as.double function :D I might have typed something wrong at start. – SAphi11 Dec 08 '22 at 13:42