Equality 等效逻辑判断

在R里双等号==用来判别其左右两边的语句是否等价,如果等价,返回逻辑值TRUE, 否则返回FALSE。请随意尝试一下下面的例子:

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIENvbXBhcmlzb24gb2YgbG9naWNhbHNcblxuXG4jIENvbXBhcmlzb24gb2YgbnVtZXJpY3NcblxuXG4jIENvbXBhcmlzb24gb2YgY2hhcmFjdGVyIHN0cmluZ3NcblxuXG4jIENvbXBhcmUgYSBsb2dpY2FsIHdpdGggYSBudW1lcmljIiwic29sdXRpb24iOiIjIENvbXBhcmlzb24gb2YgbG9naWNhbHNcblRSVUUgPT0gRkFMU0VcblxuIyBDb21wYXJpc29uIG9mIG51bWVyaWNzXG4tNiAqIDE0ICE9IDE3IC0gMTAxXG5cbiMgQ29tcGFyaXNvbiBvZiBjaGFyYWN0ZXIgc3RyaW5nc1xuXCJ1c2VSXCIgPT0gXCJ1c2VyXCJcblxuIyBDb21wYXJlIGEgbG9naWNhbCB3aXRoIGEgbnVtZXJpY1xuVFJVRSA9PSAxIn0=

因为 TRUE 在底层被赋值为1, 所以TRUE == 1 被判断为 TRUE. 请注意不要=== 傻傻分不清楚哈。一个是比较,一个是赋值,虽然我们在R里面通常用 <- 来赋值。

Greater and less than 大于和小于

这里我们介绍大于>和小于 <, 你可以添加= 来表示 大于等于或者小于等于。看看下面的例子,脑补一下出来的逻辑判断是什么? 哈哈,都是 FALSE:

Remember that for string comparison, R determines the greater than relationship based on alphabetical order. Also, keep in mind that TRUE corresponds to 1 in R, and FALSE coerces to 0 behind the scenes. Therefore, FALSE < TRUE is TRUE.

INSTRUCTIONS 书写下面语句的代码表达:

HINT A correct answer to the second instruction would be: “raining” <= “raining dogs”

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIENvbXBhcmlzb24gb2YgbnVtZXJpY3NcblxuXG4jIENvbXBhcmlzb24gb2YgY2hhcmFjdGVyIHN0cmluZ3NcblxuXG4jIENvbXBhcmlzb24gb2YgbG9naWNhbHMiLCJzb2x1dGlvbiI6IiMgQ29tcGFyaXNvbiBvZiBudW1lcmljc1xuLTYgKiA1ICsgMiA+PSAtMTAgKyAxXG5cbiMgQ29tcGFyaXNvbiBvZiBjaGFyYWN0ZXIgc3RyaW5nc1xuXCJyYWluaW5nXCIgPD0gXCJyYWluaW5nIGRvZ3NcIlxuXG4jIENvbXBhcmlzb24gb2YgbG9naWNhbHNcblRSVUUgPiBGQUxTRSJ9

Compare vectors

You are already aware that R is very good with vectors. Without having to change anything about the syntax, R’s relational operators also work on vectors.

Let’s go back to the example that was started in the video. You want to figure out whether your activity on social media platforms have paid off and decide to look at your results for LinkedIn and Facebook. The sample code in the editor initializes the vectors linkedin and facebook. Each of the vectors contains the number of profile views your LinkedIn and Facebook profiles had over the last seven days.

INSTRUCTIONS

Using relational operators, find a logical answer, i.e. TRUE or FALSE, for the following questions:

HINT Let’s get you up to speed with a comparable example. To see when your LinkedIn profile was viewed more than 10 times, use:

