0

I am working with panel data, where my two dimentsions are exporter_importer and year. I was wondering if anyone is able to translate the following code from Stata to R?

bys exporter_importer (year):egen event_year = min(cond(event,year,.))
Helix123
3,7322 gold badges19 silver badges42 bronze badges
asked Apr 3, 2021 at 15:05
2
  • 3
    Welcome to Stackover flow. It would probably help if you were explicit about what your expected output for this STATA code is - not all users of R will be familiar with STATA code. It would also help if you included a minimal dataset. These links may help to make the most of asking a question on SO How to Ask and minimal reproducible example Commented Apr 3, 2021 at 15:21
  • You're often much better off explaining what you want to calculate in R than in showing other code, here Stata code. The number of people who are very fluent in R much exceeds the number of people very fluent in R and Stata. No disrespect to Stata, as my profile implies, but a direct R question is far more likely to get a good answer quickly. So show what your data look like in R and then explain in words what you want to calculate. But at least try some R code. Questions with no attempt at code often are ignored, to say no more. Commented Apr 4, 2021 at 9:44

1 Answer 1

1

Edit 2: Significantly improved version thanks to Mike and Nick Cox (and remembering Peters comment to share data and desired output):

library(tidyverse)
df %>% arrange(exporter_importer, year) %>% 
 group_by(exporter_importer, year) %>%
 mutate(event_year = ifelse(event==1,min(year),NA))
 ungroup() # optional

Original answer:

Not a Stata expert (especially not knowing about "cond") but my guess is that you are maybe looking for something along those lines:

df %>% 
library(dplyr)
group_by(exporter_importer, year) %>%
mutate(event_year = min((if.else(event,year,.)) %>%
ungroup()
answered Apr 3, 2021 at 18:08
Sign up to request clarification or add additional context in comments.

4 Comments

@Maganius, I completely agree that it is difficult to figure out what is being asked here, especially because min is being combined with cond. Btw, cond in Stata is essentially the R equivalent of if_else but with more functionality. This journal article provides more information.
@Mike is correct. min(cond(event, year, .)) finds the minimum (i.e. first) year for which a variable event is true. In Stata, true means numeric and not zero: conventionally, but not necessarily, event would be coded 1 if true and 0 if false. So the code says, informally translated: for distinct exporter, importer pairs, return the first year in which there was an event. The bare period (stop, dot) is Stata's system missing, which would be ignored by min() to the extent possible.
I have to agree that this is a tough question without an explicit data example.
@Magasinus, as @Nick Cox's very helpful response indicates, . is the equivalent of NA in R. Additionally, @Nick Cox's suggestion of coding event as a dummy variable helps us come up with a workable answer. Because you are new to the forum, I want you to get credit. Accordingly, kindly add library(tidyverse) above your code, and I'd suggest that you update your answer as follows: df %>% arrange(exporter_importer, year) %>% group_by(exporter_importer, year) %>% mutate(event_year = ifelse(event==1,min(year),NA))

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.