Line graph ggplot geom.line

I can reproduce the error you get if AllDat is not a data frame.

class(tmp) #tmp is a tibble, basically a data frame
[1] "grouped_df" "tbl_df"     "tbl"        "data.frame"
AllDat <- 3 #define AllDat so that it is not a data frame
rbind(AllDat, tmp) #this throws the error
Error: Argument 1 must have names.
Run `rlang::last_error()` to see where the error occurred.

AllDat <- data.frame() #make AllDat an empty data frame
AllDat <- rbind(AllDat, tmp) # No error

The following code works for me to read in two csv files and rbind them into one data frame.

library(dplyr)
Files <- list.files(path = "~/R/Play/", pattern = ".csv")
 
AllDat <- data.frame()
summary(AllDat) #This line is not necessary, just for illustration
< table of extent 0 x 0 > #AllDat is an empty data frame

for ( FileName in Files) {
   tmp <- read.csv(paste0("~/R/Play/", FileName),sep=" ")
   tmp$File <- FileName
   tmp <- tmp %>% group_by(File) %>% 
     mutate(Grp = (row_number() - 1) %/% 3)
   AllDat <- rbind(AllDat, tmp)
}
summary(AllDat)
      dur         energy         f01            values          val            File          
 Min.   :35   Min.   :100   Min.   :234.0   Min.   : 633   Min.   : 2.00   Length:24         
 1st Qu.:41   1st Qu.:100   1st Qu.:238.8   1st Qu.:2790   1st Qu.: 9.25   Class :character  
 Median :47   Median :101   Median :245.0   Median :5392   Median :18.00   Mode  :character  
 Mean   :47   Mean   :101   Mean   :245.9   Mean   :5252   Mean   :17.33                     
 3rd Qu.:53   3rd Qu.:102   3rd Qu.:249.2   3rd Qu.:7762   3rd Qu.:25.50                     
 Max.   :59   Max.   :102   Max.   :275.0   Max.   :9713   Max.   :32.00                     
      Grp      
 Min.   :0.00  
 1st Qu.:0.75  
 Median :1.50  
 Mean   :1.50  
 3rd Qu.:2.25  
 Max.   :3.00  

Exactly, I don't know if it's normal that when I run:

AllDat <- data.frame()

and I see AllDat, this is empty. Should it be already filled with the csv in the directory?
I'm stuck for now.

It is normal that the data frame returned by

AllDat <- data.frame()

is empty. At that step in the code, AllDat is made so that it can receive the data that will be read in during the for loop. Within the for loop, the variable named tmp temporarily holds the data read in from each csv file and those data are appended to AllDat by the rbind function.The first time rbind is used, it appends tmp to an empty data frame and that is allowed. After that operation, AllDat has the same data as that first version on tmp. During the second iteration of the for loop, the original tmp gets overwritten but that does no harm because its data were already stored in AllDat. Each iteration of the loop builds up AllDat with the data of a new file.

This topic was automatically closed 7 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.