R语言从基础入门到提高(三)Vectors(向量)

原创 2016年08月29日 19:48:10
第1程序:
Vector selection: the good times (2)

How about analyzing your midweek results?

To select multiple(多种) elements from a vector, you can add square brackets at the end of it. You can indicate(表明) between the brackets what elements should be selected.
For example: suppose you want to select the first and the fifth day of the week: use the vector c(1, 5) between the square brackets. For example, the code below(下面) selects the first and fifth element of poker_vector:
poker_vector[c(1, 5)]
选择啦第1,5元素


要求:
Assign the poker results of Tuesday, Wednesday and Thursday to the variable poker_midweek.
源码:
# Poker and roulette winnings from Monday to Friday:
poker_vector <- c(140, -50, 20, -120, 240)
roulette_vector <- c(-24, -50, 100, -350, 10)
days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
names(poker_vector) <- days_vector
names(roulette_vector) <- days_vector

# Define a new variable based on a selection
poker_midweek <- poker_vector[c(2,3,4)]
console:
> # Poker and roulette winnings from Monday to Friday:
> poker_vector <- c(140, -50, 20, -120, 240)
> roulette_vector <- c(-24, -50, 100, -350, 10)
> days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
> names(poker_vector) <- days_vector
> names(roulette_vector) <- days_vector
> 
> # Define a new variable based on a selection
> poker_midweek <- poker_vector[c(2,3,4)]



第2程序:

Vector selection: the good times (3)

100xp

Selecting multiple elements of poker_vector with c(2, 3, 4) is not very convenient(方便). Many statisticians are lazy people by nature(天性), so they created an easier way to do this: c(2, 3, 4) can be abbreviated (简写)to2:4, which generates(引起) a vector with all natural numbers from 2 up to 4.

So, another way to find the mid-week results is poker_vector[2:4].
Notice how the vector 2:4 is placed between the square brackets to select element 2 up to 4.(这种写法是递增)

要求:
Assign to roulette_selection_vector the roulette(轮盘赌) results from Tuesday up to Friday; make use of : if it makes things easier for you.
源程序:

# Poker and roulette winnings from Monday to Friday:
poker_vector <- c(140, -50, 20, -120, 240)
roulette_vector <- c(-24, -50, 100, -350, 10)
days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
names(poker_vector) <- days_vector
names(roulette_vector) <- days_vector

# Define a new variable based on a selection
roulette_selection_vector <- roulette_vector[2:5]
console:
 # Poker and roulette winnings from Monday to Friday:
> poker_vector <- c(140, -50, 20, -120, 240)
> roulette_vector <- c(-24, -50, 100, -350, 10)
> days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
> names(poker_vector) <- days_vector
> names(roulette_vector) <- days_vector
> 
> # Define a new variable based on a selection
> roulette_selection_vector <- roulette_vector[2:5]



第3程序

Vector selection: the good times (4)

100xp

Another way to tackle(处理) the previous exercise is by using the names of the vector elements (Monday, Tuesday, ...) instead of their numeric positions. For example,

poker_vector["Monday"]

will select the first element of poker_vector since "Monday" is the name of that first element.

Just like you did in the previous exercise with numerics, you can also use the element names to select multiple elements, for example:

poker_vector[c("Monday","Tuesday")]
直接使用变量名进行调用,选中。
要求:
  • Select the first three(前3个) elements in poker_vector by using their names: "Monday""Tuesday" and "Wednesday". Assign the result of the selection to poker_start.
  • Calculate(计算) the average of the values in poker_start with the mean() function. Simply print out the result so you can inspect(检查) it.
源程序:
# Poker and roulette winnings from Monday to Friday:
poker_vector <- c(140, -50, 20, -120, 240)
roulette_vector <- c(-24, -50, 100, -350, 10)
days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
names(poker_vector) <- days_vector
names(roulette_vector) <- days_vector

# Select poker results for Monday, Tuesday and Wednesday
poker_start <- poker_vector[c("Monday","Tuesday","Wednesday")]

# Calculate the average of the elements in poker_start 直接计算平均数使用自带函数mean()
mean(poker_start) 

#mark#重点理解

console:
> # Poker and roulette winnings from Monday to Friday:
> poker_vector <- c(140, -50, 20, -120, 240)
> roulette_vector <- c(-24, -50, 100, -350, 10)
> days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
> names(poker_vector) <- days_vector
> names(roulette_vector) <- days_vector
> 
> # Select poker results for Monday, Tuesday and Wednesday
> poker_start <- poker_vector[c("Monday","Tuesday","Wednesday")]
>   
> # Calculate the average of the elements in poker_start
> mean(poker_start)
[1] 36.66667



