Error during wrapup: Replacement is 0 column, but data is 133 column error cannot be resolved

Asked 2 months ago, Updated 2 months ago, 1 views

I wrote a function to extract data from the Dendrite Length and FilamentID columns from a csv file like the image below.
Enter a description of the image here

DataandIdExtractionfromCSV<-function(path,sectionid,dataname){
  
  df = read.csv(path, 
                     fileEncoding="UTF-8-BOM",
                     stringsAsFactors=F,
                     na.string="NULL",
  )
  
  df<-na.omit(df)
  # Delete lines 0, 1 and 2
  df2<-df [c(-0,-1,-2),]
  
  
  # Convert datatype between leftmost column and FilamentID
  
  df2$dataname<-as.numeric(df2$dataname)
  df2$X.5<-as.integer(df2$X.5)
  
  
  # Redefine Dendrite Length and FilamentID as a tibble column, also with Header
  tibbledataid=tibble(tibbledataid=df2$dataname)
  tibbledata=tibble(tibbledata=df2$X.5)
  
  # Consolidate two columns into a single tibble
  df_tidy=bind_cols(tibbledataid,tibbledata)
  # matrix extracted by length and corresponding id
  head(df_tidy)
  tidieddfname=paste(sectionid, "_tidy.csv", sep="", collapse=NULL)
  write.csv(df_tidy, tidieddfname)
  
}

If you do this as follows,

DataandIdExtractionfromCSV("WT1x20ROI1Sctx06212021_Statistics/Dendrite_Length.csv", "tesuto", Dendrite.Length)
Error during wrapup—Replacement is 0 columns, but data is 133 columns. 
Error: no more error handlers available (recursive errors?); invoking 'abort' restart

The above error will appear.
After checking the error points in the browse() function, first of all,

df2$dataname<-as.numeric(df2$dataname)

I found out that it was stuck in the above parts, but I didn't know what else to do and what to do.

We would appreciate it if you could let us know.

Note: Enter a description of the image here
The X.5 column in the code was automatically given as follows:

r

2022-09-30 11:26

1 Answers

The problem lies in the notation df2$dataname.This means the dataname column of the data frame df2.Therefore, change the notation to direct access.

###Change to df2$dataname->df2[,dataname]

  df2$dataname<-as.numeric(df2$dataname)
  =>
  df2[,dataname]<-as.numeric(df2[,dataname])

  tibbledataid=tibble(tibbledataid=df2$dataname)
  =>
  tibbledataid=tibble(tibbledataid=df2[,dataname])

Also, change the third argument that you specify when calling the DataandIdExtractionfromCSV function to a string.

##Change to Dendrite.Length ->"Dendrite.Length"

  DataandIdExtractionfromCSV("...", "tesuto", Dendrite.Length)
  =>
  DataandIdExtractionfromCSV("...", "tesuto", "Dendrite.Length")


2022-09-30 11:26

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.