Chapter 2 Basics
子曾经曰过,“九层之台起于累土;千里之行始于足下”。从这里,你要开始书写你的第一行R代码;你将要学习如何把R console当作计算器来使用以及来给变量赋值;你也将了解R的数据类型。 我们开始吧!
2.1 R的数学运算:
可以当作最基本的计算器来使用。
- -加: +
- 减: -
- 乘: *
- 除: /
- 乘方: ^
- 取余数: %%
## [1] 10
## [1] 0
## [1] 15
## [1] 5
## [1] 32
## [1] 4
2.2 赋值给变量
变量是R里面的一个基本概念。
使用R时,你可以用变量来存储一个值 (e.g. 4) 或者一个对象 (e.g. 函数) in R。之后你就可以使用变量的名称来使用这个值或者对象。简单说来,就是给一些东西取个名字,这样以后方便叫它。
试着用下面这行代码,来把4
这个数存储(赋值)给变量 my_var
,
然后你只需要输入my_var,R Console就将4输出来:
## [1] 4
2.2.1 练习
练习: 轮到你了: 在编辑区创建一个变量x,并将42这个数指派给它,然后点击“run”。接下来你可以直接在R console 输入x, R console 就会给你输出42。 答案:
## [1] 42
练习: 赋值给变量 (2) Suppose you have a fruit basket with five apples. As a data analyst in training, you want to store the number of apples in a variable with the name my_apples.
- Type the following code in the editor: my_apples <- 5. This will assign the value 5 to my_apples.
- Type: my_apples below the second comment. This will print out the value of my_apples.
- Click ‘Submit Answer’, and look at the console: you see that the number 5 is printed. So R now links the variable my_apples to the value 5.
答案:
# Assign the value 5 to the variable my_apples
my_apples <- 5
# Print out the value of the variable my_apples
my_apples
## [1] 5
练习: 赋值给变量 (3) Every tasty fruit basket needs oranges, so you decide to add six oranges. As a data analyst, your reflex is to immediately create the variable my_oranges and assign the value 6 to it. Next, you want to calculate how many pieces of fruit you have in total. Since you have given meaningful names to these values, you can now code this in a clear way: my_apples + my_oranges
- Assign to my_oranges the value 6.
- Add the variables my_apples and my_oranges and have R simply print the result.
- Assign the result of adding my_apples and my_oranges to a new variable my_fruit.
答案:
# Assign a value to the variables my_apples and my_oranges
my_apples <- 5
my_oranges <- 6
# Add these two variables together
my_apples + my_oranges
## [1] 11
The great advantage of doing calculations with variables is reusability. If you just change my_apples to equal 12 instead of 5 and rerun the script, my_fruit will automatically update as well. Continue to the next exercise.
2.2.2 苹果和橙子
Common knowledge tells you not to add apples and oranges. But hey, that is what you just did, no :-)? The my_apples and my_oranges variables both contained a number in the previous exercise. The + operator works with numeric variables in R. If you really tried to add “apples” and “oranges”, and assigned a text value to the variable my_oranges (see the editor), you would be trying to assign the addition of a numeric and a character variable to the variable my_fruit. This is not possible.
- Click ‘Submit Answer’ and read the error message. Make sure to understand why this did not work.
- Adjust the code so that R knows you have 6 oranges and thus a fruit basket with 11 pieces of fruit.
# Assign a value to the variable my_apples
my_apples <- 5
# Fix the assignment of my_oranges
my_oranges <- "six"
# Create the variable my_fruit and print it out
my_fruit <- my_apples + my_oranges
my_fruit
答案:
# Assign a value to the variable my_apples
my_apples <- 5
# Fix the assignment of my_oranges
my_oranges <- 6
# Create the variable my_fruit and print it out
my_fruit <- my_apples + my_oranges
my_fruit
## [1] 11
2.3 R 的基本数据类型
R works with numerous data types. Some of the most basic types to get started are:
- Decimal values like 4.5 are called numerics.
- Natural numbers like 4 are called integers. Integers are also numerics.
- Boolean values (TRUE or FALSE) are called logical.
- Text (or string) values are called characters.
- Note how the quotation marks on the right indicate that “some text” is a character.
2.3.1 练习
Change the value of the:
- my_numeric variable to 42.
- my_character variable to “universe”. (Note that the quotation marks indicate that “universe” is a character.)
- my_logical variable to FALSE. (Note that R is case sensitive!)
2.3.2 如何判断数据类型?
Do you remember that when you added 5 + “six”, you got an error due to a mismatch in data types? You can avoid such embarrassing situations by checking the data type of a variable beforehand. You can do this with the class() function, as the code on the right shows.
Complete the code in the editor and also print out the classes of my_character and my_logical.
示例:
# Declare variables of different types:
my_numeric <- 42
my_character <- "universe"
my_logical <- FALSE
# Check class of my_numeric
class(my_numeric)
## [1] "numeric"
## [1] "character"
## [1] "logical"