2017-08-05 188 views
0
中间自动更新

请看看这个简单的数据帧:循环索引在迭代

1 4 a 2 5 b 3 6 c 4 7 d 5 8 e 6 9 f 7 10 g

忽略的第三列现在。我想创建一个带有二进制变量的第四列,表示特定行是前一行的延续。

让我说明,

1 4 a 1 2 5 b 0 3 6 c 0 4 7 d 1 5 8 e 0 6 9 f 0 7 10 g 1

第一行,我们开始用“1变为4”,这是我们的基础,路径的起点。 在下一行我们有“2到5”,但由于我们已经在4,所以2号不是一个延续,所以第4列得到一个“0”。这一行被我们的基地放弃了。 在下一行“3到6”,由于我们已经在4,我们应该重新开始,因此我们应该从4开始重新启动。

这就是接下来发生的情况......“4转到7”连接第一行并因为第四列获得“1”。 同样,只有行“7去10”重新连接路径,并获得“1”最终我想筛选“1”,因为字母列有重要的信息。但dplyr很容易。忘掉它。

我的失败方法是这样的:我创建了一个循环与i和j,这将搜索时,列2中的值将等于列1中的值,进一步下降。问题是例如行“2去5”也连接行“5去8”,并会收到一个“1”

我看不到如何指示循环找到第一个连接,“4走7”,使用该发现作为新的基地。只有这样,下一个积极的回报是以7开头的行。

我的头疼。我希望有一个头脑冷静的人能够向我展示光明。

谢谢你们。

回答

0

使用R中一个简单的循环将是另一种尝试:

x <- 1:7 
y <- 4:10 

largest_nr <- min(x, y) # to get a 1 in the first entry 
res_vec <- c() # empty vector 

# loop through the numbers and check if we have a new largest number 
for (i in 1:length(x)) { 
if (min(x[i], y[i]) >= largest_nr) { 
    # new largest number found 
    largest_nr <- max(x[i], y[i]) 
    res_vec <- c(res_vec, 1) 
} else { 
    # no new largest number found 
    res_vec <- c(res_vec, 0) 
} 
} 

cbind(x, y, res_vec) 
#>  x y res_vec 
#> [1,] 1 4  1 
#> [2,] 2 5  0 
#> [3,] 3 6  0 
#> [4,] 4 7  1 
#> [5,] 5 8  0 
#> [6,] 6 9  0 
#> [7,] 7 10  1 
0

首先你的数据集例如:

dat <- 
structure(list(X = 1:7, Y = 4:10, Z = c("a", "b", "c", "d", "e", 
"f", "g")), .Names = c("X", "Y", "Z"), class = "data.frame", row.names = c(NA, 
-7L)) 

现在尝试以下。

next_one <- function(dat, curr){ 
    # Get the connect point from the second column 
    i <- dat[curr, 2] 
    # Now a vector of potential continuations 
    i <- which(dat[, 1] >= i) 
    # If there's a continuation, it's the first, 
    # else we're at the end of the column 
    i <- if(length(i) >= 1) i[1] else 0L 
    i 
} 

W <- integer(nrow(dat)) 
W[1] <- 1L 
curr <- 1 

while(curr <= nrow(dat)){ 
    i <- next_one(dat, curr) 
    if(i){ 
     W[i] <- 1L 
     curr <- i 
    }else 
     break 
} 

new_dat <- cbind(dat, W) 
new_dat 
    X Y Z W 
1 1 4 a 1 
2 2 5 b 0 
3 3 6 c 0 
4 4 7 d 1 
5 5 8 e 0 
6 6 9 f 0 
7 7 10 g 1 
+0

大卫的回答是有点更简单,使之更灵活的测试原始数据。尽管如此,我从长远来看也是如此,所以我很感谢你的解决方案。我会仔细研究你的代码,以便表示对你的努力的赞赏,但是也为我的知识增加一个技巧。也许你可以解释“next_one”函数是如何工作的。它似乎比基础教程更先进。顺便说一句,我不能upvote你的答案,因为我没有足够的声望点。 –

+0

@RandomLuck不要担心选票,他们不会打动我的孙子。至于关于函数如何工作的解释,我将编辑我的代码并包含注释。 –