课程: R Code Challenges: Data Science

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Solution: Find the middle city

Solution: Find the middle city

- [Narrator] I like this challenge because it really points out who's thinking like an R programmer and who comes to this from a different programming language. Oftentimes people will try to solve the rock, paper, scissors, lizard, spock challenge with a long, long list of if else statements. R's strength is manipulating matrices, so I looked at how can I use a matrix to solve this problem? In the code you're looking at, you can see I've created a couple of things. The first thing I'll need is rock, paper, scissors, lizard, spock, a vector, with the words rock, paper, scissors, lizard, spock, and I do that in line one. I've already defined that 'cause I'll use that list several times. In line three, I set up the function(player1, player2). These are the choices that I'll pass into who won. Then in lines 6 through 15, I set up the truth table or a matrix that I'll use to test player1 against player2. Let's create that matrix and I will skip through lines 6, 7, 8, 9, 10, and then line…

内容