Equality 等效逻辑判断

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

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

Greater and less than 大于和小于

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

R基于字母表的顺序对字符串进行比较。

小练习:

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

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIENvbXBhcmlzb24gb2YgbnVtZXJpY3NcblxuXG4jIENvbXBhcmlzb24gb2YgY2hhcmFjdGVyIHN0cmluZ3NcblxuXG4jIENvbXBhcmlzb24gb2YgbG9naWNhbHMiLCJzb2x1dGlvbiI6IiMgQ29tcGFyaXNvbiBvZiBudW1lcmljc1xuLTYgKiA1ICsgMiA+PSAtMTAgKyAxXG5cbiMgQ29tcGFyaXNvbiBvZiBjaGFyYWN0ZXIgc3RyaW5nc1xuXCJyYWluaW5nXCIgPD0gXCJyYWluaW5nIGRvZ3NcIlxuXG4jIENvbXBhcmlzb24gb2YgbG9naWNhbHNcblRSVUUgPiBGQUxTRSJ9

比较向量 Compare vectors

R 对于向量的处理是非常在行的,不用额外的修改,R就能执行向量有关的操作。

下面的示例代码给出了两个向量,一个是weibo, 一个是twitter。每一个向量记录了过去7天,你的页面被浏览的次数。

小练习: 使用关系运算符(relational operators), 找到逻辑判断答案, i.e. TRUE or FALSE, 问题如下:

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFRoZSB3ZWlibyBhbmQgdHdpdHRlciB2ZWN0b3JzIGhhdmUgYWxyZWFkeSBiZWVuIGNyZWF0ZWQgZm9yIHlvdVxud2VpYm8gPC0gYygxNiwgOSwgMTMsIDUsIDIsIDE3LCAxNClcbnR3aXR0ZXIgPC0gYygxNywgNywgNSwgMTYsIDgsIDEzLCAxNClcblxuIyBQb3B1bGFyIGRheXNcblxuXG4jIFF1aWV0IGRheXNcblxuXG4jIHdlaWJvIG1vcmUgcG9wdWxhciB0aGFuIHR3aXR0ZXIiLCJzb2x1dGlvbiI6IiMgVGhlIHdlaWJvIGFuZCB0d2l0dGVyIHZlY3RvcnMgaGF2ZSBhbHJlYWR5IGJlZW4gY3JlYXRlZCBmb3IgeW91XG53ZWlibyA8LSBjKDE1LCA5LCAxMywgNiwgNywgMTQsIDE0KVxudHdpdHRlciA8LSBjKDIwLCA3LCA2LCAxNiwgOSwgMTMsIDE0KVxuXG4jIFBvcHVsYXIgZGF5c1xud2VpYm8gPiAxNlxuXG4jIFF1aWV0IGRheXNcbndlaWJvIDw9IDEwXG5cbiMgd2VpYm8gbW9yZSBwb3B1bGFyIHRoYW4gdHdpdHRlclxud2VpYm8gPiB0d2l0dGVyIn0=

比较矩阵Compare matrices

R 可以处理各种不同的数据结构,自然也包括矩阵。

这一次 weibo 和 twitter被存储在一个叫做views的矩阵里面。 第一行包含 weibo 的浏览量; 第二行是 twitter 的, twitter and weibo 的原始向量也都在。

