首页 > 编程知识 正文

r语言中for,r语言循环语句

时间:2023-05-04 19:02:41 阅读:238863 作者:2060

创建一个简单数据框

Sys.setlocale('LC_ALL','C')

## [1] "C"

library(tidyverse)

## -- Attaching packages ---------------------------------------------------- tidyverse 1.2.1 --

## ggplot2 3.1.0 purrr 0.3.0

## tibble 2.0.1 dplyr 0.8.0.1

## tidyr 0.8.2 stringr 1.4.0

## readr 1.3.1 forcats 0.4.0

## -- Conflicts ------------------------------------------------------- tidyverse_conflicts() --

## x dplyr::filter() masks stats::filter()

## x dplyr::lag() masks stats::lag()

df

a=rnorm(10),

b=rnorm(10),

c=rnorm(10),

d=rnorm(10)

)

df

## # A tibble: 10 x 4

## a b c d

##

## 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

##

## 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

序列+循环体

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

for (i in seq_along(df)) {

output[[i]]

}

输出结果

output

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

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

output

for (i in seq_along(df)) {

output[i]

}

output

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

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

output

for (i in seq_along(df)) {

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

output

for (i in seq_along(df)) {

value

output

}

output##函数的输出

}

col_median(df)

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

测试一下这个函数

##创建矩阵

data

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

## 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循环批量计算相关系数改装成一个批量计算的函数。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。