按组编号观察

时间:2014-01-15 20:34:48

标签: r

我试图对组内的观察进行编号,然后确定每组中的第一个和最后一个观察结果。我知道如何做到这一点,甚至在这里发布解决方案以回应过去的问题。

但是,我现在偶然发现了我的解决方案不起作用的情况,我无法弄清楚原因。感谢您提供有关如何使下面的第二个示例正常工作的任何建议。第一个例子确实有效。对不起,如果我忽略了一个愚蠢的错字。

我更喜欢使用基础R。

####################################################################

# this works

my.df = read.table(text = '
   state   county   city   miles
       1        1      1       3
       1        1      1       4
       1        1      1       4
       1        1      1       5
       1        1      2       4
       1        1      2       3
       1        2      1       4
       1        2      2       2
       1        2      2       4
       1        2      2       3
       1        2      3       3
       1        2      3       2
', header = TRUE)

my.df
str(my.df)

my.seq <- data.frame(rle(my.df$city)$lengths)
my.seq

my.df$first <- unlist(apply(my.seq, 1, function(x) seq(1,x)))
my.df$last  <- unlist(apply(my.seq, 1, function(x) seq(x,1,-1)))
my.df

my.df2 <- my.df[my.df$first==1 | my.df$last == 1,]
my.df2

####################################################################

# This does not work.  Only the data set has changed.

my.df <- read.table(text = '
   state   county    city    miles
      40        8       1       12
      40        8       1        4
      40        8       2       13
      40        8       2        3
', header = TRUE)

my.df
str(my.df)

my.seq <- data.frame(rle(my.df$city)$lengths)
my.seq

my.df$first <- unlist(apply(my.seq, 1, function(x) seq(1,x)))
my.df$last  <- unlist(apply(my.seq, 1, function(x) seq(x,1,-1)))
my.df

my.df2 <- my.df[my.df$first==1 | my.df$last == 1,]
my.df2

# The expected result with the second example is:

desired.result <- read.table(text = '
   state   county    city    miles   first   last
      40        8       1       12       1      2
      40        8       1        4       2      1
      40        8       2       13       1      2
      40        8       2        3       2      1
', header = TRUE)

####################################################################

2 个答案:

答案 0 :(得分:1)

Ii很难理解你想做什么。

我认为你因为独特城市的特殊情况而得到错误!

这里我将如何做到这一点:

这里的困难是创建分组变量:

xx <- rle(my.df$city)
my.df$group <- rep(seq_along(xx$values),xx$lengths)

然后使用ddply,您将获取每个组的第一个和最后一个:

library(plyr)
res <- ddply(my.df,.(group),function(x){
  y <- rbind(head(x,1),tail(x,1))
  cbind(y,data.frame(first=c(1,nrow(x)),
                     last = c(nrow(x),1)))
})

最后,使用unique删除重复的元素:

unique(res)

 state county city miles group first last
1    40      8    1    12     1     1    2
2    40      8    1     4     1     2    1
3    40      8    2    13     2     1    2
4    40      8    2     3     2     2    1

编辑基础R解决方案,只需将ddply替换为tapply

group <- rep(seq_along(xx$values),xx$lengths)

tapply(my.df,group,function(x){
  y <- rbind(head(x,1),tail(x,1))
  cbind(y,data.frame(first=c(1,nrow(x)),
                     last = c(nrow(x),1)))
})
unique(res)

答案 1 :(得分:0)

我想出了如何修改我的代码,以便我得到两个示例数据集的理想答案。

我只是在我的两个as.vector()语句中添加了unlist(apply())。以下是第二个示例的代码:

my.df <- read.table(text = '
   state   county    city    miles
      40        8       1       12
      40        8       1        4
      40        8       2       13
      40        8       2        3
', header = TRUE)

my.df
str(my.df)

my.seq <- data.frame(rle(my.df$city)$lengths)
my.seq

my.df$first <- as.vector(unlist(apply(my.seq, 1, function(x) seq(1,x))))
my.df$last  <- as.vector(unlist(apply(my.seq, 1, function(x) seq(x,1,-1))))
my.df

my.df2 <- my.df[my.df$first==1 | my.df$last == 1,]
my.df2

结果如下:

  state county city miles first last
1    40      8    1    12     1    2
2    40      8    1     4     2    1
3    40      8    2    13     1    2
4    40      8    2     3     2    1