-1

screenshot of my Rstudio

I would love some help figuring out if the column names in 34 .txt files are identical. The columns should make up the first 69 values in the files. I'm very new to R, but have programmed before, so I would prefer a low-level solution that I can understand.

In addition, I haven't found a way to efficiently read in all of the files. This seems not to be necessary for the remainder of my code. But I can imagine it might be necessary for the above.

Finally, in the screenshot you will see that I have a list of all the .txt files (called all_files), I have one .txt file read in, and I created a list of the column names in that .txt files (called column_names).


The idea I came up with was the following:

screenshot of script attempt

I think the problem here is that:

colnames(all_files[i]) doesn't work because:

  • the files are not read in
  • all_files[i] returns a string with .txt at the end, but colnames() wants the file name without .txt

I hope this all makes some kind of sense. Thanks very much in advance.

Kind regards,

Boris Pinksterboer

  • 1
    Welcome. Please paste the code directly into your question and **don't** post code as images. It makes it much much harder for anyone to help you. The best way is to edit your question to include the output of ``dput(head(data))``. More information can be found [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Thank you. – user438383 Apr 27 '21 at 10:56

1 Answers1

1

You need to think carefully about the steps to perform to solve this problem.

  1. read each file
  2. fetch the column names
  3. check them

You seem to be missing the first step.

There are multiple ways to accomplish this, I'm picking one that is likely well suited to your knowledge of R.


for( i in all_files ) {
  d <- read.table( all_files[i] )
  if( not(identical( colnames(d), column_names )) ) {
    cat( basename(all_files[i]), " does not have the right columns\n" )
  } else {
    cat( basename(all_files[i]), "OK!\n" )
  }
}

It will tell you in the terminal which file has the right columns and which has not.

Sirius
  • 5,224
  • 2
  • 14
  • 21