Skip to content

avoid backwards sequence trap #158

@PeterJacko

Description

@PeterJacko

Using for(i in 1:n), especially for(i in 1:length(x)), is in general unsafe in R because if n=0, then 1:0 is not an empty vector (it actually has 2 elements: 1 and 0). So, one either needs to always check that n > 0 just before the for loop or use for(i in seq_len(n)) instead.

https://win-vector.com/2018/02/19/r-tip-use-seq_len-to-avoid-the-backwards-sequence-bug/

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions