swirl学习之一——Basic Building Blocks


| Please choose a course, or type 0 to exit swirl.

1: R Programming
2: Take me to the swirl course repository!

Selection: 1

| Please choose a lesson, or type 0 to return to course menu.

1: Basic Building Blocks 2: Workspace and Files
3: Sequences of Numbers 4: Vectors
5: Missing Values 6: Subsetting Vectors
7: Matrices and Data Frames 8: Logic
9: Functions 10: lapply and sapply
11: vapply and tapply 12: Looking at Data
13: Simulation 14: Dates and Times
15: Base Graphics

Selection: 1

| | 0%

| In this lesson, we will explore some basic building blocks of
| the R programming language.

...

|== | 3%

| If at any point you'd like more information on a particular
| topic related to R, you can type help.start() at the prompt,
| which will open a menu of resources (either within RStudio or
| your default web browser, depending on your setup).
| Alternatively, a simple web search often yields the answer
| you're looking for.

...

|=== | 5%

| In its simplest form, R can be used as an interactive
| calculator. Type 5 + 7 and press Enter.

> 5+7
[1] 12

| You are doing so well!

|===== | 8%

| R simply prints the result of 12 by default. However, R is a
| programming language and often the reason we use a programming
| language as opposed to a calculator is to automate some process
| or avoid unnecessary repetition.

...

|====== | 11%

| In this case, we may want to use our result from above in a
| second calculation. Instead of retyping 5 + 7 every time we
| need it, we can just create a new variable that stores the
| result.

...

|======== | 14%

| The way you assign a value to a variable in R is by using the
| assignment operator, which is just a 'less than' symbol
| followed by a 'minus' sign. It looks like this: <-

...

|========= | 16%

| Think of the assignment operator as an arrow. You are assigning
| the value on the right side of the arrow to the variable name
| on the left side of the arrow.

...

|=========== | 19%

| To assign the result of 5 + 7 to a new variable called x, you
| type x <- 5 + 7. This can be read as 'x gets 5 plus 7'. Give it
| a try now.

> x<-5+7

| That's correct!

|============ | 22%

| You'll notice that R did not print the result of 12 this time.
| When you use the assignment operator, R assumes that you don't
| want to see the result immediately, but rather that you intend
| to use the result for something else later on.

...

|============== | 24%

| To view the contents of the variable x, just type x and press
| Enter. Try it now.

> x
[1] 12

| That's correct!

|=============== | 27%

| Now, store the result of x - 3 in a new variable called y.

> y<-x-3

| Your dedication is inspiring!

|================= | 30%

| What is the value of y? Type y to find out.

> y
[1] 9

| All that practice is paying off!

|================== | 32%

| Now, let's create a small collection of numbers called a
| vector. Any object that contains data is called a data
| structure and numeric vectors are the simplest type of data
| structure in R. In fact, even a single number is considered a
| vector of length one.

...

|==================== | 35%

| The easiest way to create a vector is with the c() function,
| which stands for 'concatenate' or 'combine'. To create a vector
| containing the numbers 1.1, 9, and 3.14, type c(1.1, 9, 3.14).
| Try it now and store the result in a variable called z.

> z<-c(1.1,9,3.14)

| Excellent job!

|===================== | 38%

| Anytime you have questions about a particular function, you can
| access R's built-in help files via the `?` command. For
| example, if you want more information on the c() function, type
| ?c without the parentheses that normally follow a function
| name. Give it a try.

> ?c

| You are amazing!

|======================= | 41%

| Type z to view its contents. Notice that there are no commas
| separating the values in the output.

> z
[1] 1.10 9.00 3.14

| All that hard work is paying off!

|======================== | 43%

| You can combine vectors to make a new vector. Create a new
| vector that contains z, 555, then z again in that order. Don't
| assign this vector to a new variable, so that we can just see
| the result immediately.

> c(z,555,z)
[1] 1.10 9.00 3.14 555.00 1.10 9.00 3.14

| You got it!

|========================== | 46%

| Numeric vectors can be used in arithmetic expressions. Type the
| following to see what happens: z * 2 + 100.

> z*2+100
[1] 102.20 118.00 106.28

| Nice work!

|=========================== | 49%

| First, R multiplied each of the three elements in z by 2. Then
| it added 100 to each element to get the result you see above.

...

|============================= | 51%

| Other common arithmetic operators are `+`, `-`, `/`, and `^`
| (where x^2 means 'x squared'). To take the square root, use the
| sqrt() function and to take the absolute value, use the abs()
| function.

...

|============================== | 54%

| Take the square root of z - 1 and assign it to a new variable
| called my_sqrt.

> my_sqrt<-sqrt(z-1)

| You are quite good my friend!

|================================ | 57%

| Before we view the contents of the my_sqrt variable, what do
| you think it contains?

1: a vector of length 3
2: a single number (i.e a vector of length 1)
3: a vector of length 0 (i.e. an empty vector)

