R 学习笔记( 2 )

今天开始 继续学习R ,进入Intermediate(中级) R部分

这次是运算符部分
1.!=  和 == 同样适用于R里面

2.TRUE ==1 , FALSE == 0

3.对于数组中的数同样可以使用运算符 ,具体用法如下:
# The linkedin and facebook vectors have already been created for you
linkedin <- c(16, 9, 13, 5, 2, 17, 14)
facebook <- c(17, 7, 5, 16, 8, 13, 14)

# Popular days
linkedin > 15

# Quiet days
linkedin <= 5

# LinkedIn more popular than Facebook
linkedin > facebook

--------------------------------------------------------->
> # Popular days
> linkedin > 15
[1]  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE
> # Quiet days
> linkedin <= 5
[1] FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE
> # LinkedIn more popular than Facebook
> linkedin > facebook
[1] FALSE  TRUE  TRUE FALSE FALSE  TRUE FALSE

4.运算符的使用很广泛 , 如下: (就是可以通过运算符给出你想要的结果,如 “输出向量里大于5的点”)
# The social data has been created for you
linkedin <- c(16, 9, 13, 5, 2, 17, 14)
facebook <- c(17, 7, 5, 16, 8, 13, 14)
views <- matrix(c(linkedin, facebook), nrow = 2, byrow = TRUE)

# When does views equal 13?
views == 13

# When is views less than or equal to 14?
views <= 14

# How often does facebook equal or exceed linkedin times two?
sum(facebook - linkedin > 2)

--------------------------------------------------------->
> # The social data has been created for you
> linkedin <- c(16, 9, 13, 5, 2, 17, 14)
> facebook <- c(17, 7, 5, 16, 8, 13, 14)
> views <- matrix(c(linkedin, facebook), nrow = 2, byrow = TRUE)
> # When does views equal 13?
> views == 13
      [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]
[1,] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
[2,] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
> # When is views less than or equal to 14?
> views <= 14
      [,1] [,2] [,3]  [,4] [,5]  [,6] [,7]
[1,] FALSE TRUE TRUE  TRUE TRUE FALSE TRUE
[2,] FALSE TRUE TRUE FALSE TRUE  TRUE TRUE
> # How often does facebook equal or exceed linkedin times two?
> sum(facebook - linkedin > 2)
[1] 2

5.逻辑运算符

与 &  或| 非!


与之前学过的相似 用法都一样
# The social data (linkedin, facebook, views) has been created for you

# linkedin exceeds 10 but facebook below 10
linkedin > 10 & facebook <10

# When were one or both visited at least 12 times?
linkedin >=12 | facebook >=12

# When is views between 11 (exclusive) and 14 (inclusive)?
views >11 & views <=14

--------------------->
> # The social data (linkedin, facebook, views) has been created for you
> # linkedin exceeds 10 but facebook below 10
> linkedin > 10 & facebook < 10
[1] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
> # When were one or both visited at least 12 times?
> # When is views between 11 (exclusive) and 14 (inclusive)?
> # The social data (linkedin, facebook, views) has been created for you
> # linkedin exceeds 10 but facebook below 10
> linkedin > 10 & facebook < 10
[1] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
> # When were one or both visited at least 12 times?
> linkedin >= 12 | facebook >= 12
[1]  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE
> # When is views between 11 (exclusive) and 14 (inclusive)?
> views > 11 & views <= 14
      [,1]  [,2]  [,3]  [,4]  [,5]  [,6] [,7]
[1,] FALSE FALSE  TRUE FALSE FALSE FALSE TRUE
[2,] FALSE FALSE FALSE FALSE FALSE  TRUE TRUE

用法之一
# li_df is pre-loaded in your workspace

# Select the second column, named day2, from li_df: second
second <- li_df[ ,2]
second

# Build a logical vector, TRUE if value in second is extreme: extremes

extremes <- (second >25 | second <5 )

# Count the number of TRUEs in extremes
sum(extremes)
-------------------------------------->

> # Select the second column, named day2, from li_df: second
> second <- li_df[, 2]
> second
 [1]  3 23 18 18 25 20  4  3 22 12 27 13 17 27  6 35 17  6  1 12 15 17 12  8  7
[26] 25 15 32 29  1 22 11  5 17 12 26 13 10 37 33 19 29  8 22 10 19 27 18 15 28
> # Build a logical vector, TRUE if value in second is extreme: extremes
> extremes <- (second > 25 | second < 5)
> # Count the number of TRUEs in extremes
> sum(extremes)
[1] 16
 



条件声明
和C,JAVA一样 if ;else ; if else
                                                     ----11/27

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值