第4程序

Selection by comparison - Step 1

100xp

By making use of comparison(比较) operators(操作符), we can approach(靠近) the previous question in a more proactive(先进) way.

The (logical) comparison operators known to R are:

  • < for less than  不到; 少于
  • > for greater than 大于
  • <= for less than or equal to 小于等于
  • >= for greater than or equal to 大于等于
  • == for equal to each other 等于
  • != not equal to each other 不等于

As seen in the previous chapter, stating 6 > 5 returns TRUE. The nice thing about R is that you can use these comparison operators also on vectors. For example:

> c(4, 5, 6) > 5
[1] FALSE FALSE TRUE
我的理解是 第一个不是 大于号,而是R的输入提示
This command tests for every element of the vector if the condition stated by the comparison operator is TRUE or FALSE.

要求:
  • Check which elements in poker_vector are positive(正数) (i.e. > 0) and assign this to selection_vector.
  • Print out selection_vector so you can inspect(验证) it. The printout tells you whether you won (TRUE) or lost (FALSE) any money for each day.

源代码:
# Poker and roulette winnings from Monday to Friday:
poker_vector <- c(140, -50, 20, -120, 240)
roulette_vector <- c(-24, -50, 100, -350, 10)
days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
names(poker_vector) <- days_vector
names(roulette_vector) <- days_vector

# Which days did you make money on poker?
selection_vector <- poker_vector[c(1,2,3,4,5)] > 0

#mark#重点理解
【刚开始时,我写的是
selection_vector <- poker_vector[c(1,2,3,4,5) > 0 ]
然后就出错啦,我没有选中向量元素就比较啦,只是选中啦向量中的下标(理解成下标吧)
】

# Print out selection_vector
selection_vector
console:
> # Poker and roulette winnings from Monday to Friday:
> poker_vector <- c(140, -50, 20, -120, 240)
> roulette_vector <- c(-24, -50, 100, -350, 10)
> days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
> names(poker_vector) <- days_vector
> names(roulette_vector) <- days_vector
> 
> # Which days did you make money on poker?
> selection_vector <- poker_vector[c(1,2,3,4,5)]>0
>   
> # Print out selection_vector
> selection_vector
   Monday   Tuesday Wednesday  Thursday    Friday 
     TRUE     FALSE      TRUE     FALSE      TRUE


第5程序

Selection by comparison - Step 2

100xp

Working with comparisons will make your data analytical life easier. Instead of selecting a subset(子集) of days to investigate(研究) yourself (like before), you can simply ask R to return only those days where you realized a positive return for poker.

In the previous exercises you used selection_vector <- poker_vector > 0 to find the days on which you had a positive poker return. Now, you would like to know not only the days on which you won, but also how much you won on those days.

You can select the desired(渴望的) elements, by putting selection_vectorbetween the square brackets that follow poker_vector:

poker_vector[selection_vector]
R knows what to do when you pass a logical vector in square brackets: it will only select the elements that correspond to(对应是) TRUE in selection_vector.
要求:
Use selection_vector in square brackets to assign the amounts(总额) that you won on the profitable(获利的) days to the variable poker_winning_days.

源程序:
# Poker and roulette winnings from Monday to Friday:
poker_vector <- c(140, -50, 20, -120, 240)
roulette_vector <- c(-24, -50, 100, -350, 10)
days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
names(poker_vector) <- days_vector
names(roulette_vector) <- days_vector

# Which days did you make money on poker?
selection_vector <- poker_vector > 0
#选中获利的那些天,即poker_vector 表示所有元素,大于0的
# Select from poker_vector these days

#mark#重点理解

poker_winning_days <- poker_vector[selection_vector]
#将获利的那些天的获利额赋值给poker_winning_days
poker_winning_days
#打印

console:
> # Poker and roulette winnings from Monday to Friday:
> poker_vector <- c(140, -50, 20, -120, 240)
> roulette_vector <- c(-24, -50, 100, -350, 10)
> days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
> names(poker_vector) <- days_vector
> names(roulette_vector) <- days_vector
> 
> # Which days did you make money on poker?
> selection_vector <- poker_vector > 0
> 
> # Select from poker_vector these days
> poker_winning_days <- poker_vector[selection_vector]
> poker_winning_days
   Monday Wednesday    Friday 
      140        20       240



第6程序

Advanced selection

100xp
Just like you did for poker, you also want to know those days where you realized a positive return for roulette.
要求:
  • Create the variable selection_vector, this time to see if you made profit with roulette for different days.
  • Assign the amounts that you made on the days that you ended positively for roulette to the variable roulette_winning_days. This vector thus contains the positive winnings of roulette_vector.

