使用 R 在循环中绑定向量
bind vectors in a loop using R
我需要在数据框中添加 remove_outliers
函数返回的向量[属性]。现在,我得到了一个大矩阵。我试过追加方法(如下)
# function to calculate IQR and upper and lower limit of given attribute
remove_outliers <- function(attribute, na.rm = TRUE, ...) {
IQR_val <- quantile(attribute, probs=c(.25, .75), na.rm = na.rm, ...)
LF <- 1.5 * IQR(attribute, na.rm = na.rm)
attribute_W_NA <- attribute
attribute_W_NA[attribute < (IQR_val[1] - LF)] <- NA
attribute_W_NA[attribute > (IQR_val[2] + LF)] <- NA
attribute_W_NA
}
cleaned_data <- NULL
for(i in 1:ncol(data_rm_val)){
# cleaned data with NA entries replacing outliers
cleaned_data <- cbind(cleaned_data, remove_outliers(data_rm_val[,i]))
}
它导致大矩阵
这是输入数据框:
当前输出是:(在循环中使用 cbind)
并且期望的结果应该是具有相同行数和列数的数据框。
如有任何帮助,我们将不胜感激。
PS: 我是 R 和数据科学的新手。
只需将 lapply
与您的用户定义函数一起使用即可避免绑定或追加。在数据框上使用 lapply()
时,您对每一列进行 运行 操作:
cleaned_data <- data.frame(lapply(data_rm_val, remove_outliers))
现在上面假设你定义的函数,remove_outliers returns 一个向量类型。为确保向量始终输出,请考虑 vapply()
定义等于输入的长度或 nrow(data_rm_val)
:
cleaned_data <- data.frame(vapply(data_rm_val, remove_outliers, numeric(nrow(data_rm_val))))
以上两个选项适用于随机数数据集(因为 OP 不提供示例数据):
data_rm_val <- data.frame(matrix(rnorm(25),5))
# X1 X2 X3 X4 X5
# 1 0.4303766 1.8152041 0.3355174 -0.4880282 -0.63612820
# 2 0.2876950 -0.7613642 -1.5046115 0.1821653 0.09397964
# 3 -2.3402548 -0.6771749 -2.0122667 -0.9442210 -1.30994853
# 4 1.4224979 -1.7940421 -0.5110736 -0.2837820 -0.24240172
# 5 -0.7484131 -0.8159326 -1.2690513 -1.0422656 1.23811458
cleaned_data <- data.frame(lapply(data_rm_val, remove_outliers))
# X1 X2 X3 X4 X5
# 1 0.4303766 NA 0.3355174 -0.4880282 -0.63612820
# 2 0.2876950 -0.7613642 -1.5046115 0.1821653 0.09397964
# 3 -2.3402548 -0.6771749 -2.0122667 -0.9442210 -1.30994853
# 4 1.4224979 NA -0.5110736 -0.2837820 -0.24240172
# 5 -0.7484131 -0.8159326 -1.2690513 -1.0422656 NA
cleaned_data2 <- data.frame(vapply(data_rm_val,
remove_outliers, numeric(nrow(data_rm_val))))
# X1 X2 X3 X4 X5
# 1 0.4303766 NA 0.3355174 -0.4880282 -0.63612820
# 2 0.2876950 -0.7613642 -1.5046115 0.1821653 0.09397964
# 3 -2.3402548 -0.6771749 -2.0122667 -0.9442210 -1.30994853
# 4 1.4224979 NA -0.5110736 -0.2837820 -0.24240172
# 5 -0.7484131 -0.8159326 -1.2690513 -1.0422656 NA
我需要在数据框中添加 remove_outliers
函数返回的向量[属性]。现在,我得到了一个大矩阵。我试过追加方法(如下)
# function to calculate IQR and upper and lower limit of given attribute
remove_outliers <- function(attribute, na.rm = TRUE, ...) {
IQR_val <- quantile(attribute, probs=c(.25, .75), na.rm = na.rm, ...)
LF <- 1.5 * IQR(attribute, na.rm = na.rm)
attribute_W_NA <- attribute
attribute_W_NA[attribute < (IQR_val[1] - LF)] <- NA
attribute_W_NA[attribute > (IQR_val[2] + LF)] <- NA
attribute_W_NA
}
cleaned_data <- NULL
for(i in 1:ncol(data_rm_val)){
# cleaned data with NA entries replacing outliers
cleaned_data <- cbind(cleaned_data, remove_outliers(data_rm_val[,i]))
}
它导致大矩阵
这是输入数据框:
当前输出是:(在循环中使用 cbind)
并且期望的结果应该是具有相同行数和列数的数据框。
如有任何帮助,我们将不胜感激。 PS: 我是 R 和数据科学的新手。
只需将 lapply
与您的用户定义函数一起使用即可避免绑定或追加。在数据框上使用 lapply()
时,您对每一列进行 运行 操作:
cleaned_data <- data.frame(lapply(data_rm_val, remove_outliers))
现在上面假设你定义的函数,remove_outliers returns 一个向量类型。为确保向量始终输出,请考虑 vapply()
定义等于输入的长度或 nrow(data_rm_val)
:
cleaned_data <- data.frame(vapply(data_rm_val, remove_outliers, numeric(nrow(data_rm_val))))
以上两个选项适用于随机数数据集(因为 OP 不提供示例数据):
data_rm_val <- data.frame(matrix(rnorm(25),5))
# X1 X2 X3 X4 X5
# 1 0.4303766 1.8152041 0.3355174 -0.4880282 -0.63612820
# 2 0.2876950 -0.7613642 -1.5046115 0.1821653 0.09397964
# 3 -2.3402548 -0.6771749 -2.0122667 -0.9442210 -1.30994853
# 4 1.4224979 -1.7940421 -0.5110736 -0.2837820 -0.24240172
# 5 -0.7484131 -0.8159326 -1.2690513 -1.0422656 1.23811458
cleaned_data <- data.frame(lapply(data_rm_val, remove_outliers))
# X1 X2 X3 X4 X5
# 1 0.4303766 NA 0.3355174 -0.4880282 -0.63612820
# 2 0.2876950 -0.7613642 -1.5046115 0.1821653 0.09397964
# 3 -2.3402548 -0.6771749 -2.0122667 -0.9442210 -1.30994853
# 4 1.4224979 NA -0.5110736 -0.2837820 -0.24240172
# 5 -0.7484131 -0.8159326 -1.2690513 -1.0422656 NA
cleaned_data2 <- data.frame(vapply(data_rm_val,
remove_outliers, numeric(nrow(data_rm_val))))
# X1 X2 X3 X4 X5
# 1 0.4303766 NA 0.3355174 -0.4880282 -0.63612820
# 2 0.2876950 -0.7613642 -1.5046115 0.1821653 0.09397964
# 3 -2.3402548 -0.6771749 -2.0122667 -0.9442210 -1.30994853
# 4 1.4224979 NA -0.5110736 -0.2837820 -0.24240172
# 5 -0.7484131 -0.8159326 -1.2690513 -1.0422656 NA