Fixing R Error – Attempt to select less than one element in get1index

This error message occurs when you try to access a list element number that is less than one. Fixing the problem involves making sure the list element number is one or greater. While this can happen even if you are not using a package function, it can easily happen when making a plot using an r package.

Description of the R error

A list is similar to a data frame in that it consists of multiple elements. However, while a single column of a data frame is accessed by its column name, the list elements are accessed through a single dimension index. Like those used with matrices, this index is contained within brackets using the format list[[index]]. A second dimension can be added by using the format list[[index1]][index2] when the list element is a vector, which is like accessing a row in a data frame. However, the first dimension must be an integer with a value of one or larger otherwise you will get our error message. A list can consist of a variety of data types. They include vector types such as numeric vectors, character vectors, character string vectors, and even logical vectors. A list is a useful object, but if you are not careful an index variable can reach a value of zero and cause a problem.

Explanation of the R error

Here we have two r code examples that show us how to produce this message.

> x=list(1:5,
+ c(“Sue”,”Louise”,”Ann”,”Paul”,”Dave”),
+ c(67,63,60,57,46),
+ c(TRUE,TRUE,TRUE,FALSE,FALSE))
> x[[0]]
Error in x[[0]] :
attempt to select less than one element in get1index

In this first example, the list index is set at zero and it triggers our message.

> x=list(1:5,
+ c(“Sue”,”Louise”,”Ann”,”Paul”,”Dave”),
+ c(67,63,60,57,46),
+ c(TRUE,TRUE,TRUE,FALSE,FALSE))
> x[[-2]]
Error in x[[-2]] :
attempt to select more than one element in get1index

In this second example, the list index is set at negative two and it triggers our message. This shows that this problem results from more than just having zero as an index, but a negative index value as well.

How to fix the R error

Here we have three examples of code that do not produce our problem.

> x=list(1:5,
+ c(“Sue”,”Louise”,”Ann”,”Paul”,”Dave”),
+ c(67,63,60,57,46),
+ c(TRUE,TRUE,TRUE,FALSE,FALSE))
> x[[1]]
[1] 1 2 3 4 5

In this example, we have the simple case of just making sure that the list index is a positive integer. Specifically, the value of the index is set to one Note this fixes the problem.

> x=list(1:5,
+ c(“Sue”,”Louise”,”Ann”,”Paul”,”Dave”),
+ c(67,63,60,57,46),
+ c(TRUE,TRUE,TRUE,FALSE,FALSE))
> x[[4]][3]
[1] TRUE

In this example, we have the case of adding a second index so that we can access a single data point. Both indexes are greater than one and there is no problem.

> x=list(1:5,
+ c(“Sue”,”Louise”,”Ann”,”Paul”,”Dave”),
+ c(67,63,60,57,46),
+ c(TRUE,TRUE,TRUE,FALSE,FALSE))
> x[[1]]*2
[1] 2 4 6 8 10

In this example, we have the case of applying an operator to a complete list item to produce output for each of its values. You can get similar results with a function as well. Note that there is no problem.

While lists offer many features, it is necessary to make sure that the list index is never less than one. If this occurs it will produce our error message. One thing that can trigger this message is incrementally subtracting one from the index variable. This is because if the index value is one, this process will produce an index of zero. It is an easy problem to fix, by simply making sure that the index is never less than one. Such a check can be done with a simple if statement. This is a problem that once you understand what causes it, avoiding it and fixing it becomes simple.

Scroll to top
Privacy Policy