remove rows from a panel with duplicate index-valu

2019-05-30 08:31发布

问题:

here's some sample data:

date <- c("2012-01-01","2012-01-02","2012-01-02","2012-01-04","2012-01-05","2012-01-03","2012-01-05","2012-01-05","2012-01-01","2012-01-01")
company <- c("A","A","A","A","A","B","B","B","C","C")
var1 <- c(-0.01, -0.013, 0.02, 0.032, -0.002, 0.022, 0.012, 0.031, -0.018, -0.034)
var2 <- c(43, 12, 34, 53, 45, 42, 23, 56, 87, 54)

mydf1 <- data.frame(date, company, var1, var2)
mydf1

#          date company   var1 var2
# 1  2012-01-01       A -0.010   43
# 2  2012-01-02       A -0.013   12
# 3  2012-01-02       A  0.020   34
# 4  2012-01-04       A  0.032   53
# 5  2012-01-05       A -0.002   45
# 6  2012-01-03       B  0.022   42
# 7  2012-01-05       B  0.012   23
# 8  2012-01-05       B  0.031   56
# 9  2012-01-01       C -0.018   87
# 10 2012-01-01       C -0.034   54

i want to run a regression with the plm package, which doesn't seem to work if there are duplicates (i get this error: duplicate couples (time-id) Error in pdim.default(index[[1]], index[[2]])).
that's why i want to keep only the first row, if there are duplicate dates for a company.

the data.frame should look like this:

#          date company   var1 var2
# 1  2012-01-01       A -0.010   43
# 2  2012-01-02       A -0.013   12
# 3  2012-01-04       A  0.032   53
# 4  2012-01-05       A -0.002   45
# 5  2012-01-03       B  0.022   42
# 6  2012-01-05       B  0.012   23
# 7  2012-01-01       C -0.018   87

how can i do this? thank you!

回答1:

mydf1[!duplicated(mydf1[c("date", "company")]),]
##         date company   var1 var2
## 1 2012-01-01       A -0.010   43
## 2 2012-01-02       A -0.013   12
## 4 2012-01-04       A  0.032   53
## 5 2012-01-05       A -0.002   45
## 6 2012-01-03       B  0.022   42
## 7 2012-01-05       B  0.012   23
## 9 2012-01-01       C -0.018   87