Selection: 1

| Keep up the great work!

|================================= | 59%

| Print the contents of my_sqrt.

> my_sqrt
[1] 0.3162278 2.8284271 1.4628739

| You are quite good my friend!

|=================================== | 62%

| As you may have guessed, R first subtracted 1 from each element
| of z, then took the square root of each element. This leaves
| you with a vector of the same length as the original vector z.

...

|==================================== | 65%

| Now, create a new variable called my_div that gets the value of
| z divided by my_sqrt.

> my_div<-z/my_sqrt

| Perseverance, that's the answer.

|====================================== | 68%

| Which statement do you think is true?

1: my_div is undefined
2: The first element of my_div is equal to the first element of z divided by the first element of my_sqrt, and so on...
3: my_div is a single number (i.e a vector of length 1)

Selection: 2

| You got it right!

|======================================= | 70%

| Go ahead and print the contents of my_div.

> my_div
[1] 3.478505 3.181981 2.146460

| Your dedication is inspiring!

|========================================= | 73%

| When given two vectors of the same length, R simply performs
| the specified arithmetic operation (`+`, `-`, `*`, etc.)
| element-by-element. If the vectors are of different lengths, R
| 'recycles' the shorter vector until it is the same length as
| the longer vector.

...

|========================================== | 76%

| When we did z * 2 + 100 in our earlier example, z was a vector
| of length 3, but technically 2 and 100 are each vectors of
| length 1.

...

|============================================ | 78%

| Behind the scenes, R is 'recycling' the 2 to make a vector of
| 2s and the 100 to make a vector of 100s. In other words, when
| you ask R to compute z * 2 + 100, what it really computes is
| this: z * c(2, 2, 2) + c(100, 100, 100).

...

|============================================= | 81%

| To see another example of how this vector 'recycling' works,
| try adding c(1, 2, 3, 4) and c(0, 10). Don't worry about saving
| the result in a new variable.

> c(1,2,3,4)+c(0,10)
[1] 1 12 3 14

| Great job!

|=============================================== | 84%

| If the length of the shorter vector does not divide evenly into
| the length of the longer vector, R will still apply the
| 'recycling' method, but will throw a warning to let you know
| something fishy might be going on.

...

|================================================ | 86%

| Try c(1, 2, 3, 4) + c(0, 10, 100) for an example.

> c(1,2,3,4)+c(0,10,100)
[1] 1 12 103 4
Warning message:
In c(1, 2, 3, 4) + c(0, 10, 100) :
longer object length is not a multiple of shorter object length

| Nice work!

|================================================== | 89%

| Before concluding this lesson, I'd like to show you a couple of
| time-saving tricks.

...

|=================================================== | 92%

| Earlier in the lesson, you computed z * 2 + 100. Let's pretend
| that you made a mistake and that you meant to add 1000 instead
| of 100. You could either re-type the expression, or...

...

|===================================================== | 95%

| In many programming environments, the up arrow will cycle
| through previous commands. Try hitting the up arrow on your
| keyboard until you get to this command (z * 2 + 100), then
| change 100 to 1000 and hit Enter. If the up arrow doesn't work
| for you, just type the corrected command.

> z*2+1000
[1] 1002.20 1018.00 1006.28

| All that practice is paying off!

|====================================================== | 97%

| Finally, let's pretend you'd like to view the contents of a
| variable that you created earlier, but you can't seem to
| remember if you named it my_div or myDiv. You could try both
| and see what works, or...

...

|========================================================| 100%

| You can type the first two letters of the variable name, then
| hit the Tab key (possibly more than once). Most programming
| environments will provide a list of variables that you've
| created that begin with 'my'. This is called auto-completion
| and can be quite handy when you have many variables in your
| workspace. Give it a try. (If auto-completion doesn't work for
| you, just type my_div and press Enter.)

> my_div
[1] 3.478505 3.181981 2.146460

| You are quite good my friend!

| Are you currently enrolled in the Coursera course associated
| with this lesson?

1: Yes
2: No

Selection: 2

| You've reached the end of this lesson! Returning to the main
| menu...

《这一生,至少当一次傻瓜》读书笔记

五一在家读完了这本书,收获了许许多多的感动,也明白了每个人都有不为人知的艰苦奋斗历程。推荐大家阅读这本书,我把书中对我自己感触深的地方摘抄下来。

(一)

“我告诉他当个傻瓜就好,做做看就会知道,没有比当傻瓜更简单的事了。既然想死,那就在死之前当一次傻瓜。身为有过相同念头的过来人,我领悟到了一点:为一件事疯狂,总有一天可以从中找到答案。”

木村这句话道尽了他的人生。


(二)

想要不使用农药栽培苹果,简直就是痴人说梦。

任何苹果果农都会这么认为。

问题是,木村为什么会疯狂地迷上这个痴人之梦?

阅读全文 »

倒退的旅行

(一)