linkedin > 10

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFRoZSBsaW5rZWRpbiBhbmQgZmFjZWJvb2sgdmVjdG9ycyBoYXZlIGFscmVhZHkgYmVlbiBjcmVhdGVkIGZvciB5b3VcbmxpbmtlZGluIDwtIGMoMTYsIDksIDEzLCA1LCAyLCAxNywgMTQpXG5mYWNlYm9vayA8LSBjKDE3LCA3LCA1LCAxNiwgOCwgMTMsIDE0KVxuXG4jIFBvcHVsYXIgZGF5c1xuXG5cbiMgUXVpZXQgZGF5c1xuXG5cbiMgTGlua2VkSW4gbW9yZSBwb3B1bGFyIHRoYW4gRmFjZWJvb2siLCJzb2x1dGlvbiI6IiMgVGhlIGxpbmtlZGluIGFuZCBmYWNlYm9vayB2ZWN0b3JzIGhhdmUgYWxyZWFkeSBiZWVuIGNyZWF0ZWQgZm9yIHlvdVxubGlua2VkaW4gPC0gYygxNiwgOSwgMTMsIDUsIDIsIDE3LCAxNClcbmZhY2Vib29rIDwtIGMoMTcsIDcsIDUsIDE2LCA4LCAxMywgMTQpXG5cbiMgUG9wdWxhciBkYXlzXG5saW5rZWRpbiA+IDE1XG5cbiMgUXVpZXQgZGF5c1xubGlua2VkaW4gPD0gNVxuXG4jIExpbmtlZEluIG1vcmUgcG9wdWxhciB0aGFuIEZhY2Vib29rXG5saW5rZWRpbiA+IGZhY2Vib29rIn0=

Compare matrices

R’s ability to deal with different data structures for comparisons does not stop at vectors. Matrices and relational operators also work together seamlessly!

Instead of in vectors (as in the previous exercise), the LinkedIn and Facebook data is now stored in a matrix called views. The first row contains the LinkedIn information; the second row the Facebook information. The original vectors facebook and linkedin are still available as well.

INSTRUCTIONS:

Using the relational operators you’ve learned so far, try to discover the following:

HINT To see when views equals 13, you can use the == operator, just like you did for vectors!

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFRoZSBzb2NpYWwgZGF0YSBoYXMgYmVlbiBjcmVhdGVkIGZvciB5b3VcbmxpbmtlZGluIDwtIGMoMTYsIDksIDEzLCA1LCAyLCAxNywgMTQpXG5mYWNlYm9vayA8LSBjKDE3LCA3LCA1LCAxNiwgOCwgMTMsIDE0KVxudmlld3MgPC0gbWF0cml4KGMobGlua2VkaW4sIGZhY2Vib29rKSwgbnJvdyA9IDIsIGJ5cm93ID0gVFJVRSlcblxuIyBXaGVuIGRvZXMgdmlld3MgZXF1YWwgMTM/XG5cblxuIyBXaGVuIGlzIHZpZXdzIGxlc3MgdGhhbiBvciBlcXVhbCB0byAxND8iLCJzb2x1dGlvbiI6IiMgVGhlIHNvY2lhbCBkYXRhIGhhcyBiZWVuIGNyZWF0ZWQgZm9yIHlvdVxubGlua2VkaW4gPC0gYygxNiwgOSwgMTMsIDUsIDIsIDE3LCAxNClcbmZhY2Vib29rIDwtIGMoMTcsIDcsIDUsIDE2LCA4LCAxMywgMTQpXG52aWV3cyA8LSBtYXRyaXgoYyhsaW5rZWRpbiwgZmFjZWJvb2spLCBucm93ID0gMiwgYnlyb3cgPSBUUlVFKVxuXG4jIFdoZW4gZG9lcyB2aWV3cyBlcXVhbCAxMz9cbnZpZXdzID09IDEzXG5cbiMgV2hlbiBpcyB2aWV3cyBsZXNzIHRoYW4gb3IgZXF1YWwgdG8gMTQ/XG52aWV3cyA8PSAxNCJ9

This exercise concludes the part on comparators. Now that you know how to query the relation between R objects, the next step will be to use the results to alter the behavior of your programs.

& and |

Before you work your way through the next exercises, have a look at the following R expressions. All of them will evaluate to TRUE:

In this exercise, you’ll be working with the last variable. This variable equals the last value of the linkedin vector that you’ve worked with previously. The linkedin vector represents the number of LinkedIn views your profile had in the last seven days, remember? Both the variables linkedin and last have already been defined in the editor.

INSTRUCTIONS:

Write R expressions to solve the following questions concerning the variable last:

HINT:

