【发布时间】:2021-08-31 01:48:40
【问题描述】:
我想在同一个循环中运行两个不同的函数,并使用特定名称 df.rescale01 和 df.rescale.02 保存结果。非常感谢。
df <- tibble( a = rnorm(10),b = rnorm(10))
rescale01 <- function(x) {
rng <- range(x, na.rm = TRUE)
(x - rng[1]) / (rng[2] - rng[1])
}
rescale02 <- function(x) {
rng <- range(x, na.rm = TRUE)
(rng[2] - rng[1]) / (x - rng[1])
}
for (i in seq_along(df)) {
df[[i]] <- rescale01(df[[i]])
}
df
预期的答案
rescale01
A tibble: 10 x 2
a b
<dbl> <dbl>
1 0.354 0.627
2 0.657 0.545
3 0 0.397
4 0.602 0
5 0.539 0.698
6 0.775 1
7 1 0.216
8 0.949 0.286
9 0.782 0.244
10 0.239 0.523
rescale02
A tibble: 10 x 2
a b
<dbl> <dbl>
1 2.83 1.59
2 1.52 1.83
3 Inf 2.52
4 1.66 Inf
5 1.85 1.43
6 1.29 1
7 1 4.63
8 1.05 3.49
9 1.28 4.10
10 4.18 1.91
【问题讨论】:
标签: r for-loop nested-loops data-manipulation