小练习:

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

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFRoZSBzb2NpYWwgZGF0YSBoYXMgYmVlbiBjcmVhdGVkIGZvciB5b3VcbndlaWJvIDwtIGMoMTYsIDksIDEzLCA1LCAyLCAxNywgMTQpXG50d2l0dGVyIDwtIGMoMTcsIDcsIDUsIDE2LCA4LCAxMywgMTQpXG52aWV3cyA8LSBtYXRyaXgoYyh3ZWlibywgdHdpdHRlciksIG5yb3cgPSAyLCBieXJvdyA9IFRSVUUpXG5cbiMgV2hlbiBkb2VzIHZpZXdzIGVxdWFsIDY/XG5cblxuIyBXaGVuIGlzIHZpZXdzIGxlc3MgdGhhbiBvciBlcXVhbCB0byA1PyIsInNvbHV0aW9uIjoiIyBUaGUgc29jaWFsIGRhdGEgaGFzIGJlZW4gY3JlYXRlZCBmb3IgeW91XG53ZWlibyA8LSBjKDE2LCA5LCAxMywgNSwgMiwgMTcsIDE0KVxudHdpdHRlciA8LSBjKDE3LCA3LCA1LCAxNiwgOCwgMTMsIDE0KVxudmlld3MgPC0gbWF0cml4KGMod2VpYm8sIHR3aXR0ZXIpLCBucm93ID0gMiwgYnlyb3cgPSBUUlVFKVxuXG4jIFdoZW4gZG9lcyB2aWV3cyBlcXVhbCAxMz9cbnZpZXdzID09IDZcblxuIyBXaGVuIGlzIHZpZXdzIGxlc3MgdGhhbiBvciBlcXVhbCB0byAxND9cbnZpZXdzIDw9IDUifQ==

& and |

下面这些表达式的结果都是 TRUE:

特别注意, 3 < x < 7 不能用来检测 x 是否在 3 and 7 之间; 要达到这个比较的目的,你需要使用 3 < x & x < 7。

在下面的小练习里,你将使用last这个变量,它等于你最后一天weibo的浏览量。

提示: 在最后的那一小题,记得要使用 & 两次, | 一次。使用括号来确保运行的顺序正确。
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFRoZSB3ZWlibyBhbmQgbGFzdCB2YXJpYWJsZSBhcmUgYWxyZWFkeSBkZWZpbmVkIGZvciB5b3VcbndlaWJvIDwtIGMoMTYsIDksIDEzLCA1LCAyLCAxNywgMTQpXG5sYXN0IDwtIHRhaWwod2VpYm8sIDEpXG5cbiMgSXMgbGFzdCB1bmRlciA1IG9yIGFib3ZlIDEwP1xuXG5cbiMgSXMgbGFzdCBiZXR3ZWVuIDE1IChleGNsdXNpdmUpIGFuZCAyMCAoaW5jbHVzaXZlKT8iLCJzb2x1dGlvbiI6IiMgVGhlIHdlaWJvIGFuZCBsYXN0IHZhcmlhYmxlIGFyZSBhbHJlYWR5IGRlZmluZWQgZm9yIHlvdVxud2VpYm8gPC0gYygxNiwgOSwgMTMsIDUsIDIsIDE3LCAxNClcbmxhc3QgPC0gdGFpbCh3ZWlibywgMSlcblxuIyBJcyBsYXN0IHVuZGVyIDUgb3IgYWJvdmUgMTA/XG5sYXN0IDwgNSB8IGxhc3QgPiAxMFxuXG4jIElzIGxhc3QgYmV0d2VlbiAxNSAoZXhjbHVzaXZlKSBhbmQgMjAgKGluY2x1c2l2ZSk/XG5sYXN0ID4gMTUgJiBsYXN0IDw9IDIwIn0=

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

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

INSTRUCTIONS:

