Need Error Help

Error: Positive column indexes in [ must match number of columns:
.data has 14 columns

  • Position 2 equals 26
  • Position 3 equals 27
  • Position 4 equals 28
  • Position 5 equals 29
  • ... and 9 more problems

Does anyone know what exactly this error means?

This is the rename function i used: rename(Clean_AIM9X, 26=Jan) 26 being the old column name and Jan being the new

26 is not a viable column name in a data.frame, so its not clear how you got here.
But if you are using dplyr::rename, the new name should be on the left with the old on the right:

a_frame <- data.frame(a1=c(1,2,3))
# fine
b_frame <- data.frame(2=c(1,2,3))
# error
c_frame <- data.frame(`2`=c(1,2,3))
names(c_frame)
#fine but got auto renamed to X2
> names(c_frame)
[1] "X2"
rename(c_frame, Jan=X2)
#now its called Jan
```r

to share 20 records from your data try:

dput(head(Clean_AIM9X,n=20))

and paste the results but use a line of 3 backticks to make it format in the post

```r
my code to format

I imported my data from excel and it automatically set the variable names as 1,2,3,4 etc.

I'm sure we can come up with good code to change your variable names, but we need you to give us representative data. I suggested a method for that.

EQM_Data<-read_excel(file.choose(),sheet='Table')%>%
rename(EQM_Data, 8=Jan")

I believe my issue lies with the import from excel, it set a lot of the variables as numbers i.e 1,2,3.. etc

I could read in my own excel files, but I doubt that would help you.
if you don't (for whatever reason) feel up to running dput() as suggested you could read names, to now for sure what the names are...

names(EQM_Data)

also, EQM_Data is not Clean_AIM9X, so I guess its a recurring issue for you.

the names function showed me the specific names, there are a bunch of decimals involved, I will work on it after lunch and let you know how it goes. Thank you!

Okay, so i used the name function to return names and it showed me that the variable names are ...8, etc and not just 8. However, when i use the rename function from deplyr with these variables it still returns me the same error about positive column indexes in '[' must match number of columns. Sadly i cannot share with you guys the exact data i am using, but i will work to get a example that is not proprietary information.

probably a nice and simple approach for you would be to decide on a list of names (in order of the position of the column) and just set the names(mydf) <- mylistofnames
to change it
type ?names in the console to see examples in the help

I will work on this today and most likely respond Monday, thanks for the help!!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.