我靠在座位上,仰头盯着车厢顶上淡黄色平行的两行灯,眼角的余光在不经意间,感受到两侧车窗外的一切物体都在向前迅速掠过。一切的岩石、树木、楼房、花草、电线塔,无一例外,都迅速向前、向前,消失不见。也许是前方有个黑洞,把这一切的物体都统统吞噬了吧。


(二)

周围的窗户黑了下来,一道道光影划过,仿佛置身时光列车中,穿梭于跨越时空的隧道。

我一直在后退,快速地后退,沿着时光的脚印倒退,退回到我身后那片记忆中充满欢笑与爱的土地——故乡。

阅读全文 »

用心聆听你们的声音

今天上午,连续四节的《软件体系结构》实验课,从早上八点开始,一直持续到将近中午十二点,我担任这门课的助教。

实验的内容关于设计模式,题目有六道,在上个星期四布置,要求同学们在本周四(今天)前完成,并在实验课上由助教逐一检查。

由于我自己平时在这门课的理论课上也没有仔细听讲,多是去了把签到表发下去,然后就径自坐在教室后排看自己带的书,所以只好临时突击。昨天晚上和今天一早把六个设计模式看了一遍,并把题目和详解也看了一遍,当然,这么短的时间看下来,只是知其然而已。

阅读全文 »

摘抄小诗一首

别让我祈祷能在险恶中获得庇护,

而祈祷可以勇敢地面对险恶;


别让我乞求痛苦止息,

而乞求我的心可以战胜痛苦;


别让我在人生战场上盼望盟友,

而是发现自己的力量。

阅读全文 »

顶澳仔小广场卖豆花的老爷爷

下午六点多去顶澳仔小广场,接即将跳舞结束的梦梦。

傍晚的小广场,往来的行人络绎不绝。在灯火辉煌的街边小店和众人之中,我看到了一个坐在石墩上的老爷爷,他的背微微弓起,面前是一辆三轮车,拖斗里放着一桶做好的豆花,车上立着一个大牌子,写着“客家豆花”。

我看到有人来买豆花,老爷爷起身,背弓得很厉害,深深弯着腰,用纸杯打着豆花,加了点糖,盖好盖子,递给客人,那样子真是礼貌极了。

阅读全文 »

没有任何借口

我这个人有个坏毛病,有时候对待家人和女朋友,明明是没有关心和照顾到,却说着许许多多正当的理由。

生活和学习中的很多时候,我如果没做好一件事情,心里常会反思着一定是自己不够认真或者没有尽心尽力,而不会找其他的借口。和同学一起做某件事,当看到摊上麻烦事时大家相互推脱、把自己置身事外的样子时,我心里总会一声叹息,然后想着怎么把事情往好的方向调整,下一步要怎么做才是关键,互相推卸责任、把自己与事情撇清关系是不可取的,是没有责任心和缺乏担当的表现。

阅读全文 »

厦大院长推荐书目

阅读全文 »

《异类》读书笔记整理

马尔科姆·格拉德威尔(Malcolm Gladwell)的这本《异类》,我当时买的时候,是因为这个书名与众不同。昨天快速读了一遍,之所以快速,是因为书中的内容并不是特别符合我现在的品味,有些是已经知道的,譬如马太效应和10000小时法则等,还有些是偏向励志类的故事。

不过每本书都有其可取和可学之处。我就把我感受深的几处内容整理一下,是为读后笔记。

注:其中标明“原文摘抄”的部分,即是从书中截取的段落文字;标明“原文整理”的部分,是我将原文内容进行整合,并适当加入了自己的语言来描述。

阅读全文 »

感悟在思维碰撞间迸发

今晚刚刚听完讲座《跨界·对话——人性迷思》,是新闻传播学院的黄合水教授主讲。

黄教授的本科、硕士和博士都是学习心理学专业,他讲座中的许多观点是不错的。譬如他谈到人要活着、不死,就要满足三点:维衡,避害,度闲。这关联到人的三个需求:生理,安全,感动。这又对应地关联到第一、第二、第三产业。

此话怎讲?具体来说,人要活着,首先要维持自身的平衡,吃喝拉撒睡,还有性,这些是最基本的生理需求,要满足这个需求,我们就有了农业,要种植粮食来维持生命。其次是避害,人要活着就要有基本的安全保障,保证自己不被老虎吃掉,不被洪水冲走等等,那么就有了制造业,我们生产工具,来对抗动物,来抵抗洪水。最后,当生理需求和安全需求都满足的情况下,人每天除了睡觉吃饭之外,还有很多的空闲时间,要怎么度过这些空闲时间便成了一个问题,于是有了服务业,我们可以从事商业活动,可以运输东西,可以玩手机,可以跳广场舞等等,我们可以做各种各样的事情来打发空闲的时间,我们可以运用各种感官来感受世界,接收信息,观察事物,来获得感动,得到对世界的认知和体悟。

阅读全文 »