2017-10-01 37 views
-4

私は大学院生のセカンドセミナーでRを勉強し始めました。ここでは多くのことを理解することは難しいです。私が正しい方向に向けることができれば幸いです。Rプログラムのエラーが多すぎます

最初に私はなぜラインがエラーを出しているのか分からなかったので、彼らは私の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 
> ################################################################### 
> 
+1

本当にそのファイルを読み込む必要がありますか?これは、 'dataset'が組み込まれた' base R'データセットである 'mtcars'に似ている' summary(dataset) 'の結果と思われます。私はあなたの先生に相談して、あなたの質問をするべきであると信じています。 –

+0

こんにちは、私はこの行を行ごとにしようとしています。上記はコード全体を実行した結果です。したがって、例えば。なぜ私はこのエラーが理解できないのですか? > auto $ cyl < - as.factor(auto $ cyl) '$ < - 。data.frame'( '* tmp *'、cyl、value = integer(0)): 置換は0行、データは6 – Nikita

答えて

1

、エラーメッセージ Error in $<-.data.frame(tmp, cyl, value = integer(0)) : replacement has 0 rows, data has 6は、電子ので起こります。 g。 auto$cylNULLを返します。

auto <- read.table("auto_mpg.txt", sep="\t", header = TRUE) を実行すると、コード名のように名前が付けられていないためです。 eの代わりに。 g。 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には、オブジェクトに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は、誤った列の名前付けを再び目的としています。これは、コードスニペットの末尾に続く次のエラーメッセージも参照します。

0

ようこそスタックオーバーフロー! Rを使うと、重要なプログラミング言語を学び、役に立つプラットフォームを使い始めることができます。おそらく、how you can create a minimal exampleask great questionsについて読むことを検討したいと思うかもしれません。コードを投稿するときは、コードで達成したいことを知ることが常に有益です。重要なステップごとに# commentsを使用することをお勧めします。

@ rui-barradasは、あなたのデータを読んでいるときに問題があると思っています(これは通常のケースです)。データの似たような例がなければ、言うことは難しいです。 head(auto)を使用して、プライベートデータでない場合は結果を質問に追加することができます。

しかし、おそらくView(auto)を使用して、Rがデータセットを正しく読み取って望ましいデータフレームを生成しているかどうかを確認したいとします。 (実際にdata.frameの場合はclass(auto)で確認できます)結果が醜い場合(例えばカラム名が奇妙な場合)、?read.tableを使用し、read.table()のオプションをカスタマイズする方法を探索してください(例:stringsAsFactors = FALSEまたはsep = ";"あなたの* .txtファイルのデータで使用されている))を使用して、Rがどのようにデータを読み取るかを制御します。

関連する問題