In the last instruction, you should use the & operator twice and the | operator once. Use parentheses to make sure that the order of execution is correct!

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFRoZSBsaW5rZWRpbiBhbmQgbGFzdCB2YXJpYWJsZSBhcmUgYWxyZWFkeSBkZWZpbmVkIGZvciB5b3VcbmxpbmtlZGluIDwtIGMoMTYsIDksIDEzLCA1LCAyLCAxNywgMTQpXG5sYXN0IDwtIHRhaWwobGlua2VkaW4sIDEpXG5cbiMgSXMgbGFzdCB1bmRlciA1IG9yIGFib3ZlIDEwP1xuXG5cbiMgSXMgbGFzdCBiZXR3ZWVuIDE1IChleGNsdXNpdmUpIGFuZCAyMCAoaW5jbHVzaXZlKT8iLCJzb2x1dGlvbiI6IiMgVGhlIGxpbmtlZGluIGFuZCBsYXN0IHZhcmlhYmxlIGFyZSBhbHJlYWR5IGRlZmluZWQgZm9yIHlvdVxubGlua2VkaW4gPC0gYygxNiwgOSwgMTMsIDUsIDIsIDE3LCAxNClcbmxhc3QgPC0gdGFpbChsaW5rZWRpbiwgMSlcblxuIyBJcyBsYXN0IHVuZGVyIDUgb3IgYWJvdmUgMTA/XG5sYXN0IDwgNSB8IGxhc3QgPiAxMFxuXG4jIElzIGxhc3QgYmV0d2VlbiAxNSAoZXhjbHVzaXZlKSBhbmQgMjAgKGluY2x1c2l2ZSk/XG5sYXN0ID4gMTUgJiBsYXN0IDw9IDIwIn0=

Like relational operators, logical operators work perfectly fine with vectors and matrices.

Both the vectors linkedin and facebook are available again. Also a matrix - views - has been defined; its first and second row correspond to the linkedin and facebook vectors, respectively. Ready for some advanced queries to gain more insights into your social outreach?

INSTRUCTIONS:

When did LinkedIn views exceed 10 and did Facebook views fail to reach 10 for a particular day? Use the linkedin and facebook vectors. When were one or both of your LinkedIn and Facebook profiles visited at least 12 times? When is the views matrix equal to a number between 11 and 14, excluding 11 and including 14?

HINT: Be sure to using a single & and a single | in these exercises! && and || will only examine the first element of your vector or matrix.

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImxpbmtlZGluIDwtIGMoMTYsIDksIDEzLCA1LCAyLCAxNywgMTQpXG5mYWNlYm9vayA8LSBjKDE3LCA3LCA1LCAxNiwgOCwgMTMsIDE0KVxudmlld3MgPC0gbWF0cml4KGMobGlua2VkaW4sIGZhY2Vib29rKSwgbnJvdyA9IDIsIGJ5cm93ID0gVFJVRSkiLCJzYW1wbGUiOiIjIFRoZSBzb2NpYWwgZGF0YSAobGlua2VkaW4sIGZhY2Vib29rLCB2aWV3cykgaGFzIGJlZW4gY3JlYXRlZCBmb3IgeW91XG5cbiMgbGlua2VkaW4gZXhjZWVkcyAxMCBidXQgZmFjZWJvb2sgYmVsb3cgMTBcblxuXG4jIFdoZW4gd2VyZSBvbmUgb3IgYm90aCB2aXNpdGVkIGF0IGxlYXN0IDEyIHRpbWVzP1xuXG5cbiMgV2hlbiBpcyB2aWV3cyBiZXR3ZWVuIDExIChleGNsdXNpdmUpIGFuZCAxNCAoaW5jbHVzaXZlKT8iLCJzb2x1dGlvbiI6IiMgVGhlIHNvY2lhbCBkYXRhIChsaW5rZWRpbiwgZmFjZWJvb2ssIHZpZXdzKSBoYXMgYmVlbiBjcmVhdGVkIGZvciB5b3VcblxuIyBsaW5rZWRpbiBleGNlZWRzIDEwIGJ1dCBmYWNlYm9vayBiZWxvdyAxMFxubGlua2VkaW4gPiAxMCAmIGZhY2Vib29rIDwgMTBcblxuIyBXaGVuIHdlcmUgb25lIG9yIGJvdGggdmlzaXRlZCBhdCBsZWFzdCAxMiB0aW1lcz9cbmxpbmtlZGluID49IDEyIHwgZmFjZWJvb2sgPj0gMTJcblxuIyBXaGVuIGlzIHZpZXdzIGJldHdlZW4gMTEgKGV4Y2x1c2l2ZSkgYW5kIDE0IChpbmNsdXNpdmUpP1xudmlld3MgPiAxMSAmIHZpZXdzIDw9IDE0In0=

