R语言从基础入门到提高（一）Intro to basics（基础介绍）

662人阅读 评论(0)

https://www.datacamp.com/home

####（中没有意识到！！！

####

# An addition
5 + 5

# A subtraction
5 - 5

# A multiplication
3 * 5

# A division
(5 + 5) / 2

# Exponentiation
2 ^ 5

# Modulo
28 %% 6

console：
> 5 + 5
[1] 10
>
> # A subtraction
> 5 - 5
[1] 0
>
> # A multiplication
> 3 * 5
[1] 15
>
>  # A division
> (5 + 5) / 2
[1] 5
>
> # Exponentiation
> 2 ^ 5
[1] 32
>
> # Modulo
> 28 %% 6
[1] 4

# Assign the value 42 to x
x <- 42

# Print out the value of the variable x
x
console：
> # Assign the value 42 to x
> x <- 42
>
> # Print out the value of the variable x
> x
[1] 42

# Assign the value 5 to the variable my_apples
my_apples <- 5

# Print out the value of the variable my_apples
my_apples

console：
> # 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
>

# 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

# Create the variable my_fruit
my_fruit <- my_oranges+my_apples
console：
> # 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
>
> # Create the variable my_fruit
> my_fruit <- my_oranges+my_apples
>

改错练习：
# 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

console：
> # 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
Error: non-numeric argument to binary operator
> my_fruit

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.
Take Hint (-30xp)
Incorrect submission
Your code contains an error that you should fix:
Error: non-numeric argument to binary operator
You can do this by setting the my_oranges variable to a numeric value, not a string!


基本数据类型：
Basic data types in R
100xp
R works with numerous data types. Some of the most basic types to get started are:
Decimals(小数，十进制数) 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.

# Change my_numeric to be 42
my_numeric <- 42

# Change my_character to be "universe"
my_character <- "universe"

# Change my_logical to be FALSE
my_logical <- FALSE

console：
> # Change my_numeric to be 42
> my_numeric <- 42
>
> # Change my_character to be "universe"
> my_character <- "universe"
>
> # Change my_logical to be FALSE
> my_logical <- FALSE


"数据"类型 检查 cheak
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.

# Declare variables of different types
my_numeric <- 42
my_character <- "universe"
my_logical <- FALSE

# Check class of my_numeric
class(my_numeric)

# Check class of my_character
class(my_character)

# Check class of my_logical
class(my_logical)

console:

> # Declare variables of different types
> my_numeric <- 42
> my_character <- "universe"
> my_logical <- FALSE
>
> # Check class of my_numeric
> class(my_numeric)
[1] "numeric"      小数&整数
>
> # Check class of my_character
> class(my_character)
[1] "character"       字符串
>
> # Check class of my_logical
> class(my_logical)
[1] "logical"      布尔
>


ps:一切内容均是本人总结提炼的，方式有网上查阅，翻阅书籍。如果设计版权希望能及时提醒更改。同时希望注重保护他人成果！

1
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：182364次
• 积分：3460
• 等级：
• 排名：第10075名
• 原创：173篇
• 转载：6篇
• 译文：1篇
• 评论：8条
My Site
个人网站
欢迎访问
博客专栏
 Java网络编程阅读笔记 文章：2篇 阅读：1959
 计算机网络 文章：2篇 阅读：2575
 R语言从基础入门到提高 文章：11篇 阅读：15869