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

13 篇文章 0 订阅
11 篇文章 0 订阅

其实之前我学习R语言,很是迷茫,不知道从何下手,想找一些视频,但是网上的视频不只少的可怜,而且还收费,对于开源的R来说就显得那么的悲剧啦,然后我想还是找一些书看一下吧,也不知道买什么书,还害怕买完后,不愿意看,然后就是PDF,找了一些PDF的书,单数吸收的没这么好,感觉都跨度挺大的,没有从基础语法讲起,即使讲了,也没那么清晰。

在一次查找ggplot 函数帮助时,发现Rstudio help 里居然有 learning R  在线的学习网站,抱着试试看的态度,就点进去看了看,然后感觉还是国外的有些良心网站,真正做到开源,free,所有的学习资源,都是free的。

这个网站是英文的,但是感觉单词都是很常见的英文词汇,不是问题,除非遇到是在不懂得词汇时,可是用一些翻译词典,本人用的是hai词。

好了下面就把这个网站贴出来

https://www.datacamp.com/home 

这个网站是交互式的,有在线编译器

然后就是我的学习笔记啦,里面有我的心得体会,包括全部运行代码,运行结果,注释等。

如果你不愿意,在线学习,可以跟着我的笔记学习,这样就省了,查单词,翻译,运行,琐事啦,但是本人强烈建议大家,一步步扎实的跟着网站,在线学习一下!!!

####(中没有意识到!!!

突然意识到一个问题,解读代码:

先是可能是一段介绍,有的简单的就没有介绍,然后就是贴出来的代码

代码包括:console为分界线,console之前为源代码(含注释),console为 运行结果

####


# An addition
5 + 5 

# A subtraction
5 - 5 

# A multiplication
3 * 5

 # A division
(5 + 5) / 2 

# Exponentiation
2 ^ 5

# Modulo
28 %% 6

console:
> # An addition
> 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
Error: object 'my_fruit' not found


提示:
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!
How helpful is this feedback?


然后 把six 改为6 就可以啦  不贴了!
基本数据类型:
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:一切内容均是本人总结提炼的,方式有网上查阅,翻阅书籍。如果设计版权希望能及时提醒更改。同时希望注重保护他人成果!



  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值