今天开始 继续学习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 <- 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
> 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?
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)
> 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)?
# 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
> # 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
# 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)
> 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