Run a script to multiple files in the same directory

knitr::opts_chunk$set(echo = TRUE)

#Hi. I need to run the script from A to E for all the text files in the same directory. This script
#doesn't work. Can someone help me? Thank you

files<-list.files(path = "~/Desktop/Zelos Enegy/Script project/Test_files2", pattern=.txt)
files
library(readr)
for(i in files){ 
  A<-read_table('~/Desktop/Zelos Enegy/Script project/Test_files2/'
                ,col_names = FALSE, skip = 68),
  B<-A[-c(1,2,13)],
  C<-B[29,5],
  D<-B[43,2],
  E<-data.frame(A,B),
  E
}


this looks wrong, it should almost certainly be quoted

, pattern=".txt")

Yes, sorry. Still I get stuck after the loop

Error: unexpected ',' in:
" A<-read_table('~/Desktop/Zelos Enegy/Script project/Test_files2/'
,col_names = FALSE, skip = 68),

the solution is provided in the error message.
you have commas fulfilling no purpose at the end of your lines.
remove them.

for(i in files){ 
  A<-read_table('~/Desktop/Zelos Enegy/Script project/Test_files2/'
                ,col_names = FALSE, skip = 68)
  B<-A[-c(1,2,13)]
  C<-B[29,5]
  D<-B[43,2]
  E<-data.frame(A,B)
  E
}

Get this error :frowning:
Error in file(con, "rb") : cannot open the connection
In addition: Warning messages:
1: In file(con, "rb") :
'raw = FALSE' but '/Users/sgalla/Desktop/Zelos Enegy/Script project/Test_files2' is not a regular file
2: In file(con, "rb") :
cannot open file '/Users/sgalla/Desktop/Zelos Enegy/Script project/Test_files2': it is a directory

oh, you get i from list of files, but you dont use it anywhere...

  A<-read_table(file.path('~/Desktop/Zelos Enegy/Script project/Test_files2',i),
                col_names = FALSE, skip = 68)

ok so with this:

files<-list.files(path = "~/Desktop/Zelos Enegy/Script project/Test_files2", pattern=".txt")
files
library(readr)
for(i in files){
A<-read_table(file.path('~/Desktop/Zelos Enegy/Script project/Test_files2/',i),
,col_names = FALSE, skip = 68)
B<-A[-c(1,2,13)]
C<-B[29,5]
D<-B[43,2]
E<-data.frame(A,B)
E
}

I am getting something. Not the result I want though.
I need the script to open each txt.file in the folder (skipping the first 68 lines for each file), delete those three columns, and go get the value for two specific cells. I would like it just to print me this two values for all the txt files in the folder. Do you think it's possible?

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.