1. 程式人生 > >R語言——學習筆記(一)

R語言——學習筆記(一)

1.“>”符號後輸入指令。

2.1:100   指輸入1—100連續數值

3.sum()    函式,計算總和

4.sample()    函式,隨機取出

    例1:>sample(1:6,1)    從1—6的整數中取出1個

    例2:>sample(1:6,100,replace=TRUE)    從1—6的整數中取100次,replace=TRUE是指定放回抽樣

    例3:>sample(choujiang,100,prob=c(99,1),replace=TRUE)    prob=c(99,1)事件概率比例為99:1

5.table()    函式,生成表格

    例:>table(sample(1:6,100,replace=TRUE))    頻率分佈表

6.hist()    函式,生成直方圖

    例:>hist(sample(1:6,10000,replace=TRUE),breaks=0:6)    breaks=0:6設定橫軸的座標

7.mean()    函式,求平均值

    例:>mean(sample(1:6,100,replace=TRUE))

8.replicate()    函式,將一個命令重複執行指定次數

    例1:>res<-replicate(100000,mean(sample(1:6,100,replace=TRUE)))

9.製圖軟體包ggplot2

    >install.packages("ggplot2")

    >library(ggplot2)

    >dice<-data.frame(骰子=res)

  >ggplot(dice,aes(x=骰子))+geom_histogram(binwidth=.1,fill="steelblue",colour="black",alpha=0.5)+xlab("期望值")+ylab("次數")+ggtitle("骰100次骰子後的期望值")