R Markdown 文件的常用語法

1. 字型大小:

網絡分析

網絡分析

網絡分析

網絡分析

網絡分析
網絡分析

2. 文句強調:

斜體 and 粗體

3. 條列語句:

Network data analysis:

  • Ego-network

  • Centrality measures

  • Group and community structures

  • Positions and role analysis

  • Network-level indices

4. 表格:

第一組 第二組 第三組 第四組 第五組
樣本1 樣本2 樣本3 樣本4 樣本5
樣本6 樣本7 樣本8 樣本9 樣本10

5. 上標與下標:

單位:km2

水分子:H2O is a liquid.

6. 分隔線:


7. 插入 R Code Chuncks:

作業一 (A Short Introduction to R)

ToDo 1

(2014 - 2009) / (2014 - 1991) * 100
## [1] 21.73913

ToDo 2

a = (2014 - 2009) / (2014 - 1991) * 100
a
## [1] 21.73913

ToDo 3

b = c(4,5,8,11)
sum(b)
## [1] 28
x = rnorm(100)
plot(x)

ToDo 4: Help and documentation

help(sqrt)
## starting httpd help server ... done

ToDo 5: Scripts

setwd("D:/R_Labs/rmd_demo")
source("firstscript.R")

ToDo 6: Data structures

p = 31:60
Q = matrix(data=p, nrow = 6)

x1 = rnorm(100)
x2 = rnorm(100)
x3 = rnorm(100)
t = data.frame(a=x1,b=x1+x2,c=x1+x2+x3)
plot(t)

sd(as.numeric(unlist(t)))
## [1] 1.245676

ToDo 7: Graphic

plot(t$a, type="l", ylim=range(t),lwd=3, col=rgb(1,1,0,0.7))
lines(t$b, type="s", lwd=2,col=rgb(0.3,0.4,0.3,0.9))
points(t$c, pch=22, cex=2,col=rgb(0,0,1,0.3))

ToDo 8: Reading and writing data files

setwd("D:/R_Labs/rmd_demo")
d = read.table(file="tst1.txt",header = TRUE)
d$g = d$g * 5
write.table(d,file="tst2.txt",row.names=FALSE)

ToDo 9: Not available data

mean(sqrt(rnorm(100)))
## Warning in sqrt(rnorm(100)): 產生了 NaNs
## [1] NaN

ToDo 10: Classes

data1 = c(strptime(c("20141205","20150612"),format="%Y%m%d"))
data2 = c(100,90)
plot(data1,data2, xlab = "Date",ylab="Count",main = "The number of presents I expected.")

ToDo 11: Programming tools

s = 1:100
for(i in 1:100)
{
  if (s[i] < 5 | s[i] > 90)
  {
    s[i] = s[i] * 10
  } else
  {
    s[i] = s[i] * 0.1
  }
}
s
##   [1]   10.0   20.0   30.0   40.0    0.5    0.6    0.7    0.8    0.9    1.0
##  [11]    1.1    1.2    1.3    1.4    1.5    1.6    1.7    1.8    1.9    2.0
##  [21]    2.1    2.2    2.3    2.4    2.5    2.6    2.7    2.8    2.9    3.0
##  [31]    3.1    3.2    3.3    3.4    3.5    3.6    3.7    3.8    3.9    4.0
##  [41]    4.1    4.2    4.3    4.4    4.5    4.6    4.7    4.8    4.9    5.0
##  [51]    5.1    5.2    5.3    5.4    5.5    5.6    5.7    5.8    5.9    6.0
##  [61]    6.1    6.2    6.3    6.4    6.5    6.6    6.7    6.8    6.9    7.0
##  [71]    7.1    7.2    7.3    7.4    7.5    7.6    7.7    7.8    7.9    8.0
##  [81]    8.1    8.2    8.3    8.4    8.5    8.6    8.7    8.8    8.9    9.0
##  [91]  910.0  920.0  930.0  940.0  950.0  960.0  970.0  980.0  990.0 1000.0
k=1:10
fun = function(arg1)
{
  l = length(arg1)
  for(i in 1:l)
  {
    if (arg1[i] < 5 | arg1[i] > 90)
    {
      arg1[i] = arg1[i] * 10
    } else
    {
      arg1[i] = arg1[i] * 0.1
    }
  }
  return (arg1)
}
fun(arg1=k)
##  [1] 10.0 20.0 30.0 40.0  0.5  0.6  0.7  0.8  0.9  1.0
k
##  [1]  1  2  3  4  5  6  7  8  9 10