You’ll have noticed how easy it is to use logical operators to vectors and matrices. What do these results tell us? The third day of the recordings was the only day where your LinkedIn profile was visited more than 10 times, while your Facebook profile wasn’t. Can you draw similar conclusions for the other results?

Reverse the result: ! On top of the & and | operators, you also learned about the ! operator, which negates a logical value. To refresh your memory, here are some R expressions that use !. They all evaluate to FALSE:

Blend it all together

With the things you’ve learned by now, you’re able to solve pretty cool problems. 需要构建数据集才能work

Instead of recording the number of views for your own LinkedIn profile, suppose you conducted a survey inside the company you’re working for. You’ve asked every employee with a LinkedIn profile how many visits their profile has had over the past seven days. You stored the results in a data frame called li_df. This data frame is available in the workspace; type li_df in the console to check it out.

INSTRUCTIONS: Select the entire second column, named day2, from the li_df data frame as a vector and assign it to second. Use second to create a logical vector, that contains TRUE if the corresponding number of views is strictly greater than 25 or strictly lower than 5 and FALSE otherwise. Store this logical vector as extremes. Use sum() on the extremes vector to calculate the number of TRUEs in extremes (i.e. to calculate the number of employees that are either very popular or very low-profile). Simply print this number to the console.

HINT: You can select the data for the second day with li_df$day2. Use a combination of relational and logical operators for the second instruction: >, < and |. sum(extremes) will count all the TRUEs in extremes.

HINT: You can select the data for the second day with li_df$day2. Use a combination of relational and logical operators for the second instruction: >, < and |. sum(extremes) will count all the TRUEs in extremes.

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImxpX2RmPC1yZWFkLmNzdignaHR0cHM6Ly93d3cuZHJvcGJveC5jb20vcy81cjgyN3FtcHdmNjN6NnQvbGlfZGYuY3N2P2RsPTEnLCBoZWFkZXIgPSBUUlVFKSIsInNhbXBsZSI6IiMgbGlfZGYgaXMgcHJlLWxvYWRlZCBpbiB5b3VyIHdvcmtzcGFjZVxuXG4jIFNlbGVjdCB0aGUgc2Vjb25kIGNvbHVtbiwgbmFtZWQgZGF5MiwgZnJvbSBsaV9kZjogc2Vjb25kXG5cblxuIyBCdWlsZCBhIGxvZ2ljYWwgdmVjdG9yLCBUUlVFIGlmIHZhbHVlIGluIHNlY29uZCBpcyBleHRyZW1lOiBleHRyZW1lc1xuXG5cbiMgQ291bnQgdGhlIG51bWJlciBvZiBUUlVFcyBpbiBleHRyZW1lc1xuXG5cbiMgU29sdmUgaXQgd2l0aCBhIG9uZS1saW5lciIsInNvbHV0aW9uIjoiIyBsaV9kZiBpcyBwcmUtbG9hZGVkIGluIHlvdXIgd29ya3NwYWNlXG5cbiMgU2VsZWN0IHRoZSBzZWNvbmQgY29sdW1uLCBuYW1lZCBkYXkyLCBmcm9tIGxpX2RmOiBzZWNvbmRcbnNlY29uZCA8LSBsaV9kZiRkYXkyXG5cbiMgQnVpbGQgYSBsb2dpY2FsIHZlY3RvciwgVFJVRSBpZiB2YWx1ZSBpbiBzZWNvbmQgaXMgZXh0cmVtZTogZXh0cmVtZXNcbmV4dHJlbWVzIDwtIHNlY29uZCA+IDI1IHwgc2Vjb25kIDwgNVxuXG4jIENvdW50IHRoZSBudW1iZXIgb2YgVFJVRXMgaW4gZXh0cmVtZXNcbnN1bShleHRyZW1lcylcblxuIyBTb2x2ZSBpdCB3aXRoIGEgb25lLWxpbmVyXG5zdW0obGlfZGYkZGF5MiA+IDI1IHwgbGlfZGYkZGF5MiA8IDUpIn0=

