美文网首页R
R语言简单for循环(二)

R语言简单for循环(二)

作者: 医科研 | 来源:发表于2019-06-27 12:12 被阅读55次

创建一个简单数据框

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

元素提取的差异 df[1]与df[[1]]

提取第一列

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

提取第一列的元素操作

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

实现一个需求:要求使用求出每一列的中位值
当然这个需求可以用简单的代码实现,因为数据少,但我们偏要用for循环来解决

先来思考for循环的三个部分

1. 输出

2. 序列

3. 函数体

输出

注意这里的输出是明确长度的,因此可以确定下来

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

序列+循环体

这里的seq_along与length类似,但要好一些

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

输出结果

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

如果使用output[i]索引 发现得到了类似的结果

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

for (i in seq_along(df)) {
  output[i]<-median(df[[i]])
}
output
## [1] -0.5227902 -0.6708582 -0.4300781 -0.3684835

调整思路-创建未知长度的空向量

output<-vector()##

for (i in seq_along(df)) {
  value<-median(df[[i]])
  output<-c(value,output)
}
output
## [1] -0.3684835 -0.4300781 -0.6708582 -0.5227902

for循环改装成函数

R语言是一门函数式编程语言,这意味着可以先将for循环包装在函数中 然后可以直接调用函数,而不是直接去使用for循环 下面我们示例来改装一下,想一个合适的函数名,因为计算每列的中位值,可命为col_median
创建一个函数col_median

输入参数1:df,是一个简单数据框tibble

col_median<-function(df){
  output<-vector()##
  for (i in seq_along(df)) {
  value<-median(df[[i]])
  output<-c(value,output)
}
  output##函数的输出
}
col_median(df)
## [1] -0.3684835 -0.4300781 -0.6708582 -0.5227902

测试一下这个函数

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

由于函数接受的输入是tibble,调整数据格式为tibble

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

测试成功!

通过这个例子我们可以思考,去完成将for循环改装为函数,比如将此前的R语言for循环批量计算相关系数改装成一个批量计算的函数。

相关文章

  • R语言简单for循环(二)

    创建一个简单数据框 元素提取的差异 df[1]与df[[1]] 提取第一列 提取第一列的元素操作 实现一个需求:要...

  • R语言for循环①

    for循环是一种重复控制结构,可以让您有效地编写一个需要执行特定次数的循环。 示例: 它产生如下结果: 下标访问 ...

  • R语言 循环

    可能有一种情况,当你需要执行一段代码几次。 通常,顺序执行语句。 首先执行函数中的第一个语句,然后执行第二个语句,...

  • R语言:循环

    这里介绍五种R语言的循环语法,分别是: for if repeat which while for if repe...

  • R语言流程控制

    都知道R语言是向量式编程,可是有时候确实需要用到循环语句,这里就介绍R语言里面的主要循环——for循环。 与其它语...

  • R语言for循环练习

    for循环在R中很常见也很重要,虽然很简单,但是一定要理解其中的思想。以R语言初级作业第9题为例。加数据加载到R中...

  • R语言 循环作图

  • R语言for循环练习

    Assignment We obtained the readcount and tpm matrix of mi...

  • R语言-0基础学习4-实战1-常见操作

    R语言学习系列R语言-0基础学习1-数据结构R语言-0基础学习2-构建子集R语言-0基础学习3-循环排序信息处理函...

  • R purrr_ for循环与函数式编程

    for 循环在R中不像其他语言中那么重要,因为R是一门函数式编程的语言。这意味着可以先将for 循环封装在函数里面...

网友评论

    本文标题:R语言简单for循环(二)

    本文链接:https://www.haomeiwen.com/subject/bswlcctx.html