R for Data Science Ch.4

4.1 Coding basics

  • assignment statements (賦值): create new objects with <- .Don’t be lazy and use =: it will work, but it will cause confusion later. 在RStuido中快速鍵: Alt + - (the minus sign)
#  create new objects with <-
object_name <- value

4.2 What’s in a name? 命名習慣與規則

  • Object names must start with a letter, and can only contain letters, numbers, _ and .
  • 命名最好帶有意義,以便日後重讀code時容易讀懂
  • 作者推薦以 snake_case命名:以底線_分隔小寫字母
i_use_snake_case
otherPeopleUseCamelCase
some.people.use.periods
And_aFew.People_RENOUNCEconvention
  • 輸入文字按 Tab 鍵,會列出符合輸入文字的物件、函數、功能……,等所有指令
  • 按F1鍵,會跳出相關的help檔
  • The + tells you that R is waiting for more input. Usually that means you’ve forgotten either a " or a ) .

4.3 Calling functions

# y <- seq(1, 10, length.out = 5)
# y

#This common action can be shortened by surrounding the assignment with parentheses, 
#which causes assignment and “print to screen” to happen.
(y <- seq(1, 10, length.out = 5))
  • 在RStudio右上方的Environment可以查看我們所建立的所有的object

Exercise 4.1
容易造成bug,不易分辨的兩組英文字母及數字:

  • the numeral zero (0), the Latin small letter O (o), and the Latin capital letter O (O),
  • the numeral one (1), the Latin small letter I (i), the Latin capital letter I (I), and Latin small letter L (l).

Error messages of the form "object '...' not found" mean exactly what they say.  The most common scenarios in which I encounter this error message are

  1. I forgot to create the object, or an error prevented the object from being created.
  2. I made a typo in the object’s name, either when using it or when I created it (as in the example above), or I forgot what I had originally named it. If you find yourself often writing the wrong name for an object, it is a good indication that the original name was not a good one.
  3. I forgot to load the package that contains the object using library().

Exercise 4.3
Press Alt + Shift + K, this gives a menu with keyboard shortcuts. This can be found in the menu under Tools -> Keyboard Shortcuts Help.

發表者:Q

塵世中一個迷途小書僮

發表留言

使用 WordPress.com 設計專業網站
立即開始使用