When did weibo views exceed 10 and did twitter views fail to reach 10 for a particular day? Use the weibo and twitter vectors. When were one or both of your weibo and twitter 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.

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IndlaWJvIDwtIGMoMTYsIDksIDEzLCA1LCAyLCAxNywgMTQpXG50d2l0dGVyIDwtIGMoMTcsIDcsIDUsIDE2LCA4LCAxMywgMTQpXG52aWV3cyA8LSBtYXRyaXgoYyh3ZWlibywgdHdpdHRlciksIG5yb3cgPSAyLCBieXJvdyA9IFRSVUUpIiwic2FtcGxlIjoiIyBUaGUgc29jaWFsIGRhdGEgKHdlaWJvLCB0d2l0dGVyLCB2aWV3cykgaGFzIGJlZW4gY3JlYXRlZCBmb3IgeW91XG5cbiMgd2VpYm8gZXhjZWVkcyAxMCBidXQgdHdpdHRlciBiZWxvdyAxMFxuXG5cbiMgV2hlbiB3ZXJlIG9uZSBvciBib3RoIHZpc2l0ZWQgYXQgbGVhc3QgMTIgdGltZXM/XG5cblxuIyBXaGVuIGlzIHZpZXdzIGJldHdlZW4gMTEgKGV4Y2x1c2l2ZSkgYW5kIDE0IChpbmNsdXNpdmUpPyIsInNvbHV0aW9uIjoiIyBUaGUgc29jaWFsIGRhdGEgKHdlaWJvLCB0d2l0dGVyLCB2aWV3cykgaGFzIGJlZW4gY3JlYXRlZCBmb3IgeW91XG5cbiMgd2VpYm8gZXhjZWVkcyAxMCBidXQgdHdpdHRlciBiZWxvdyAxMFxud2VpYm8gPiAxMCAmIHR3aXR0ZXIgPCAxMFxuXG4jIFdoZW4gd2VyZSBvbmUgb3IgYm90aCB2aXNpdGVkIGF0IGxlYXN0IDEyIHRpbWVzP1xud2VpYm8gPj0gMTIgfCB0d2l0dGVyID49IDEyXG5cbiMgV2hlbiBpcyB2aWV3cyBiZXR3ZWVuIDExIChleGNsdXNpdmUpIGFuZCAxNCAoaW5jbHVzaXZlKT9cbnZpZXdzID4gMTEgJiB2aWV3cyA8PSAxNCJ9

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 weibo profile was visited more than 10 times, while your twitter 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 weibo profile, suppose you conducted a survey inside the company you’re working for. You’ve asked every employee with a weibo 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 weibo information” if the medium variable equals “weibo”. 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.
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFZhcmlhYmxlcyByZWxhdGVkIHRvIHlvdXIgbGFzdCBkYXkgb2YgcmVjb3JkaW5nc1xubWVkaXVtIDwtIFwid2VpYm9cIlxubnVtX3ZpZXdzIDwtIDE0XG5cbiMgRXhhbWluZSB0aGUgaWYgc3RhdGVtZW50IGZvciBtZWRpdW1cbmlmIChtZWRpdW0gPT0gXCJ3ZWlib1wiKSB7XG5wcmludChcIlNob3dpbmcgd2VpYm8gaW5mb3JtYXRpb25cIilcbn1cblxuIyBXcml0ZSB0aGUgaWYgc3RhdGVtZW50IGZvciBudW1fdmlld3MiLCJzb2x1dGlvbiI6IiMgVmFyaWFibGVzIHJlbGF0ZWQgdG8geW91ciBsYXN0IGRheSBvZiByZWNvcmRpbmdzXG5tZWRpdW0gPC0gXCJ3ZWlib1wiXG5udW1fdmlld3MgPC0gMTRcblxuIyBFeGFtaW5lIHRoZSBpZiBzdGF0ZW1lbnQgZm9yIG1lZGl1bVxuaWYgKG1lZGl1bSA9PSBcIndlaWJvXCIpIHtcbnByaW50KFwiU2hvd2luZyB3ZWlibyBpbmZvcm1hdGlvblwiKVxufVxuXG4jIFdyaXRlIHRoZSBpZiBzdGF0ZW1lbnQgZm9yIG51bV92aWV3c1xuaWYgKG51bV92aWV3cyA+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 !
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFZhcmlhYmxlcyByZWxhdGVkIHRvIHlvdXIgbGFzdCBkYXkgb2YgcmVjb3JkaW5nc1xubWVkaXVtIDwtIFwid2VpYm9cIlxubnVtX3ZpZXdzIDwtIDE0XG5cbiMgQ29udHJvbCBzdHJ1Y3R1cmUgZm9yIG1lZGl1bVxuaWYgKG1lZGl1bSA9PSBcIndlaWJvXCIpIHtcbnByaW50KFwiU2hvd2luZyB3ZWlibyBpbmZvcm1hdGlvblwiKVxufVxuXG5cblxuIyBDb250cm9sIHN0cnVjdHVyZSBmb3IgbnVtX3ZpZXdzXG5pZiAobnVtX3ZpZXdzID4gMTUpIHtcbnByaW50KFwiWW91J3JlIHBvcHVsYXIhXCIpXG59Iiwic29sdXRpb24iOiIjIFZhcmlhYmxlcyByZWxhdGVkIHRvIHlvdXIgbGFzdCBkYXkgb2YgcmVjb3JkaW5nc1xubWVkaXVtIDwtIFwid2VpYm9cIlxubnVtX3ZpZXdzIDwtIDE0XG5cbiMgQ29udHJvbCBzdHJ1Y3R1cmUgZm9yIG1lZGl1bVxuaWYgKG1lZGl1bSA9PSBcIndlaWJvXCIpIHtcbnByaW50KFwiU2hvd2luZyB3ZWlibyBpbmZvcm1hdGlvblwiKVxufSBlbHNle1xucHJpbnQoXCJVbmtub3duIG1lZGl1bVwiKVxufVxuXG4jIENvbnRyb2wgc3RydWN0dXJlIGZvciBudW1fdmlld3NcbmlmIChudW1fdmlld3MgPiAxNSkge1xucHJpbnQoXCJZb3UncmUgcG9wdWxhciFcIilcbn0gZWxzZSB7XG5wcmludChcIlRyeSB0byBiZSBtb3JlIHZpc2libGUhXCIpXG59In0=

