1. 程式人生 > >R語言入門簡單練習

R語言入門簡單練習

[email protected]:~$ R

R version 3.4.4 (2018-03-15) -- "Someone to Lean On"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R是自由軟體,不帶任何擔保。
在某些條件下你可以將其自由散佈。
用'license()'或'licence()'來看散佈的詳細條件。

R是個合作計劃,有許多人為之做出了貢獻.
用'contributors()'來看合作者的詳細情況
用'citation()'會告訴你如何在出版物中正確地引用R或R程式包。

用'demo()'來看一些示範程式,用'help()'來閱讀線上幫助檔案,或
用'help.start()'通過HTML瀏覽器來看幫助檔案。
用'q()'退出R.

> revenue <- c(201451,214941,220185,197121,179431,187123,164877)
> active_players <- c (187421,197456,214784,201544,175446,187454,170154)
> mean(revenue)
[1] 195018.4
> mean(revenue)
[1] 195018.4
> mean(active_playesrs)
Error in mean(active_playesrs) : 找不到物件'active_playesrs'
> mean(active_players)
[1] 190608.4
> sd(revenue)
[1] 19535.76
> sd(active_players)
[1] 15380.33
> cor(revenue,active_players)
[1] 0.9014327
> plot(active_players,revenue)
> summary(revenue)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 164877  183277  197121  195018  208196  220185 
> summary(active_players)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 170154  181434  187454  190608  199500  214784 
> a <- c(1,3,5,7,9,13)
> b <- c("dau","wau","mau")
> c <- c(FALSE,FALSE,TRUE,TRUE)
> a[2]
[1] 3
> a[c(1,2,5)]
[1] 1 3 9
> a[2:5]
[1] 3 5 7 9
> y <- matrix(1:20,nrow=4,ncol=5)
> y
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    5    9   13   17
[2,]    2    6   10   14   18
[3,]    3    7   11   15   19
[4,]    4    8   12   16   20
> y2 <- matrix(1:20,nrow=4,ncol=5,byrow=TRUE)
> y2
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    2    3    4    5
[2,]    6    7    8    9   10
[3,]   11   12   13   14   15
[4,]   16   17   18   19   20
> y3 <- matrix(1:20,nrow=4,ncol=5,byrow=TRUE,dimnames=list(c("R1","R2","R3","R4"),c("C1","C2","C3","C4","C5")))
> y3
   C1 C2 C3 C4 C5
R1  1  2  3  4  5
R2  6  7  8  9 10
R3 11 12 13 14 15
R4 16 17 18 19 20
> y3[3:5]
[1] 11 16  2
> y3[2,4]
[1] 9
> y3[2,2:5]
C2 C3 C4 C5 
 7  8  9 10 
> dim1 <- c("A1","A2")
> dim2 <- c("B1","B2","B3")
> dim3 <- c("C1","C2","C3","C4")
> z <- array(1:24,c(2,3,4),dimnames=list(dim1,dim2,dim3))
> z
, , C1

   B1 B2 B3
A1  1  3  5
A2  2  4  6

, , C2

   B1 B2 B3
A1  7  9 11
A2  8 10 12

, , C3

   B1 B2 B3
A1 13 15 17
A2 14 16 18

, , C4

   B1 B2 B3
A1 19 21 23
A2 20 22 24

>