R 語言 當提出疑問時 如何做一個可重現的例子

R 語言  當提出疑問時  如何做一個可重現的例子

簡單來說,當遇到問題時,想要在網路上提出疑問
如果問題夠深夠細,一般中文論壇可能是不容易找到答案的
這時就只好,向英文論壇前進

另個困難就來了,該如何問才能讓別人了解你所面臨的問題
首先我看來參考一下  以下網址: 

 其中一段是這樣說的

A minimal reproducible example consists of the following items:

  • a minimal dataset, necessary to reproduce the error
  • the minimal runnable code necessary to reproduce the error, which can be run on the given dataset.
  • the necessary information on the used packages, R version, and system it is run on.
  • in the case of random processes, a seed (set by set.seed()) for reproducibility 
     

挑重點來說:
如何使用一個小的數據集來,重現提問者遇到的問題
當然最好是用一些簡單的程式碼,來製造出問題數據
最好不要使用檔案傳遞來讓別人了解問題  

那如何做?

Copy your data

If you have some data that would be too difficult to construct using these tips, then you can always make a subset of your original data, using eg head()subset() or the indices. Then use eg. dput() to give us something that can be put in R immediately :

如果你有一些數據太難構建
你可以使用 
dput()來建立原始數據
 

> dput(head(iris,4))
structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6), 
          Sepal.Width = c(3.5, 3, 3.2, 3.1), 
          Petal.Length = c(1.4, 1.4, 1.3, 1.5), 
          Petal.Width = c(0.2, 0.2, 0.2, 0.2), 
          Species = structure(c(1L, 1L, 1L, 1L), 
          .Label = c("setosa", "versicolor", "virginica"), class = "factor")), 
          .Names = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species"), 
          row.names = c(NA, 4L), class = "data.frame")

這樣你就可以取得自動重構數據集的程式碼
以這段程式碼來發問,別人就比較能夠理解數據本身的問題