2

I'm trying to replicate some Stata code in r. In my df, there is a variable "time" and one "exposure" (both numeric, so with values like 1,2,3 etc.

This is what the original Stata code looks like:

replace time = (time - 31) if exposure == 0

And this is what I tried:

recoded_df <- recoded_df %>% mutate(time = case_when(exposure == 0 ~ (time - 31))

I get the error that there is an "unexpected symbol" in the code. But I have no idea what I am doing wrong.

9
  • Could you please share a reproducible piece of your data by means of dput(head(data))? It's essential if you are to have any chances of getting relevant solutions. Commented May 25, 2021 at 15:47
  • 2
    Without sample data to test, your code looks right except that you are missing a ) at the end. You probably also want to set a default of "no changes", mutate(time = case_when(exposure == 0 ~ (time - 31), TRUE ~ time)) Commented May 25, 2021 at 15:52
  • In addition to sample data, I can't figure out how to get that error from this code: without the last right-paren, R just waits for me to close out the expression, no error. My guess is that you have something else, perhaps mutate(time = case_when(...) time2 = foo(.)), where there needs to be a comma (just a guess, though). Commented May 25, 2021 at 15:55
  • @GregorThomas, I tend to prefer that methodology as well, explicitly adding a default value, but case_when does default to NA which might be what the OP is intending. (idk) Commented May 25, 2021 at 15:56
  • 1
    And @r2evans my assumption is the Stata code shared would not make the non-transformed values NA. Commented May 25, 2021 at 15:57

4 Answers 4

2
library(dplyr)

df <- data.frame(
  exposure = c(0, 1),
  time = c(81, 20)
)

df %>%
  mutate(
    time = case_when(exposure == 0 ~ time - 30,
                     TRUE ~ time)
  )

  exposure time
1        0   51
2        1   20
Sign up to request clarification or add additional context in comments.

Comments

1

Using data.table, fifelse

library(data.table)
setDT(df)[, time := fifelse(!exposure, time - 30,  time)]

Comments

0

Here are two possibilities how you can achieve your task with a fake dataframe:

# fake dataframe:
df <- tribble(
  ~time, ~exposure,
  100, 0,
 70, 1, 
  90, 0, 
  60, 1, 
  50, 0
)

# 1. with mutate and ifelse
library(dplyr)
df %>% 
  mutate(time = ifelse(exposure == 0, time-31, time))

# 2. with base R and ifelse
df$time <- ifelse(df$exposure == 0, df$time - 31, df$time) 

Comments

0

If I understand correctly, ifelse from base R should cover this:

recoded_df$time = ifelse(recoded_df$exposure == 0,
                         recoded_df$time - 31, recoded_df$time)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.