1

Im new to R and not used to the Syntax very well i got the following Error: “Error: unexpected '}' in ”}" so i know now that there is any Problem with my parantheses. Problem is, I am looking for 1 h now and I couldnt find any unmached Brackets. while i was parsing the Code itselve seemed quiet expensive for a solution which should be simple.

so my Intention ist to search a directroy full of CSV and i want to concatenate those (rowwise) which have the same Filename. Is there any function in R yet? Or is the following approach acceptable?

concated_CSV <- data.frame()
Data1 <- data.frame(n)
Data2 <- data.frame()

for (File in Filenames) {
  if (Data1$n == 1) {
    Data1               <- read.csv(File, header=T, sep=";", dec=",")
    Filename_Data1      <- unlist(strsplit(File, ".csv"))
    Tendril_Nr_Data1    <- unlist(strsplit(File, "_"))[1]
  } 
  else if (is.na(Data1$n)) {
    Data2               <- read.csv(File, header=T, sep=";", dec=",")
    Filename_Data2      <- unlist(strsplit(File, ".csv"))
    Tendril_Nr_Data2    <- unlist(strsplit(File, "_"))[1]
  }
  else if (Tendril_Nr_Data1 == Tendril_Nr_Data2) {
    concated_CSV        <- rbind(Data1, Data2)
    new_Filename        <- paste0(trg_dir, "/", Tendril_Nr_Data1, ".csv")
    write.csv(concated_CSV, new_Filename, row.names=FALSE)
  }
}
      

thank you very much and
best wishes

jay.sf
  • 60,139
  • 8
  • 53
  • 110
blueliquid
  • 11
  • 1
  • 1
    Not sure what you are trying to achieve maybe "read multiple files and rowbind", see https://stackoverflow.com/questions/11433432/how-to-import-multiple-csv-files-at-once – zx8754 May 17 '21 at 13:10
  • We can't replicate your issue, read: https://stackoverflow.com/a/5963610/6574038. ANyway, I think the second `else if` should be an `if` because you refer to the preceding code. And instead of `Tendril_Nr_Data1 == Tendril_Nr_Data2` try `identical(Tendril_Nr_Data1, Tendril_Nr_Data2)`. Not sure, what you're doing though. – jay.sf May 17 '21 at 13:36
  • Note that if `Data1$n` is missing `if (Data1$n == 1)` will throw an error before you get to `else if (is.na(Data1$n))`. You should put the "if missing" condition first. – Gregor Thomas May 17 '21 at 13:48
  • It's a bit confusing having a clarification of the question as an answer - the question should be editted to contain the correct code. The first error with the revised code relates to `if (Data1$n=!1)` - `=!` is not valid syntax in this situation, and should be `!=`. – Miff May 17 '21 at 14:46

1 Answers1

0

thanks for your Answers. As you see Im aswell new to Stackoverflow and was just on the reading side so far. here ist the code i tryied to simplify so you cant use it. the "Filenames" represents the Filenames im dealing with.

 #Stackoverflow example
Filenames <- c("6.1.3.1_1.CSV","6.1.3.1_2.CSV","6.4.3.1.CSV","6.1.2.1_1.CSV","6.1.2.1_2.CSV","6.1.5.CSV")
Filename_Data1 <- "6.1.3.1_1.CSV"
Filename_Data2 <- "6.1.3.1_2.CSV"

#record File for an Output 

concated_CSV<- data.frame()
n <- 1
Data1 <- data.frame(n)
Data2<- data.frame()
  
for(File in Filenames){
   
  
    if (Data1$n==1 ){
      Data1               <- read.csv(File, header=T, sep=";", dec=",")
      Filename_Data1      <- unlist(strsplit(File, ".csv"))
      Tendril_Nr_Data1 <- unlist(strsplit(Filename_Data1, "_"))[1]
      
  
   } else if (Data1$n=!1){ 
      Data2               <-  read.csv(File, header=T, sep=";", dec=",")
      Filename_Data2      <- unlist(strsplit(File, ".csv"))
      Tendril_Nr_Data2    <- unlist(strsplit(Filename_Data1, "_"))[1]
    
    } else if (identical(Tendril_Nr_Data1, Tendril_Nr_Data2)){
          concated_CSV <- rbind(Data1, Data2)
          #tis is the name and directory to which the file should be saved in
          #new_Filename <- paste0(trg_dir, "/",Tendril_Nr_Data1,".csv")
          n_Filename <- "hello"
          write.csv(concated_CSV,n_Filename, row.names = FALSE)
          
    }
}
    

the missing parantheses hasnt disappered.

My intention ist to write a program which compares CSV-Data-Filenames in a given Directory and if there is a Filename twice for example "abc_1.csv" and abc_2.csv" the Program shall concatenate the CSV-Data rowwise and save a file named "abc.csv" (hope this is clearer).

blueliquid
  • 11
  • 1