The if statement 条件语句

Before diving into some exercises on the if statement, have another look at its syntax:

if (condition) { expr } Remember your vectors with social profile views? Let’s look at it from another angle. The medium variable gives information about the social website; the num_views variable denotes the actual number of views that particular medium had on the last day of your recordings. Both these variables have already been defined in the editor.

INSTRUCTIONS: Examine the if statement that prints out “Showing LinkedIn information” if the medium variable equals “LinkedIn”. Code an if statement that prints “You’re popular!” to the console if the num_views variable exceeds 15.

HINT Use the print() function to print things to the console output. Have another look at the video if you are not sure how to write your own if statements.

HINT Use the print() function to print things to the console output. Have another look at the video if you are not sure how to write your own if statements.
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFZhcmlhYmxlcyByZWxhdGVkIHRvIHlvdXIgbGFzdCBkYXkgb2YgcmVjb3JkaW5nc1xubWVkaXVtIDwtIFwiTGlua2VkSW5cIlxubnVtX3ZpZXdzIDwtIDE0XG5cbiMgRXhhbWluZSB0aGUgaWYgc3RhdGVtZW50IGZvciBtZWRpdW1cbmlmIChtZWRpdW0gPT0gXCJMaW5rZWRJblwiKSB7XG5wcmludChcIlNob3dpbmcgTGlua2VkSW4gaW5mb3JtYXRpb25cIilcbn1cblxuIyBXcml0ZSB0aGUgaWYgc3RhdGVtZW50IGZvciBudW1fdmlld3MiLCJzb2x1dGlvbiI6IiMgVmFyaWFibGVzIHJlbGF0ZWQgdG8geW91ciBsYXN0IGRheSBvZiByZWNvcmRpbmdzXG5tZWRpdW0gPC0gXCJMaW5rZWRJblwiXG5udW1fdmlld3MgPC0gMTRcblxuIyBFeGFtaW5lIHRoZSBpZiBzdGF0ZW1lbnQgZm9yIG1lZGl1bVxuaWYgKG1lZGl1bSA9PSBcIkxpbmtlZEluXCIpIHtcbnByaW50KFwiU2hvd2luZyBMaW5rZWRJbiBpbmZvcm1hdGlvblwiKVxufVxuXG4jIFdyaXRlIHRoZSBpZiBzdGF0ZW1lbnQgZm9yIG51bV92aWV3c1xuaWYgKG51bV92aWV3cyA+IDE1KSB7XG5wcmludChcIllvdSdyZSBwb3B1bGFyIVwiKVxufSJ9

Try to see what happens if you change the medium and num_views variables and run your code again. Let’s further customize these if statements in the next exercise.

Add an else

You can only use an else statement in combination with an if statement. The else statement does not require a condition; its corresponding code is simply run if all of the preceding conditions in the control structure are FALSE. Here’s a recipe for its usage:

if (condition) { expr1 } else { expr2 } It’s important that the else keyword comes on the same line as the closing bracket of the if part!

Both if statements that you coded in the previous exercises are already available in the editor. It’s now up to you to extend them with the appropriate else statements!

INSTRUCTIONS: Add an else statement to both control structures, such that

HINT To add an else statement, change:

if () { } to

