Something like this?
pdf[!ave(pdf$obs, pdf$id, FUN = function(x) length(unique(x)) != 1), ]
# id time obs
# 2-1 2 1 1
# 2-2 2 2 1
# 2-3 2 3 1
# 4-1 4 1 0
# 4-2 4 2 0
# 4-3 4 3 0
Domanda
Suppose I have a pdata.frame (using the plm package in r) like so:
> df <- data.frame(id=c(1,1,1,2,2,2,3,3,3,4,4,4), time=rep(1:3,4),obs=c(1,2,3,1,1,1,1,1,2,0,0,0))
> pdf <- pdata.frame(df)
> pdf
id time obs
1-1 1 1 1
1-2 1 2 2
1-3 1 3 3
2-1 2 1 1
2-2 2 2 1
2-3 2 3 1
3-1 3 1 1
3-2 3 2 1
3-3 3 3 2
4-1 4 1 0
4-2 4 2 0
4-3 4 3 0
I have an observation which does not vary with time in some of the id
indices, but does in others. I would like to find the ones that do not vary with time, i.e. I want to extract something that looks like
id time obs
2-1 2 1 1
2-2 2 2 1
2-3 2 3 1
4-1 4 1 0
4-2 4 2 0
4-3 4 3 0
is there an easy way to do this?
Soluzione
Something like this?
pdf[!ave(pdf$obs, pdf$id, FUN = function(x) length(unique(x)) != 1), ]
# id time obs
# 2-1 2 1 1
# 2-2 2 2 1
# 2-3 2 3 1
# 4-1 4 1 0
# 4-2 4 2 0
# 4-3 4 3 0