Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中逻辑数组的最内层真值_R - Fatal编程技术网

R中逻辑数组的最内层真值

R中逻辑数组的最内层真值,r,R,如果我有一个逻辑数组 x = c(TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE) 在上面的例子中,获取最内部真实值的最简单方法是在索引2和索引6,我不确定您的问题是否明确,但在这种特定情况下,rle为您提供了所需的: > rle(x)$lengths[1] [1] 2 > rle(x)$lengths[1]+rle(x)$lengths[2]+1 [1] 6 我不确定您的问题是否定义明确,但在这种特定情况下,rle为您提供了所

如果我有一个逻辑数组

x = c(TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE)

在上面的例子中,获取最内部真实值的最简单方法是在索引2和索引6,我不确定您的问题是否明确,但在这种特定情况下,rle为您提供了所需的:

> rle(x)$lengths[1]
[1] 2
> rle(x)$lengths[1]+rle(x)$lengths[2]+1
[1] 6

我不确定您的问题是否定义明确,但在这种特定情况下,rle为您提供了所需的:

> rle(x)$lengths[1]
[1] 2
> rle(x)$lengths[1]+rle(x)$lengths[2]+1
[1] 6

这可能更稳健?如果真与假切换超过两次,rle将无法工作

    # you could try it on the original vector..
    # x <- c(TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE)

    # ..but it also works on a more scattered vector
    x <- c(TRUE, FALSE , TRUE, FALSE, FALSE, FALSE, FALSE , TRUE, TRUE, TRUE)

    # find the position of all TRUEs
    true.positions <- which( x )

    # find the midpoint of the vector
    midpoint <- length( x ) / 2

    # find the smallest distance from the midpoint,
    small.dist <- ( true.positions - midpoint )

    # both above and below
    small.dist.above <- min( small.dist[ small.dist >= 0 ] )
    small.dist.below <- abs( max( small.dist[ small.dist <= 0 ] ) )

    # find the lowest position above the midpoint
    lpa <- which( small.dist.above == true.positions - midpoint )
    # find the highest position below the midpoint
    hpb <- which( small.dist.below == midpoint - true.positions )

    # if both are the midpoint, combine them
    closest.trues <- unique( c( hpb , lpa ) )

    # return the position in the original vector x
    # of the innermost TRUE
    true.positions[ closest.trues ]

这可能更稳健?如果真与假切换超过两次,rle将无法工作

    # you could try it on the original vector..
    # x <- c(TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE)

    # ..but it also works on a more scattered vector
    x <- c(TRUE, FALSE , TRUE, FALSE, FALSE, FALSE, FALSE , TRUE, TRUE, TRUE)

    # find the position of all TRUEs
    true.positions <- which( x )

    # find the midpoint of the vector
    midpoint <- length( x ) / 2

    # find the smallest distance from the midpoint,
    small.dist <- ( true.positions - midpoint )

    # both above and below
    small.dist.above <- min( small.dist[ small.dist >= 0 ] )
    small.dist.below <- abs( max( small.dist[ small.dist <= 0 ] ) )

    # find the lowest position above the midpoint
    lpa <- which( small.dist.above == true.positions - midpoint )
    # find the highest position below the midpoint
    hpb <- which( small.dist.below == midpoint - true.positions )

    # if both are the midpoint, combine them
    closest.trues <- unique( c( hpb , lpa ) )

    # return the position in the original vector x
    # of the innermost TRUE
    true.positions[ closest.trues ]