Great. Then, if one is interested in selecting the second row of a variable
(from a unique id), something like this should work:
aggregate(value ~ id, data = dat, FUN = function(x) head(x, 2)[2])
Thanks, Michael and Dennis!
AC
On Sun, Sep 25, 2011 at 7:10 PM, Dennis Murphy wrote:
> Hi:
>
>
Hi:
The head() function is helpful here:
(i) plyr::ddply()
library('plyr')
ddply(dat, .(id), function(d) head(d, 1))
id value
1 1 5
2 2 4
(ii) aggregate():
aggregate(value ~ id, data = dat, FUN = function(x) head(x, 1))
id value
1 1 5
2 2 4
The formula version of aggreg
I think that's in general a trickier question:
If you are absolutely certain your id's are in blocks and that there are at
least k of them, it would perhaps work to do a manual adjustment of the
indices:something like
# UNTESTED
with(dat, dat[which(!duplicated(id)) + k - 1, ]) # where k is the
wh
dat <- data.frame(id = c(1,1,1,2,2), value = c(5,6,7,4,5), value2 =
c(1,4,3,3,4))
with(dat, dat[!duplicated(id),])
Michael
On Sun, Sep 25, 2011 at 4:43 PM, AC Del Re wrote:
> Great but how can I then retain the other variable (or variables if >1)
> value associated with those retained values?
>
Hi,
I am trying to select the first row of a variable with data in long-format,
e.g.,
# sample data
id <- c(1,1,1,2,2)
value <- c(5,6,7,4,5)
dat <- data.frame(id, value)
dat
How can I select/subset the first 'value' for each unique 'id'?
Thanks,
AC
[[alternative HTML version deleted]
5 matches
Mail list logo