You also had twitter 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.

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFZhcmlhYmxlcyByZWxhdGVkIHRvIHlvdXIgbGFzdCBkYXkgb2YgcmVjb3JkaW5nc1xubWVkaXVtIDwtIFwid2VpYm9cIlxubnVtX3ZpZXdzIDwtIDE0XG5cbiMgQ29udHJvbCBzdHJ1Y3R1cmUgZm9yIG1lZGl1bVxuaWYgKG1lZGl1bSA9PSBcIndlaWJvXCIpIHtcbnByaW50KFwiU2hvd2luZyB3ZWlibyBpbmZvcm1hdGlvblwiKVxufSBlbHNlIGlmIChtZWRpdW0gPT0gXCJ0d2l0dGVyXCIpIHtcbiMgQWRkIGNvZGUgdG8gcHJpbnQgY29ycmVjdCBzdHJpbmcgd2hlbiBjb25kaXRpb24gaXMgVFJVRVxuXG59IGVsc2Uge1xucHJpbnQoXCJVbmtub3duIG1lZGl1bVwiKVxufVxuXG4jIENvbnRyb2wgc3RydWN0dXJlIGZvciBudW1fdmlld3NcbmlmIChudW1fdmlld3MgPiAxNSkge1xucHJpbnQoXCJZb3UncmUgcG9wdWxhciFcIilcbn0gZWxzZSBpZiAobnVtX3ZpZXdzIDw9IDE1ICYgbnVtX3ZpZXdzID4gMTApIHtcbiMgQWRkIGNvZGUgdG8gcHJpbnQgY29ycmVjdCBzdHJpbmcgd2hlbiBjb25kaXRpb24gaXMgVFJVRVxuXG59IGVsc2Uge1xucHJpbnQoXCJUcnkgdG8gYmUgbW9yZSB2aXNpYmxlIVwiKVxufSIsInNvbHV0aW9uIjoiIyBWYXJpYWJsZXMgcmVsYXRlZCB0byB5b3VyIGxhc3QgZGF5IG9mIHJlY29yZGluZ3Ncbm1lZGl1bSA8LSBcIndlaWJvXCJcbm51bV92aWV3cyA8LSAxNFxuXG4jIENvbnRyb2wgc3RydWN0dXJlIGZvciBtZWRpdW1cbmlmIChtZWRpdW0gPT0gXCJ3ZWlib1wiKSB7XG5wcmludChcIlNob3dpbmcgd2VpYm8gaW5mb3JtYXRpb25cIilcbn0gZWxzZSBpZiAobWVkaXVtID09IFwidHdpdHRlclwiKSB7XG4jIEFkZCBjb2RlIHRvIHByaW50IGNvcnJlY3Qgc3RyaW5nIHdoZW4gY29uZGl0aW9uIGlzIFRSVUVcbnByaW50KFwiU2hvd2luZyB0d2l0dGVyIGluZm9ybWF0aW9uXCIpXG59IGVsc2Uge1xucHJpbnQoXCJVbmtub3duIG1lZGl1bVwiKVxufSJ9

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 weibo and twitter 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=