David
David

Reputation: 119

Autocompletion error during the data frame column selection in RStudio

I used the readxl package to import from the Excel file into RStudio. Now I'm trying to access a column in that dataset using the $ operator. However, I keep getting the notification:

(Type Error): null is not an object (evaluating a.length)

Even though I've performed this type of operation many times before without issue...

The error I'm getting:

enter image description here

The dataset in the Global Environment pane:

enter image description here

Upvotes: 11

Views: 2898

Answers (1)

Artem
Artem

Reputation: 3414

The root of the problem is located in NA used as a column name. The error is thrown since RStudio autocompletion is not able to extract column names.

Please see the reproduction of the problem:

df <- data.frame(a = 1:3, b = 1:3)
names(df)[2] <- NA

If you will try to typedf$a the error below will be generated.

enter image description here

To avoid this kind of situation you should assign data.frame column names explicitly. You have to options:

  1. assign names(df) <- c("a", "b");

  2. delete the spacer columns from the source Excel file to avoid NA use as the column names.

Upvotes: 1

Related Questions