JavaEar 专注于收集分享传播有价值的技术资料

How to solve it. error:Error in data[[rowvar]] : attempt to select less than one element

In order to simplify my question, I will first show an example of a data file.

Data file:

clo1  clo2    clo3
1     2       3
1     2       3
5     6       7

R code:

read.table("demo.txt", header = TRUE, sep = "\t", row.names = "a")

Output:

  
> Error in data[[rowvar]] : attempt to select less than one element
In addition: Warning message:
In read.table(file = file, header = header, sep = sep, quote = quote,  :
  incomplete final line found by readTableHeader on '../demo.txt'

1个回答

    最佳答案
  1. The problem is caused by row.names = "a". Data set must have different row names. If you try to name all rows "a", you get the error. If you have a vector which contains the row names you want to use, you can do this.

    > a <- c("A", "B", "C")
    > read.table("demo.txt", header = TRUE, row.names = a)
      clo1 clo2 clo3
    A    1    2    3
    B    1    2    3
    C    5    6    7