0

我有两个向量

 x<-c(1,1,1,2,2,2,3,3,4,5,5,6)
 y<-c("a","b","c","d","e","f","g","h","i","j","k","l")

我只想选择 y 中的那些,它们与 x 中相等值序列中的第一个元素重合所以,在我的情况下,最终解决方案应该是这样的:

    y x
 1  a 1
 4  d 2
 7  g 3
 9  i 4
 10 j 5
 12 l 6

我们编写了一个脚本,它可以工作,但需要添加和删除额外的行(以便稍后使用 cbind),这对我来说是一团糟。

aaa<-data.frame(y,x)
df<-NULL
for (i in 2:length(aaa$x)){   # you may see it stars from the second element because 
                               # of x[i-1]
   bbb<-ifelse((aaa$x[i]!= aaa$x[i-1]), aaa$x[i], NA)
     df<-rbind(df,bbb)
}
df
df<-rbind(1,df)
aaa$x<-df[,1]
bbb<-na.omit(aaa)
bbb

我已经尝试应用 rle() ,因为它是我之前推荐的如何只选择非中断号码? 但在这种情况下失败了。

很想听听你的建议,

谢谢你。

4

2 回答 2

3

这是一个简单的解决方案:

aaa <- data.frame(y, x)
aaa[!duplicated(aaa$x), ]

#    y x
# 1  a 1
# 4  d 2
# 7  g 3
# 9  i 4
# 10 j 5
# 12 l 6
于 2012-10-26T13:44:25.903 回答
1

也许是这样的:

justFirst <- function(x, y){
    stopifnot(length(x) > 1 && length(y) > 1 && length(x) == length(y))
    newX <- newY <- vector()
    for (i in 1:length(x)){
        if (i == 1){
            newX <- c(newX, x[i])
            newY <- c(newY, y[i])
        }
        else{
            if (x[i] != x[i-1]){
                newX <- c(newX, x[i])
                newY <- c(newY, y[i])
            }
        }
    }
    return(data.frame(newX, newY))
}

x<-c(1,1,1,2,2,2,3,3,4,5,5,6)
y<-c("a","b","c","d","e","f","g","h","i","j","k","l")

justFirst(x, y)

我把它放在这里作为替代方案,以防出现 x 和 y 看起来像这样(无序且混乱)的情况:

x<-c(1,1,1,2,1,2,3,3,4,5,5,6,7,4,4,4,4,3,2,3)
y<-c("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t")

但是,也许还有更好的方法来处理这种情况......

于 2012-10-26T13:57:41.227 回答