R - 在两个语句之后将值写入变量

时间:2016-11-17 18:58:38

标签: r variables if-statement for-loop

我有一个包含两个if语句的for循环。一切正常,除了我想将第二个if语句传递给变量v1的值。我首先分配变量,变量充满了很多NA和我期待的三个值。但我只想在变量中使用这三个值。

因此,不是只编写满足要求的i,而是将每个i写入变量。

v1 <- c()

for(i in seq_along(y$values)){
  if(y$lengths[i] < 500 && y$values[i] == TRUE){
    y$values[i] <- FALSE
  }
  if(y$lengths[i] > 500 && y$values[i] == TRUE){
    print(y$lengths[i])
    v1[[i]] <- y$lengths[i]
  }
}

我查看了apply函数,但无法真正弄清楚如何在apply中使用这两个if语句。

数据是一个很大的逻辑向量。我在该向量上使用y <- rle(vec),它给了y$lengths(1到几十万之间的所有内容)和y$values(TRUE和FALSE)。

所需的结果是rle长度> 500且TRUE为真。:

[1] 5120
[1] 16630
[1] 10188

哪个打印命令让我很好。

dput(y$length)给了我:

c(129719L, 1L, 79337L, 2L, 4L, 1L, 3L, 1L, 2L, 1L, 1L, 1L, 4L, 
2L, 2L, 3L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 5L, 1L, 3L, 1L, 
6L, 1L, 5120L, 1L, 39L, 1L, 12L, 1L, 121L, 1L, 14L, 1L, 6L, 1L, 
3L, 1L, 3L, 1L, 2L, 1L, 6L, 1L, 11L, 1L, 9L, 1L, 10L, 2L, 6L, 
1L, 2L, 2L, 1L, 1L, 7L, 2L, 4L, 1L, 2L, 1L, 4L, 1L, 3L, 2L, 5L, 
1L, 5L, 4L, 8L, 1L, 4L, 1L, 4L, 2L, 2L, 2L, 9L, 1L, 2L, 1L, 2L, 
1L, 3L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 3L, 3L, 1L, 
7L, 1L, 1L, 1L, 1L, 2L, 6L, 2L, 2L, 1L, 2L, 4L, 3L, 1L, 1L, 1L, 
4L, 3L, 2L, 1L, 5L, 5L, 2L, 2L, 3L, 1L, 2L, 1L, 1L, 1L,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, 
1L, 6L, 1L, 6L, 1L, 6L, 2L, 14L, 1L, 5L, 1L, 9L, 1L, 1L, 1L, 
1L, 2L, 39L, 1L, 20L, 1L, 1L, 1L, 6L, 1L, 9L, 2L, 5L, 1L, 7L, 
1L, 16L, 1L, 22L, 1L, 1L, 1L, 10L, 1L, 20L, 1L, 18L, 1L, 20L, 
1L, 3L, 1L, 2L, 1L, 2L, 1L, 5L, 1L, 9L, 1L, 3L, 1L, 3L, 1L, 15L, 
1L, 10L, 1L, 40L, 1L, 30L, 1L, 111L, 1L, 314L, 1L, 9L, 1L, 10188L, 
4L, 88L, 1L, 8L, 1L, 1L, 1L, 1L, 1L, 15L, 1L, 24L, 1L, 1L, 2L, 
3L, 1L, 16L, 1L, 3L, 1L, 4L, 2L, 2L, 2L, 1L, 2L, 5L, 2L, 2L, 
7L, 1L, 1L, 3L, 2L, 3L, 7L, 2L, 1L, 1L, 6L, 1L, 4L, 2L, 2L, 1L, 
2L, 1L, 1L, 1L, 13L, 1L, 2L, 1L, 2L, 1L, 3L, 1L, 3L, 1L, 4L, 
1L, 16L, 1L, 4071L, 5L, 162912L, 1L, 6L, 1L, 280986L)

2 个答案:

答案 0 :(得分:1)

你可以非常有效地获得最终的v1向量而不用循环或像这样应用:

v1 <- y$lengths[y$lengths > 500 & y$values == TRUE]

如果你想使用循环,这里有两个选项:

(1)您可以像这样附加到v1:

v1 <- c()
for(i in seq_along(y$values)){
  if(y$lengths[i] < 500 && y$values[i] == TRUE){
    y$values[i] <- FALSE
  }
  if(y$lengths[i] > 500 && y$values[i] == TRUE){
    print(y$lengths[i])
    v1 <- c(v1, y$lengths[i])
  }
}

(2)如果向量v1真的很长,那么像这样的追加可能会变慢。作为替代方案,您可以预先分配,然后最终排除这些NAs:

v1 <- rep(NA, length)
for(i in seq_along(y$values)){
  if(y$lengths[i] < 500 && y$values[i] == TRUE){
    y$values[i] <- FALSE
  }
  if(y$lengths[i] > 500 && y$values[i] == TRUE){
    print(y$lengths[i])
    v1[i] <- y$lengths[i]
  }
}
v1 <- v1[!is.na(v1)]

答案 1 :(得分:0)

这似乎适用于我通过阅读您的问题推断出的样本数据:

error

**

y<-data.frame(lengths=seq(100,900,100), 
              values=c("TRUE", "FALSE", "TRUE", "FALSE", "TRUE", "FALSE", "TRUE", "FALSE", "TRUE"))
v1 <- c()

for(i in seq_along(y$values)){
  if(y$lengths[i] < 500 && y$values[i] == TRUE){
y$values[i] <- FALSE
  }
  if(y$lengths[i] > 500 && y$values[i] == TRUE){
#print(y$lengths[i])
ifelse(length(v1)>0, v1<-rbind(v1, y$lengths[i]), v1<-y$lengths[i])
#v1 <- rbind(y$lengths[i])
  }
}

**