# R语言从基础入门到提高（二）Vectors（向量）

973人阅读 评论(0)

http://tryr.codeschool.com/

####（中没有意识到！！！

####

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:一切内容均是本人总结提炼的，方式有网上查阅，翻阅书籍。如果设计版权希望能及时提醒更改。同时希望注重保护他人成果！

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：168347次
• 积分：3328
• 等级：
• 排名：第10520名
• 原创：173篇
• 转载：6篇
• 译文：1篇
• 评论：6条
My Site
个人网站
欢迎访问
博客专栏
 Java网络编程阅读笔记 文章：2篇 阅读：1906
 计算机网络 文章：2篇 阅读：2505
 R语言从基础入门到提高 文章：11篇 阅读：14501