小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

R語(yǔ)言簡(jiǎn)單for循環(huán)

 醫(yī)科研 2021-01-25

  

歡迎來(lái)到醫(yī)科研,這里是白介素2的讀書筆記,跟我一起聊臨床與科研的故事, 生物醫(yī)學(xué)數(shù)據(jù)挖掘,R語(yǔ)言,TCGA、GEO數(shù)據(jù)挖掘。

  簡(jiǎn)單for循環(huán) 

創(chuàng)建一個(gè)簡(jiǎn)單數(shù)據(jù)框

1Sys.setlocale('LC_ALL','C')
2## [1] "C"
3library(tidyverse)
4## -- Attaching packages ---------------------------------------------------- tidyverse 1.2.1 --
5## <U+221A> ggplot2 3.1.0       <U+221A> purrr   0.3.0  
6## <U+221A> tibble  2.0.1       <U+221A> dplyr   0.8.0.1
7## <U+221A> tidyr   0.8.2       <U+221A> stringr 1.4.0  
8## <U+221A> readr   1.3.1       <U+221A> forcats 0.4.0
9## -- Conflicts ------------------------------------------------------- tidyverse_conflicts() --
10## x dplyr::filter() masks stats::filter()
11## x dplyr::lag()    masks stats::lag()
12df<-tibble(
13  a=rnorm(10),
14  b=rnorm(10),
15  c=rnorm(10),
16  d=rnorm(10)
17)
18df
19## # A tibble: 10 x 4
20##         a      b      c       d
21##     <dbl>  <dbl>  <dbl>   <dbl>
22##  1 -0.956 -0.848  0.701 -1.10  
23##  2  1.10  -0.925 -0.326 -0.328 
24##  3 -1.39  -0.553 -1.36   1.17  
25##  4 -0.102 -2.13   0.949  0.219 
26##  5 -0.930 -0.789 -1.81  -0.611 
27##  6 -1.56  -1.08   1.54  -0.403 
28##  7 -0.115 -0.310 -2.07  -1.10  
29##  8  0.817  0.151 -0.464 -0.0366
30##  9 -1.97   1.25  -0.396 -0.700 
31## 10  2.44   0.486 -1.91  -0.334

元素提取的差異 df[1]與df[[1]]

提取第一列

1df[1]
2## # A tibble: 10 x 1
3##         a
4##     <dbl>
5##  1 -0.956
6##  2  1.10 
7##  3 -1.39 
8##  4 -0.102
9##  5 -0.930
10##  6 -1.56 
11##  7 -0.115
12##  8  0.817
13##  9 -1.97 
14## 10  2.44

提取第一列的元素操作

1df[[1]]
2##  [1] -0.9556780  1.1017707 -1.3890825 -0.1017430 -0.9304293 -1.5648136
3##  [7] -0.1151510  0.8174654 -1.9693236  2.4369937

實(shí)現(xiàn)一個(gè)需求:要求使用求出每一列的中位值
當(dāng)然這個(gè)需求可以用簡(jiǎn)單的代碼實(shí)現(xiàn),因?yàn)閿?shù)據(jù)少,但我們偏要用for循環(huán)來(lái)解決

先來(lái)思考for循環(huán)的三個(gè)部分

  1. 輸出

  2. 序列

  3. 函數(shù)體

輸出

注意這里的輸出是明確長(zhǎng)度的,因此可以確定下來(lái)

1output<-vector("double",ncol(df))##

序列+循環(huán)體

這里的seq_along與length類似,但要好一些

1for (i in seq_along(df)) {
2  output[[i]]<-median(df[[i]])
3}

輸出結(jié)果

1output
2## [1] -0.5227902 -0.6708582 -0.4300781 -0.3684835

如果使用output[i]索引 發(fā)現(xiàn)得到了類似的結(jié)果

1output<-vector("double",ncol(df))##
2
3for (i in seq_along(df)) {
4  output[i]<-median(df[[i]])
5}
6output
7## [1-0.5227902 -0.6708582 -0.4300781 -0.3684835

調(diào)整思路-創(chuàng)建未知長(zhǎng)度的空向量

1output<-vector()##
2
3for (i in seq_along(df)) {
4  value<-median(df[[i]])
5  output<-c(value,output)
6}
7output
8## [1-0.3684835 -0.4300781 -0.6708582 -0.5227902

for循環(huán)改裝成函數(shù)

R語(yǔ)言是一門函數(shù)式編程語(yǔ)言,這意味著可以先將for循環(huán)包裝在函數(shù)中 然后可以直接調(diào)用函數(shù),而不是直接去使用for循環(huán) 下面我們示例來(lái)改裝一下,想一個(gè)合適的函數(shù)名,因?yàn)橛?jì)算每列的中位值,可命為col_median
創(chuàng)建一個(gè)函數(shù)col_median

輸入?yún)?shù)1:df,是一個(gè)簡(jiǎn)單數(shù)據(jù)框tibble

1col_median<-function(df){
2  output<-vector()##
3  for (i in seq_along(df)) {
4  value<-median(df[[i]])
5  output<-c(value,output)
6}
7  output##函數(shù)的輸出
8}
9col_median(df)
10## [1-0.3684835 -0.4300781 -0.6708582 -0.5227902

測(cè)試一下這個(gè)函數(shù)

1###創(chuàng)建矩陣
2data<-matrix(rnorm(60),nrow = 6,ncol = 10)
3head(data)
4##              [,1]        [,2]       [,3]       [,4]       [,5]       [,6]
5## [1,] -0.821624744 -1.42055483 -1.6212754 -0.9966531  0.3076887 -1.3621218
6## [2,]  0.389617000  0.91243785 -0.1969125  2.2527410 -1.5184419 -0.9301008
7## [3,]  0.002507928  0.33528398 -0.3859504  1.9842380  0.5913518  1.1907294
8## [4,] -0.974889049  0.55289822  1.2403620 -0.2995565 -0.1231051  1.5828930
9## [5,] -0.057873471  0.01868644  0.3741498  0.8565143 -1.3681877  2.0053276
10## [6,]  0.539604156  3.16231565  0.4713369 -0.4801932  0.4394711 -0.9297590
11##            [,7]       [,8]       [,9]      [,10]
12## [1,] -0.4878761 -2.0633086 -0.9382000  1.2048067
13## [2,] -0.1664908 -0.5548023 -1.5614915  1.3281471
14## [3,] -1.1470670 -0.2582778  0.5214065  0.3797929
15## [4,] -1.1619733 -0.9926352  0.6941835 -0.2334606
16## [5,]  1.5798016 -1.3599199 -0.1201355 -0.2287048
17## [6,] -1.5974384 -0.8201751  0.2489410  0.7259488
18dim(data)
19## [1]  6 10

由于函數(shù)接受的輸入是tibble,調(diào)整數(shù)據(jù)格式為tibble

1data<-as_tibble(data)
2## Warning: `as_tibble.matrix()` requires a matrix with column names or a `.name_repairargumentUsing compatibility `.name_repair`.
3## This warning is displayed once per session.
4col_median(data)
5##  [1]  0.55287084  0.06440272 -0.90640517 -0.81747157  0.13048523
6##  [6]  0.09229177  0.27847891  0.08861867  0.44409110 -0.02768277

測(cè)試成功!

通過(guò)這個(gè)例子我們可以思考,去完成將for循環(huán)改裝為函數(shù),比如將此前的R語(yǔ)言for循環(huán)批量計(jì)算相關(guān)系數(shù)改裝成一個(gè)批量計(jì)算的函數(shù)。


    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多