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语言编程艺术学习笔记(快速入门、向量)

1.1.2 批处理模式
  • wowenfo
  • wowenfo
  • 2014年05月19日 12:33
  • 991

R语言入门——向量操作和赋值

向量操作和赋值: x x 可以对赋值的进行运算 可以将向量作为值赋给另一个向量 基本的算术运算符就是常用的+,-,*,/ 和做幂运算用的^。另...

R语言从基础入门到提高(四)matrices(矩阵)

第1程序 What's a matrix(矩阵)? 100xp In R, a matrix is a collection of elements of the same da...

R语言从基础入门到提高(五)factor 因子

第1程序 What's a factor and why would you use it? 100xp In this chapter you dive into the wo...

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

第1程序: Lists,why would you need them? 100xp Congratulations! At this point in thecourse you are alr...

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

其实之前我学习R语言,很是迷茫,不知道从何下手,想找一些视频,但是网上的视频不只少的可怜,而且还收费,对于开源的R来说就显得那么的悲剧啦,然后我想还是找一些书看一下吧,也不知道买什么书,还害怕买完后,...

R语言基础入门之一

1 什么是R语言 R语言是一个开源的数据分析环境,起初是由数位统计学家建立起来,以更好的进行统计计算和绘图,这篇wiki中包含了一些基本情况的介绍。由于R可以通过安装扩展包(Packages)而得到...

Thrust快速入门教程(二) —— Vectors

Thrust提供了两个vector容器:host_vector 与 device_vector。顾名思义,host_vector位于主机端,device_vector位于GPU设备端。Thrust的v...

R语言 ---- 基础入门

(一)向量 赋值: > x > x [1] 1 2 3 4 5 > y y [1] 1 2 3 4 5 6 7 8 > x[3] # 求向量x第3个元...
  • susht
  • susht
  • 2016年05月23日 22:08
  • 249
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:R语言从基础入门到提高(二)Vectors(向量)
举报原因:
原因补充:

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