Therefore, I am currently facing a problem in R that I know exactly how to deal with in Stata, but wasted more than two hours to execute in R.
Using the data.frame below, I want to get exactly one first observation for each group, while the groups are formed by several variables and should be sorted by another variable, that is, mydata data.frame obtained by:
id <- c(1,1,1,1,2,2,3,3,4,4,4)
day <- c(1,1,2,3,1,2,2,3,1,2,3)
value <- c(12,10,15,20,40,30,22,24,11,11,12)
mydata <- data.frame(id, day, value)
Must be converted to:
id day value
1 1 10
1 2 15
1 3 20
2 1 40
2 2 30
3 2 22
3 3 24
4 1 11
4 2 11
4 3 12
( row[1]: (id,day)=(1,1)), (, ).
Stata :
bys id day (value): keep if _n == 1
, , :
mydata$id1 <- paste(mydata$id,"000",mydata$day, sep="")
myid.uni <- unique(mydata$id1)
a<-length(myid.uni)
last <- c()
for (i in 1:a) {
temp<-subset(mydata, id1==myid.uni[i])
if (dim(temp)[1] > 1) {
last.temp<-temp[dim(temp)[1],]
}
else {
last.temp<-temp
}
last<-rbind(last, last.temp)
}
last
, :
1. ( ).
2. , Stata.
3. ( 100 000 , 6), 1,5 .
Stata bys var1 var2: keep if _n == 1?