在R中使用lapply与在全局环境中使用常量的函数一起出现问题

f87krz0w  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(146)

我尝试使用lapply将一个函数应用于一个dataframe,该函数从一个向量(BASELINE_CLASSIFICATION_THRESHOLDS)中保存的全局环境中获取一些输入。本质上,它将数字转换为级别(轻度,中度,严重,极端):

BASELINE_CLASSIFICATION_THRESHOLDS  <- c(0, 3.5, 6.5, 10.0000001)

value_to_classification <- function(x){

  if((x >= BASELINE_CLASSIFICATION_THRESHOLDS[1]) && (x < BASELINE_CLASSIFICATION_THRESHOLDS[2])){
    classification <- "Mild"
  } else if((x >= BASELINE_CLASSIFICATION_THRESHOLDS[2]) && (x < BASELINE_CLASSIFICATION_THRESHOLDS[3])){
    classification <- "Moderate"
  } else if((x >= BASELINE_CLASSIFICATION_THRESHOLDS[3]) && (x < round(BASELINE_CLASSIFICATION_THRESHOLDS[4]))){
    classification <- "Severe"
  } else {
    classification <- "Extreme"
  }
  return(classification)
}

df <- data.frame(x = runif(10, min = 0, max = 10),
                 y = runif(10, min = 0, max = 10),
                 z = runif(10, min = 0, max = 10))

但是当我尝试将value_to_classification应用到x列时,我得到一个错误:

lapply(df["x"], value_to_classification)
$x
[1] "Mild"

Warning messages:
1: In (x >= BASELINE_CLASSIFICATION_THRESHOLDS[1]) && (x < BASELINE_CLASSIFICATION_THRESHOLDS[2]) :
  'length(x) = 10 > 1' in coercion to 'logical(1)'
2: In (x >= BASELINE_CLASSIFICATION_THRESHOLDS[1]) && (x < BASELINE_CLASSIFICATION_THRESHOLDS[2]) :
  'length(x) = 10 > 1' in coercion to 'logical(1)'

另一方面,如果我写

lapply(df[["x"]], value_to_classification)

我最终想做的是写一些类似于

df[c("x1", "x2")] <-  lapply(df[c("x", "y")], value_to_classification)

一些搜索似乎表明我的语法是正确的,但我显然有什么地方错了。我做错了什么,我该如何解决?
真诚的,并预先表示感谢
托马斯·菲利普斯

jvidinwx

jvidinwx1#

问题是value_to_classification不适用于向量。你可以运行value_to_classification(c(1,2,3)),它只返回一个值(而不是3)。
一种解决方案是将函数向量化:

vectorized_value_to_classification <- Vectorize(value_to_classification)

df[c("x1", "x2")] <-  lapply(df[c("x", "y")], vectorized_value_to_classification)

df
          x        y         z       x1       x2
1  3.233599 5.612147 2.9525939     Mild Moderate
2  5.453014 3.659298 8.1642952 Moderate Moderate
3  7.104259 6.333049 7.1706136   Severe Moderate
4  4.199447 3.277607 8.9458447 Moderate     Mild
5  9.352140 7.135801 2.6721405   Severe   Severe
6  7.682951 1.358830 4.2102313   Severe     Mild
7  6.551999 9.986188 1.9995422   Severe   Severe
8  7.436272 9.260056 0.1093833   Severe   Severe
9  5.163593 7.689474 0.2999034 Moderate   Severe
10 7.500994 4.599129 8.5266752   Severe Moderate

相关问题