源程序:
# Poker and roulette winnings from Monday to Friday:
poker_vector <- c(140, -50, 20, -120, 240)
roulette_vector <- c(-24, -50, 100, -350, 10)
days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
names(poker_vector) <- days_vector
names(roulette_vector) <- days_vector

# Which days did you make money on roulette?
selection_vector <- roulette_vector > 0

# Select from roulette_vector these days
roulette_winning_days <- roulette_vector[selection_vector]

console:

> # Poker and roulette winnings from Monday to Friday:
> poker_vector <- c(140, -50, 20, -120, 240)
> roulette_vector <- c(-24, -50, 100, -350, 10)
> days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
> names(poker_vector) <- days_vector
> names(roulette_vector) <- days_vector
> 
> # Which days did you make money on roulette?
> selection_vector <- roulette_vector > 0
> 
> # Select from roulette_vector these days
> roulette_winning_days <- roulette_vector[selection_vector]

这一章又完成啦。















版权声明:本文为博主原创文章,未经博主允许不得转载。不准各种形式的复制及盗图!!!

R语言从基础入门到提高(二)Vectors(向量)

其实学习R感觉还挺快的,可能是刚开始学习吧,前面都是一些基础,有点编程基础的可能会入门快一些 在(一)中, 我提到了DataCamp 其实官网上是提供啦,两种还一个是 try R  http://tr...
  • qq_26816591
  • qq_26816591
  • 2016年08月28日 15:54
  • 1120

R语言基础编程技巧汇编 - 1

1.      建立多维数组 array(1:36, dim = c(2, 2, 3, 3)) 2.      使用list.files函数遍历文件夹中的文件 比如列出当前工作目录下的所有文件 lis...
  • liu7788414
  • liu7788414
  • 2015年03月19日 17:56
  • 1633

java语言基础入门——各类流的介绍

1、什么是流?流是一个抽象的概念,代表一串数据集合,当java程序需要从数据源读取数据时,就开启了一个到数据源的流,同样,当数据需要输出数据到达目的地时,也需要开启一个流。流是用来处理数据的通道。流有...
  • qq_29672495
  • qq_29672495
  • 2016年08月05日 17:56
  • 1545

R语言中向量的加法和乘法

在R语言中,不同长度的向量也是可以相加和相乘的,乘法的规则和加法类似 1,相同长度的向量相加 > x y z z [1] 2 4 6 8 规则就是 x[1]+y[1],x[2]+y[2],x[3]+...
  • hongjinlongno1
  • hongjinlongno1
  • 2016年08月20日 20:47
  • 4632

R语言-向量构造 及 函数构造

1,生成向量的方法 (1) seq()函数 > x=seq(from=1, to=5, by=0.5) > x # [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 ...
  • ytadx
  • ytadx
  • 2015年01月09日 13:58
  • 1847

R语言从基础入门到提高(七)LIST (列表)

第1程序: Lists,why would you need them? 100xp Congratulations! At this point in thecourse you are alr...
  • qq_26816591
  • qq_26816591
  • 2016年09月03日 19:08
  • 4767

R语言学习笔记,(1)基础及向量操作

一、基础部分(持续更新) 1、大小写明感 2、source(“d:/test.R”):可以执行对应目录下得test.R脚本。R中\符号为转义字符,所有目录中需使用/或者\\. 3、sink(“d:/o...
  • kgzhw
  • kgzhw
  • 2017年04月07日 22:48
  • 327

R语言中使用支持向量机

R语言中使用支持向量机 Including the SVM package The SVM package is in a package called "e1071." Firt you n...
  • cleverbegin
  • cleverbegin
  • 2014年11月30日 15:57
  • 2533

R语言高级算法之支持向量机(Support Vector Machine)

1.支持向量机原理解析 机器学习本质上就是一种对所研究问题真实模型的逼近,通常会假设一个近似模型,然后根据适当的原理将这个近似模型不断逼近真实模型.结构风险就是指近似模型与真实模型之间的差距. 我...
  • claroja
  • claroja
  • 2017年02月15日 13:41
  • 1103

R语言统计应用(一) —— 基础概述

在数据挖掘中,统计学习方法常常用到R语言,因此,我们后面将对R语言在统计学习中的应用进行专题介绍,方便从理论上对统计学习有进一步深入的认识。...
  • lipengcn
  • lipengcn
  • 2016年04月27日 09:56
  • 1128
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:R语言从基础入门到提高(三)Vectors(向量)
举报原因:
原因补充:

(最多只允许输入30个字)