我尝试使用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)
一些搜索似乎表明我的语法是正确的,但我显然有什么地方错了。我做错了什么,我该如何解决?
真诚的,并预先表示感谢
托马斯·菲利普斯
1条答案
按热度按时间jvidinwx1#
问题是
value_to_classification
不适用于向量。你可以运行value_to_classification(c(1,2,3))
,它只返回一个值(而不是3)。一种解决方案是将函数向量化: