0

I am trying to use a loop to manipulate multiple files but also to retain the names of the variables / outputs in each loop cycle.

Here is an example of what I am trying to do:

#define my source
SourceDir <- file.path('where/it/is')

#define my array
Wpns <- ('Bren', 'Welrod', 'Vickers')

#start for-loop
for (Wpn in Wpns){

    #example of for loop contents 1 (is this a sensible way to get a file path?)
    WpnFile <- file.path(paste0(SourceDir, '/My'_, Wpn, '_File.txt')

    #example of for loop contents 2
    WpnDataFrame <- read_delim(WpnFile, ' ')

}

So what I want now is to have six variables (well the last three are data frames) that I could call to look at. Obviously what I am actually going to do is overwrite WpnFile and WpnDataFrame over and over.

BrenFile
WelrodFile
VickersFile

BrenDataFrame
WelrodDataFrame
VickersDataFrame

This is the very first time I have used r so I wouldn't be surprised if what I have written (or maybe what I am trying to achieve) looks bizarre.

(For some context if it matters: I'm trying to manipulate and then plot data from text file. However I really need to check each stage of my for-loop (e.g. the data frames I create), so that I can check what I'm doing. This is also key in helping me visualise the changes I am making, as I am not used to working with matrices and additionally I may need toto return to specific data frames later.)

Solebay Sharp
  • 519
  • 7
  • 24
  • Possible duplicate of [How to import multiple .csv files at once?](https://stackoverflow.com/questions/11433432/how-to-import-multiple-csv-files-at-once) – R. Schifini Apr 07 '19 at 03:26
  • If it is a duplicate I apologise, I try to check quite thoroughly before posting but I'm not very good at using accurate terminology to describe my goals and intentions and my lack of experience means I overlook answers that might help me sometimes. – Solebay Sharp Apr 07 '19 at 03:31
  • 2
    No problem. The comment is an automated message when tagging a possible duplicate. See if the answer in the link helps you or not. If not, rewrite your question with highlighting where your question differs from the link. – R. Schifini Apr 07 '19 at 03:52
  • @SolebaySharp: see also this https://stackoverflow.com/a/48105838/786542 – Tung Apr 07 '19 at 04:42
  • I'd say this is actually [an XY problem](https://meta.stackexchange.com/q/66377): you're asking about working with a `for` loop to import files, but even the most beautiful code to do that won't work because you've incorrectly defined your vector: `Wpns <- ('Bren', 'Welrod', 'Vickers')`. That set of names should be wrapped in `c()`, not just `()`. I'm guessing you're coming to R from another language (Python?), but in R vectors are defined like this with `c()` – camille Apr 07 '19 at 15:20
  • @camille yes, I've dabbled in python and bash. I'm now dabbling in R. Subsequently my attempts to write code often start as strange mosaics of three languages, much like the English language. Except English works. – Solebay Sharp Apr 23 '19 at 14:57

1 Answers1

1

So if I get it right you want to add the names of the files to the source in the loop and then load the corresponding data. If so, I can suggest doing

# define my source
SourceDir <- file.path('where/it/is') # unchanged

#define my array
Wpns <- c('Bren', 'Welrod', 'Vickers') # you must use c()

# creating an empty list for your data being loaded
WpnDataFrame <- mylist <- vector("list", length(Wpns))
# naming the list
names(WpnDataFrame) <- Wpns

# creating a vector for the file paths
WpnFile <- rep(NA, length(Wpns))

# i can not show it with real data since my pc has other file paths then yours so here is some data for demonstration
df <- 1:3

# running for loop
for(i in 1:length(Wpns)){
# saving each file path
WpnFile[i] <- file.path(paste0(SourceDir, '/My_', Wpns[i], '_File.txt'))

# loading the data
# for demonstration:  
WpnDataFrame[[Wpns[i]]] <- df[i] 
# you want to do this: 
# WpnDataFrame[[Wpns[i]]] <- read_delim(WpnFile, ' ')
}

This gives you

WpnFile
[1] "where/it/is/My_Bren_File.txt"    
"where/it/is/My_Welrod_File.txt" 
[3] "where/it/is/My_Vickers_File.txt"

WpnDataFrame
$Bren
[1] 1
$Welrod
[1] 2
$Vickers
[1] 3

Some notes: I used a class list for WonDataFrame since I am unsure whether your data being loaded is in same format (same number of columns, columns in same order). In a list it does not matter if one df has 3 columns and another has 4.

  • So by the looks of it, a list of Data frames is the way to go. However I now have a list of 4 by 1 Data Frames, of which the first one has a path followed by three NA's, the second has two paths followed by two NA's and so on. Also 'mylist' is just full of NULL, what it is doing? – Solebay Sharp Apr 07 '19 at 09:27
  • Ignore that, when I copy and pasted it I deleted a bit by mistake and filled it back in with the wrong information. Thank you very much for helping, though I'm still confused by the empty list I have, not that it matters at all! – Solebay Sharp Apr 07 '19 at 09:37
  • You can always try to insert some value for i, for example, and go thru the code inside the for loop and look what happens. While doing this check every object whether it is what yoy exoect to be. –  Apr 07 '19 at 09:42