if () { } else { } Up to you to fill in the !
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFZhcmlhYmxlcyByZWxhdGVkIHRvIHlvdXIgbGFzdCBkYXkgb2YgcmVjb3JkaW5nc1xubWVkaXVtIDwtIFwiTGlua2VkSW5cIlxubnVtX3ZpZXdzIDwtIDE0XG5cbiMgQ29udHJvbCBzdHJ1Y3R1cmUgZm9yIG1lZGl1bVxuaWYgKG1lZGl1bSA9PSBcIkxpbmtlZEluXCIpIHtcbnByaW50KFwiU2hvd2luZyBMaW5rZWRJbiBpbmZvcm1hdGlvblwiKVxufVxuXG5cblxuIyBDb250cm9sIHN0cnVjdHVyZSBmb3IgbnVtX3ZpZXdzXG5pZiAobnVtX3ZpZXdzID4gMTUpIHtcbnByaW50KFwiWW91J3JlIHBvcHVsYXIhXCIpXG59Iiwic29sdXRpb24iOiIjIFZhcmlhYmxlcyByZWxhdGVkIHRvIHlvdXIgbGFzdCBkYXkgb2YgcmVjb3JkaW5nc1xubWVkaXVtIDwtIFwiTGlua2VkSW5cIlxubnVtX3ZpZXdzIDwtIDE0XG5cbiMgQ29udHJvbCBzdHJ1Y3R1cmUgZm9yIG1lZGl1bVxuaWYgKG1lZGl1bSA9PSBcIkxpbmtlZEluXCIpIHtcbnByaW50KFwiU2hvd2luZyBMaW5rZWRJbiBpbmZvcm1hdGlvblwiKVxufSBlbHNle1xucHJpbnQoXCJVbmtub3duIG1lZGl1bVwiKVxufVxuXG4jIENvbnRyb2wgc3RydWN0dXJlIGZvciBudW1fdmlld3NcbmlmIChudW1fdmlld3MgPiAxNSkge1xucHJpbnQoXCJZb3UncmUgcG9wdWxhciFcIilcbn0gZWxzZSB7XG5wcmludChcIlRyeSB0byBiZSBtb3JlIHZpc2libGUhXCIpXG59In0=

You also had Facebook information available, remember? Time to add some more statements to our control structures using else if! Customize further: else if The else if statement allows you to further customize your control structure. You can add as many else if statements as you like. Keep in mind that R ignores the remainder of the control structure once a condition has been found that is TRUE and the corresponding expressions have been executed. Here’s an overview of the syntax to freshen your memory:

if (condition1) { expr1 } else if (condition2) { expr2 } else if (condition3) { expr3 } else { expr4 } Again, It’s important that the else if keywords comes on the same line as the closing bracket of the previous part of the control construct!

INSTRUCTIONS:

Add code to both control structures such that:

HINT: For both instructions, just keep the if, else if and else statements that are already available; just add the appropriate code within the else if statement.

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFZhcmlhYmxlcyByZWxhdGVkIHRvIHlvdXIgbGFzdCBkYXkgb2YgcmVjb3JkaW5nc1xubWVkaXVtIDwtIFwiTGlua2VkSW5cIlxubnVtX3ZpZXdzIDwtIDE0XG5cbiMgQ29udHJvbCBzdHJ1Y3R1cmUgZm9yIG1lZGl1bVxuaWYgKG1lZGl1bSA9PSBcIkxpbmtlZEluXCIpIHtcbnByaW50KFwiU2hvd2luZyBMaW5rZWRJbiBpbmZvcm1hdGlvblwiKVxufSBlbHNlIGlmIChtZWRpdW0gPT0gXCJGYWNlYm9va1wiKSB7XG4jIEFkZCBjb2RlIHRvIHByaW50IGNvcnJlY3Qgc3RyaW5nIHdoZW4gY29uZGl0aW9uIGlzIFRSVUVcblxufSBlbHNlIHtcbnByaW50KFwiVW5rbm93biBtZWRpdW1cIilcbn1cblxuIyBDb250cm9sIHN0cnVjdHVyZSBmb3IgbnVtX3ZpZXdzXG5pZiAobnVtX3ZpZXdzID4gMTUpIHtcbnByaW50KFwiWW91J3JlIHBvcHVsYXIhXCIpXG59IGVsc2UgaWYgKG51bV92aWV3cyA8PSAxNSAmIG51bV92aWV3cyA+IDEwKSB7XG4jIEFkZCBjb2RlIHRvIHByaW50IGNvcnJlY3Qgc3RyaW5nIHdoZW4gY29uZGl0aW9uIGlzIFRSVUVcblxufSBlbHNlIHtcbnByaW50KFwiVHJ5IHRvIGJlIG1vcmUgdmlzaWJsZSFcIilcbn0iLCJzb2x1dGlvbiI6IiMgVmFyaWFibGVzIHJlbGF0ZWQgdG8geW91ciBsYXN0IGRheSBvZiByZWNvcmRpbmdzXG5tZWRpdW0gPC0gXCJMaW5rZWRJblwiXG5udW1fdmlld3MgPC0gMTRcblxuIyBDb250cm9sIHN0cnVjdHVyZSBmb3IgbWVkaXVtXG5pZiAobWVkaXVtID09IFwiTGlua2VkSW5cIikge1xucHJpbnQoXCJTaG93aW5nIExpbmtlZEluIGluZm9ybWF0aW9uXCIpXG59IGVsc2UgaWYgKG1lZGl1bSA9PSBcIkZhY2Vib29rXCIpIHtcbiMgQWRkIGNvZGUgdG8gcHJpbnQgY29ycmVjdCBzdHJpbmcgd2hlbiBjb25kaXRpb24gaXMgVFJVRVxucHJpbnQoXCJTaG93aW5nIEZhY2Vib29rIGluZm9ybWF0aW9uXCIpXG59IGVsc2Uge1xucHJpbnQoXCJVbmtub3duIG1lZGl1bVwiKVxufSJ9

