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

原创 2016年08月28日 15:54:46

其实学习R感觉还挺快的,可能是刚开始学习吧,前面都是一些基础,有点编程基础的可能会入门快一些

在(一)中, 我提到了DataCamp 其实官网上是提供啦,两种还一个是 try R 

http://tryr.codeschool.com/

这个网站我没有探索,如果大家觉得,DataCamp 用起来没有那么随心应手,大家可以尝试一下这个网站,估计也是挺不错的

好了,我继续把我的笔记( 贴出来吧),哦,对了,关于笔记我还想说一个问题,大家可能不会像我一样,写个博客啥的,那你的笔记,要想很是“坚挺”,就是不会因为系统废掉忘备份,或是手贱不小心删了等等,推荐大家使用印象云笔记,随时随地访问翻阅你的笔迹(有网),还支持导入导出(这一点很多云笔记都耍流氓),因为笔记对于我们来说就是财富!!!

好了,大家,考虑一下吧(绝逼不是打广告,我跟印象一毛钱关系都没有!!!)


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

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

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

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

####

Feelinglucky? You better, because this chapter takes you on a trip to the City of Sins, also knownas Statisticians Paradise!

Thanks toR and your new data-analytical skills, you will learn how to uplift yourperformance at the tables and fire off your career as a professional gambler.This chapter will show how you can easily keep track of your betting progressand how you can do some simple analyses on past actions. Next stop, VegasBaby... VEGAS!!

 

R语言严格

Do youstill remember what you have learned in the first chapter? Assign thevalue "Go!" to the variable vegas. Remember: R is casesensitive! 灵敏,敏感的

# Define the variable vegas
vegas <- "Go!"
vegas

console:
> # Define the variable vegas
> vegas <- "Go!"
> vegas
[1] "Go!"

Let usfocus first!

On yourway from rags(贫穷) to riches(富有), you will make extensive(广泛) use of vectors(向量).Vectors are one-dimension arrays that can hold numeric data, character data, orlogical data. In other words, a vector is a simple tool to store data. Forexample, you can store your daily gains and losses in the casinos.

In R, youcreate a vector with the combine function c(). Youplace the vector elements separated(分开)by a comma(逗号) between the parentheses(圆括号). For example:

numeric_vector<- c(1, 10, 49)

character_vector<- c("a", "b", "c")

# Complete the code for boolean_vector
boolean_vector <-c(TRUE, FALSE,TRUE)
console:

> numeric_vector <- c(1, 10, 49)
> character_vector <- c("a", "b", "c")
> 
> # Complete the code for boolean_vector
> boolean_vector <-c(TRUE, FALSE,TRUE)
>
输送向量数据:
# Poker winnings from Monday to Friday
poker_vector <- c(140, -50, 20, -120, 240)

# Roulette winnings from Monday to Friday
roulette_vector <-  c(-24, -50, 100, -350, 10)
console:
> # Poker winnings from Monday to Friday
> poker_vector <- c(140, -50, 20, -120, 240)
>
> # Roulette winnings from Monday to Friday
> roulette_vector <-  c(-24, -50, 100, -350, 10)
>

Naming avector

100xp

As a dataanalyst, it is important to have a clear view on the data that you are using.Understanding what each element refers to is therefore essential.

In theprevious exercise, we created a vector with your winnings over the week. Eachvector element refers to a day of the week but it is hard to tell which elementbelongs to which day. It would be nice if you could show that in the vectoritself.

You cangive a name to the elements of a vector with the names() function.Have a look at this example:

some_vector<- c("John Doe", "poker player")

names(some_vector)<- c("Name", "Profession")

names() 是一个函数 命名函数 将括号里的向量的元素进行 命名

This codefirst creates a vector some_vector and then gives the two elements aname. The first element is assigned the name Name, while the secondelement is labeled Profession. Printing the contents to the console yieldsfollowing output:

          Name     Profession

    "John Doe" "pokerplayer" 

# Poker winnings from Monday to Friday
poker_vector <- c(140, -50, 20, -120, 240)

# Roulette winnings from Monday to Friday
roulette_vector <- c(-24, -50, 100, -350, 10)

# Assign days as names of poker_vector
names(poker_vector) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")

# Assign days as names of roulette_vectors
names(roulette_vector) <- c("Monday", "Tuesday", "Wednesday", "Thursday","Friday")

console:
> # Poker winnings from Monday to Friday
> poker_vector <- c(140, -50, 20, -120, 240)
> 
> # Roulette winnings from Monday to Friday
> roulette_vector <- c(-24, -50, 100, -350, 10)
> 
> # Assign days as names of poker_vector
> names(poker_vector) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
> 
> # Assign days as names of roulette_vectors
> names(roulette_vector) <- c("Monday", "Tuesday", "Wednesday", "Thursday","Friday")

