too many errors in R program : I have just started studying R in second sem of my undergrad. It is hard to figure out a lot of things here. I hope if you can point me in right direction?

initially i was not sure why require lines were giving error, then i figured they were trying to load packages which were not in my RStudio installation, so i got them on. I also figured that the data given to me was to be put in a file named auto_mpg.txt. But the errors after that line are hard to figure.

can you pl help me understand this?

ty.


    R version 3.4.1 (2017-06-30) -- "Single Candle"
    Copyright (C) 2017 The R Foundation for Statistical Computing
    Platform: x86_64-w64-mingw32/x64 (64-bit)
    
    R is free software and comes with ABSOLUTELY NO WARRANTY.
    You are welcome to redistribute it under certain conditions.
    Type 'license()' or 'licence()' for distribution details.
    
    R is a collaborative project with many contributors.
    Type 'contributors()' for more information and
    'citation()' on how to cite R or R packages in publications.
    
    Type 'demo()' for some demos, 'help()' for on-line help, or
    'help.start()' for an HTML browser interface to help.
    Type 'q()' to quit R.
    
    [Workspace loaded from ~/R/my_project/.RData]
    
    > #################################################################
    > #################  NIKITA TIWARI ############################
    > #################################################################
    > #################  DATA SUMMARY PJ1  ############################
    > ##       mpg             cyl              wt           region     
    > ##  Min.   : 9.00   Min.   :3.000   Min.   :1613   Min.   :1.000  
    > ##  1st Qu.:17.50   1st Qu.:4.000   1st Qu.:2224   1st Qu.:1.000  
    > ##  Median :23.00   Median :4.000   Median :2804   Median :1.000  
    > ##  Mean   :23.51   Mean   :5.455   Mean   :2970   Mean   :1.573  
    > ##  3rd Qu.:29.00   3rd Qu.:8.000   3rd Qu.:3608   3rd Qu.:2.000  
    > ##  Max.   :46.60   Max.   :8.000   Max.   :5140   Max.   :3.000  
    > ##                                                                
    > ##             model    
    > ##  ford pinto    :  6  
    > ##  amc matador   :  5  
    > ##  ford maverick :  5  
    > ##  toyota corolla:  5  
    > ##  amc gremlin   :  4  
    > ##  amc hornet    :  4  
    > ##  (Other)       :369
    > #################################################################
    > #################################################################
    > 
    > require(gridExtra) #given to me
    Loading required package: gridExtra
    Warning message:
    package ‘gridExtra’ was built under R version 3.4.2 
    > require(ggplot2) #given to me
    Loading required package: ggplot2
    Warning message:
    package ‘ggplot2’ was built under R version 3.4.2 
    > 
    > 
    > auto <- read.table("auto_mpg.txt", sep="\t", header = TRUE) #given to me
    > head(auto)
                mpg.............cyl..............wt...........region
    1 Min.   : 9.00   Min.   :3.000   Min.   :1613   Min.   :1.000  
    2 1st Qu.:17.50   1st Qu.:4.000   1st Qu.:2224   1st Qu.:1.000  
    3 Median :23.00   Median :4.000   Median :2804   Median :1.000  
    4 Mean   :23.51   Mean   :5.455   Mean   :2970   Mean   :1.573  
    5 3rd Qu.:29.00   3rd Qu.:8.000   3rd Qu.:3608   3rd Qu.:2.000  
    6 Max.   :46.60   Max.   :8.000   Max.   :5140   Max.   :3.000  
    > summary(auto) #given to me
                                         mpg.............cyl..............wt...........region
     1st Qu.:17.50   1st Qu.:4.000   1st Qu.:2224   1st Qu.:1.000  :1                        
     3rd Qu.:29.00   3rd Qu.:8.000   3rd Qu.:3608   3rd Qu.:2.000  :1                        
     Max.   :46.60   Max.   :8.000   Max.   :5140   Max.   :3.000  :1                        
     Mean   :23.51   Mean   :5.455   Mean   :2970   Mean   :1.573  :1                        
     Median :23.00   Median :4.000   Median :2804   Median :1.000  :1                        
     Min.   : 9.00   Min.   :3.000   Min.   :1613   Min.   :1.000  :1                        
    > 
    > 
    > auto$cyl <- as.factor(auto$cyl)
    Error in `$<-.data.frame`(`*tmp*`, cyl, value = integer(0)) : 
      replacement has 0 rows, data has 6
    > auto$region <- as.factor(auto$region)
    Error in `$<-.data.frame`(`*tmp*`, region, value = integer(0)) : 
      replacement has 0 rows, data has 6
    > auto$cyl[auto$cyl == 1] <- "USA"
    Error in `$<-.data.frame`(`*tmp*`, cyl, value = character(0)) : 
      replacement has 0 rows, data has 6
    > auto$cyl[auto$cyl == 2] <- "EUR"
    Error in `$<-.data.frame`(`*tmp*`, cyl, value = character(0)) : 
      replacement has 0 rows, data has 6
    > auto$cyl[auto$cyl == 3] <- "ASIA"
    Error in `$<-.data.frame`(`*tmp*`, cyl, value = character(0)) : 
      replacement has 0 rows, data has 6
    > auto$region[auto$region == 1] <- "USA"
    Error in `$<-.data.frame`(`*tmp*`, region, value = character(0)) : 
      replacement has 0 rows, data has 6
    > auto$region[auto$region == 2] <- "EUR"
    Error in `$<-.data.frame`(`*tmp*`, region, value = character(0)) : 
      replacement has 0 rows, data has 6
    > auto$region[auto$region == 3] <- "ASIA"
    Error in `$<-.data.frame`(`*tmp*`, region, value = character(0)) : 
      replacement has 0 rows, data has 6
    > 
    > auto$cyl <- factor(auto$cyl, levels=c("USA","EUR","ASIA"))
    Error in `$<-.data.frame`(`*tmp*`, cyl, value = integer(0)) : 
      replacement has 0 rows, data has 6
    > auto$region <- factor(auto$region, levels=c("USA","EUR","ASIA"))
    Error in `$<-.data.frame`(`*tmp*`, region, value = integer(0)) : 
      replacement has 0 rows, data has 6
    > summary(auto)
                                         mpg.............cyl..............wt...........region
     1st Qu.:17.50   1st Qu.:4.000   1st Qu.:2224   1st Qu.:1.000  :1                        
     3rd Qu.:29.00   3rd Qu.:8.000   3rd Qu.:3608   3rd Qu.:2.000  :1                        
     Max.   :46.60   Max.   :8.000   Max.   :5140   Max.   :3.000  :1                        
     Mean   :23.51   Mean   :5.455   Mean   :2970   Mean   :1.573  :1                        
     Median :23.00   Median :4.000   Median :2804   Median :1.000  :1                        
     Min.   : 9.00   Min.   :3.000   Min.   :1613   Min.   :1.000  :1                        
    > 
    > ###################################################################
    > # calculate the mean mpg for cars, broken out by the number of 
    > # cylinders in the car.
    > ###################################################################
    > 
    > 
    > ###################################################################
    > #Provide a description of what you notice above
    > ###################################################################
    > # write a line describing the purpose of the next code chunk
    > ###################################################################
    > 
    > 
    > ###################################################################
    > #provide a dsecriptiojn of what you notice above
    > ###################################################################
    > # Histograms
    > ###################################################################
    > # the next chunk of code is creating bar graphs and filling them 
    > # with either region or cyl , with count on the y axis and 
    > # mpg on the x axis
    > ###################################################################
    > ggplot(auto, aes(x = mpg, y = count, fill = cyl)) + geom_bar()
    Don't know how to automatically pick scale for object of type tbl_df/tbl/data.frame. Defaulting to continuous.
    Error in FUN(X[[i]], ...) : object 'count' not found
    > ggplot(auto, aes(x = mpg, y = count, fill = region)) + geom_bar()
    Don't know how to automatically pick scale for object of type tbl_df/tbl/data.frame. Defaulting to continuous.
    Error in FUN(X[[i]], ...) : object 'count' not found
    > ###################################################################
    > # provide a description of what you notice above
    > ###################################################################
    > # write a line describe the purpose of the next chunck of code
    > ###################################################################
    > b1 <- ggplot(auto, aes(x=cyl, fill=cyl)) + geom_bar()
    > b2 <- ggplot(auto, aes(x=region, fill=region)) + geom_bar()
    > ###################################################################
    > # the above code is used to creat a specific bar graph and it's 
    > # x axis is the same a the fill so that it is more clear
    > ###################################################################
    > # the next chunck of code is used to fill the graph with the bars
    > ###################################################################
    > b3 <- ggplot(auto, aes(x = cyl, fill = region)) + geom_bar(position = "fill")
    > b4 <- ggplot(auto, aes(x = region, fill = cyl)) + geom_bar(position = "fill")
    > grid.arrange(b1, b2 ,b3, b4, ncol= 4)
    Error in FUN(X[[i]], ...) : object 'cyl' not found
    > ###################################################################
    > # provide a description of what you notice above
    > ###################################################################
    > # the code below is going to make box plots of the data
    > ###################################################################
    > bp1 <- ggplot ( auto, aes(x = cyl, y = mpg, fill = cyl)) + geom_boxplot()
    > bp2 <- ggplot ( auto, aes(x = region, y = mpg, fill = region)) + geom_boxplot()
    > grid.arrange(bp1, bp2, ncol = 2)
    Don't know how to automatically pick scale for object of type tbl_df/tbl/data.frame. Defaulting to continuous.
    Error in FUN(X[[i]], ...) : object 'cyl' not found
    > ###################################################################
    > # provide a description of what you notice above
    > ###################################################################
    > # the code below is going to make box plots of the data spilt into
    > # 3, 4, ,5, 6, 8 data with mpg being y axis, region being x axis
    > # and region being the fill
    > ###################################################################
    > 
    > 
    > 
    > 
    > ###################################################################
    > # provide a description of what you notice above
    > ###################################################################
    > # the code below is going to a jitter plot so basically its just a 
    > # lot dots on a graph
    > ###################################################################
    > jp1 <- ggplot ( auto, aes(x = wt, y = mpg, fill = cyl)) + geom_jitter()
    > jp2 <- ggplot ( auto, aes(x = wt, y = mpg, fill = region)) + geom_jitter()
    > grid.arrange(jp1, jp2, ncol = 2)
    Don't know how to automatically pick scale for object of type tbl_df/tbl/data.frame. Defaulting to continuous.
    Error in FUN(X[[i]], ...) : object 'wt' not found
    > ###################################################################
    > # provide a description of what you notice above
    > ###################################################################
    > # the code below is going to a jitter plot but separate the graphs
    > # into the different regions and only that region shows up on the
    > # graph 
    > ###################################################################
    > fg1 <- ggplot ( auto, aes(x = wt, y = mpg, fill = cyl)) + geom_jitter()
    > + facet_grid(region)
    Error in facet_grid(region) : object 'region' not found
    > fg1
    Don't know how to automatically pick scale for object of type tbl_df/tbl/data.frame. Defaulting to continuous.
    Error in FUN(X[[i]], ...) : object 'wt' not found
    > 
    > 
    > ###################################################################
    > # provide a description of what you notice above
    > ###################################################################
    > # the code below is going to a jitter plot but separate the graphs
    > # into the different regions and into different cyl and only that 
    > # cyl and that region show up in that graph
    > ###################################################################
    > 