Have another look at the second control structure. Because R abandons the control flow as soon as it finds a condition that is met, you can simplify the condition for the else if part in the second construct to num_views > 10

Else if 2.0 You can do anything you want inside if-else constructs. You can even put in another set of conditional statements. Examine the following code chunk:

if (number < 10) { if (number < 5) { result <- “extra small” } else { result <- “small” } } else if (number < 100) { result <- “medium” } else { result <- “large” } print(result) Have a look at the following statements:

If number is set to 6, “small” gets printed to the console. If number is set to 100, R prints out “medium”. If number is set to 4, “extra small” gets printed out to the console. If number is set to 2500, R will generate an error, as result will not be defined. Select the option that lists all the true statements.

Take control!

In this exercise, you will combine everything that you’ve learned so far: relational operators, logical operators and control constructs. You’ll need it all!

In the editor, we’ve coded two values beforehand: li and fb, denoting the number of profile views your LinkedIn and Facebook profile had on the last day of recordings. Go through the instructions to create R code that generates a ‘social media score’, sms, based on the values of li and fb.

INSTRUCTIONS 70 XP Finish the control-flow construct with the following behavior:

If both li and fb are 15 or higher, set sms equal to double the sum of li and fb. If both li and fb are strictly below 10, set sms equal to half the sum of li and fb. In all other cases, set sms equal to li + fb. Finally, print the resulting sms variable to the console. Show Answer (-70 XP) HINT There are several ways to solve this exercise. The easiest will be to use an if-else if-else construct, and maintain the order of the instructions in your construct. You’ll need the & operator in this approach.
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFZhcmlhYmxlcyByZWxhdGVkIHRvIHlvdXIgbGFzdCBkYXkgb2YgcmVjb3JkaW5nc1xubGkgPC0gMVxuZmIgPC0gOVxuXG4jIENvZGUgdGhlIGNvbnRyb2wtZmxvdyBjb25zdHJ1Y3RcbmlmIChfX18gJiBfX18pIHtcbnNtcyA8LSAyICogKGxpICsgZmIpXG59IF9fXyAoX19fKSB7XG5zbXMgPC0gMC41ICogKGxpICsgZmIpXG59IGVsc2Uge1xuc21zIDwtIF9fX1xufVxuXG4jIFByaW50IHRoZSByZXN1bHRpbmcgc21zIHRvIHRoZSBjb25zb2xlIiwic29sdXRpb24iOiIjIFZhcmlhYmxlcyByZWxhdGVkIHRvIHlvdXIgbGFzdCBkYXkgb2YgcmVjb3JkaW5nc1xubGkgPC0gMTVcbmZiIDwtIDlcblxuIyBDb2RlIHRoZSBjb250cm9sLWZsb3cgY29uc3RydWN0XG5pZiAobGkgPj0gMTUgJiBmYiA+PSAxNSkge1xuc21zIDwtIDIgKiAobGkgKyBmYilcbn0gZWxzZSBpZiAobGkgPCAxMCAmIGZiIDwgMTApIHtcbnNtcyA8LSAwLjUgKiAobGkgKyBmYilcbn0gZWxzZSB7XG5zbXMgPC0gbGkgKyBmYlxufVxuXG4jIFByaW50IHRoZSByZXN1bHRpbmcgc21zIHRvIHRoZSBjb25zb2xlXG5wcmludChzbXMpIn0=