If youwant to become a good statistician, you have to become lazy.

如果你想成为一个合格的统计学家,你就必须得懒!!!! (哈哈哈 我确实懒 #_#+)

(If youare already lazy, chances are high you are one of those exceptional,natural-born statistical talents.)

In theprevious exercises you probably experienced that it is boring and frustratingto type and retype information such as the days of the week. However, when youlook at it from a higher perspective, there is a more efficient way to do this,namely, to assign the days of the week vector to avariable!

Just likeyou did with your poker and roulette returns, you can also create a variablethat contains the days of the week. This way you can use and re-use it.

# Poker winnings from Monday to Friday
poker_vector <- c(140, -50, 20, -120, 240)

# Roulette winnings from Monday to Friday
roulette_vector <- c(-24, -50, 100, -350, 10)

# The variable days_vector
days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")

# Assign the names of the day to roulette_vector and poker_vector
names(poker_vector) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
names(roulette_vector) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")

console:


> # Roulette winnings from Monday to Friday
> roulette_vector <- c(-24, -50, 100, -350, 10)
> 
> # The variable days_vector
> days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
>  
> # Assign the names of the day to roulette_vector and poker_vector
> names(poker_vector) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
> names(roulette_vector) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
>

教你偷懒的,反正我是复制粘贴啦 

官网提供的建议

Nice one!A word of advice: try to avoid code duplication at all times. 

      

Calculatingtotal winnings

100xp

Now thatyou have the poker and roulette winnings nicely as named vectors, you can startdoing some data analytical magic.

You wantto find out the following type of information:

How muchhas been your overall profit or loss per day of the week?

Have youlost money over the week in total?

Are youwinning/losing money on poker or on roulette?

To getthe answers, you have to do arithmetic calculations on vectors.

It isimportant to know is that if you sum two vectors in R, it takes theelement-wise sum. For example, the following three statements are completelyequivalent:

c(1, 2,3) + c(4, 5, 6)

c(1 + 4,2 + 5, 3 + 6)

c(5, 7,9)

You canalso do the calculations with variables that represent vectors:

a <- c(1, 2, 3) 
b <- c(4, 5, 6)
c <- a + b


 

A_vector <- c(1, 2, 3)
B_vector <- c(4, 5, 6)

# Take the sum of A_vector and B_vector
total_vector <- A_vector + B_vector

# Print out total_vector
total_vector


console:
> A_vector <- c(1, 2, 3)
> B_vector <- c(4, 5, 6)
> 
> # Take the sum of A_vector and B_vector
> total_vector <- A_vector + B_vector
>   
> # Print out total_vector
> total_vector
[1] 5 7 9
>

Calculatingtotal winnings (2)

100xp

Now youunderstand how R does arithmetic with vectors, it is time to get those Ferrarisin your garage! First, you need to understand what the overall profit or lossper day of the week was. The total daily profit is the sum of the profit/lossyou realized on poker per day, and the profit/loss you realized on roulette perday.

In R,this is just the sum of roulette_vector and poker_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

# Assign to total_daily how much you won/lost on each day
total_daily <- poker_vector + roulette_vector 
total_daily

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
> 
> # Assign to total_daily how much you won/lost on each day
> total_daily <- poker_vector + roulette_vector 
> total_daily
   Monday   Tuesday Wednesday  Thursday    Friday 
      116      -100       120      -470       250 
>

Calculatingtotal winnings (3)

100xp

Based onthe previous analysis, it looks like you had a mix of good and bad days. Thisis not what your ego expected, and you wonder if there may be a very tiny(细微) chance you have lost money over the week intotal?

Afunction that helps you to answer this question is sum().

Itcalculates the sum of all elements of a vector. 将会计算所有元素的和

Forexample, to calculate the total amount of money you have lost/won with pokeryou do:

total_poker <- sum(poker_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

# Total winnings with poker
total_poker <- sum(poker_vector)

# Total winnings with roulette
total_roulette <-  sum(roulette_vector)

# Total winnings overall
total_week <- total_poker + total_roulette

# Print out total_week
total_week

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
> 
> # Total winnings with poker
> total_poker <- sum(poker_vector)
> 
> # Total winnings with roulette
> total_roulette <-  sum(roulette_vector)
> 
> # Total winnings overall
> total_week <- total_poker + total_roulette
> 
> # Print out total_week
> total_week
[1] -84

Comparingtotal winnings

100xp

Oops, itseems like you are losing money. Time to rethink and adapt your strategy! Thiswill require some deeper analysis...

After ashort brainstorm(好主意) in your hotel's jacuzzi(浴缸), you realize that a possible explanationmight be that your skills in roulette are not as well developed as your skillsin poker. So maybe your total gains in poker are higher (or > )than in roulette(赌盘).

# 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

# Calculate total gains for poker and roulette
total_poker <- sum(poker_vector)
total_roulette <- sum(roulette_vector)

# Check if you realized higher total gains in poker than in roulette 
total_poker > total_roulette  直接返回布尔类型

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
> 
> # Calculate total gains for poker and roulette
> total_poker <- sum(poker_vector)
> total_roulette <- sum(roulette_vector)
> 
> # Check if you realized higher total gains in poker than in roulette 
> total_poker > total_roulette
[1] TRUE
>

Vectorselection: the good times

100xp

Yourhunch seemed to be right. It appears that the poker game is more your cup oftea than roulette.

Anotherpossible route for investigation is your performance at the beginning of theworking week compared to the end of it. You did have a couple of Margaritacocktails at the end of the week...

To answerthat question, you only want to focus on a selection of the total_vector.In other words, our goal is to select specific elements of the vector. Toselect elements of a vector (and later matrices, data frames, ...), you can usesquare brackets. Between the square brackets, you indicate what elements to select.For example, to select the first element of the vector, youtype poker_vector[1]. To select the second element of the vector, youtype poker_vector[2], etc.

Noticethat the first element in a vector has index 1, not 0 as in many otherprogramming languages.需要注意的是 是从1开始的,不是从0开始的

# 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_wednesday <- poker_vector[3]

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_wednesday <- poker_vector[3]
>

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























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

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

第1程序: Vector selection: the good times (2) How about analyzing your midweek results? To select m...
  • qq_26816591
  • qq_26816591
  • 2016年08月29日 19:48
  • 918

C++从入门到入坟

本文是关于c++的教程文章,本文的风格是,重要的事情决定不说三遍,不重要的事情说个百来遍也无妨,作为一个好老师,本人绝对不会谈那些用不到的,用得到的也不谈。...
  • qq_35158290
  • qq_35158290
  • 2017年09月02日 23:12
  • 656

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

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

R语言用RODBC连接oracle时报错“negative length vectors are not allowed”的解决方案记录

conn
  • huangjunli864
  • huangjunli864
  • 2017年01月07日 15:22
  • 573

从入门到放弃C语言-瞎倒腾(1)

请加公众号,周三周六定时更新 记得上完第一堂编程课以后,老师说,当你把一个语言学好以后,往往学习一个新的语言只需要一个星期。所以老师让我们用一个礼拜自学了C语言。 在这之后,接触了java...
  • search_way
  • search_way
  • 2016年08月14日 10:08
  • 968

C语言从入门到放弃

有人问我 你用C语言做过什么有意义的事情。 我自豪地告诉他 我用C语言考了一份不到60分的卷面成绩。     大一刚来校园一直在熟悉 交各种朋友。晃晃悠悠了一年  、   大二觉得无聊自己去开店,...
  • MXL1996
  • MXL1996
  • 2016年09月25日 13:01
  • 1879

C语言入门教程:c语言从入门到精通(2016最新版)

C语言入门教程地址:http://c.biancheng.net/cpp/u/jiaocheng/
  • hhsu28
  • hhsu28
  • 2016年10月25日 20:45
  • 777

R连接oracle数据库Error in odbcFetchRows negative length vectors are not allowed解决方法

问题: 解决方法:配置ODBC连接时,对于oracle和Sybase驱动需要设置参数believeNRows=FALSE 参数believeNRows说明:logical. Is the nu...
  • kancy110
  • kancy110
  • 2017年08月21日 23:24
  • 255

从入门到放弃C语言-入门篇(1)

想弄一份C语言的东西是因为被好几次问及怎样学C语言。        想说庆幸我刚学习的时候第一门语言不是C语言,如果那时候开头就学C语言我现在已经转去学习我最初的志愿数学或者经济学了。   ...
  • search_way
  • search_way
  • 2016年07月09日 10:34
  • 5947

ExtJs从入门到精通

  ExtJs从入门到精通(作成中)1、入门篇    ①窗口Window及对话框MessageBox      地址:http://www.myext.cn/Article/61.html     ②...
  • szwangdf
  • szwangdf
  • 2009年05月08日 12:26
  • 1629
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:R语言从基础入门到提高(二)Vectors(向量)
举报原因:
原因补充:

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