[英]Too many errors in R program
我剛剛在本科第二學期開始學習 R。 在這里很難弄清楚很多事情。 我希望你能指出我正確的方向嗎?
最初我不確定為什么 require 行會出錯,然后我想他們正在嘗試加載不在我的 R 安裝中的包,所以我安裝了它們。 我還發現給我的數據應該放在一個名為 auto_mpg.txt 的文件中。 但是那條線之后的錯誤對我來說很難想象。
你能幫我理解這個嗎?
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
> ###################################################################
>
作為起點,錯誤消息Error in $<-.data.frame(tmp, cyl, value = integer(0)) : replacement has 0 rows, data has 6
發生,因為例如auto$cyl
返回NULL
。
這是因為在執行auto <- read.table("auto_mpg.txt", sep="\\t", header = TRUE)
您的列名沒有按照您的代碼命名。 而不是例如cyl
,您的auto
對象中的相應列似乎是cyl..............
。 所以你應該做的第一件事是交叉檢查你的read.table
函數是否按預期工作,列是否按預期命名。
此外,錯誤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
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
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
表示您的auto
對象中沒有名為count
列(在執行summary(auto)
時也未提及)。 因此,我假設您必須自己計算列count
的值。
最后, 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
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
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
中的錯誤: 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
目的是再次為您的列命名錯誤。 這也指代碼片段末尾的以下錯誤消息。
歡迎使用堆棧溢出! 使用 R,您將學習一門重要的編程語言並開始使用一個有用的平台。 也許您想考慮閱讀有關如何創建最小示例並提出好問題的信息。 此外,在發布代碼時,了解您想用代碼實現什么總是有益的 - 我建議對每個重要步驟使用# comments
。
正如@rui-barradas 懷疑的那樣,我認為讀取您的數據時存在問題(這是正常情況:),盡管沒有類似的數據示例很難說。 如果它不是私有數據,您可以使用head(auto)
並將結果添加到您的問題中。
但是,也許您想使用View(auto)
來檢查 R 是否正確讀取您的數據集並生成所需的數據框。 (您可以通過class(auto)
檢查它是否確實是 data.frame。)如果結果看起來很難看(例如列名顯示很奇怪),請使用?read.table
並探索如何自定義read.table()
(例如, stringsAsFactors = FALSE
或sep = ";"
(即 *.txt 文件數據中使用的分隔符))以控制 R 讀取數據的方式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.