r - 返回新数据框,其中包含在 R 中使用用户给定名称的函数内创建的列
问题描述
请在下面查看我的代码:
# functions to get percentile threshold, and assign new values to outliers
get_low_perc <- function(var_name) {
return(quantile(var_name, c(0.01)))
}
get_hi_perc <- function(var_name) {
return(quantile(var_name, c(0.99)))
}
round_up <- function(target_var, flag_var, floor) {
target_var <- as.numeric(ifelse(flag_var == 1, floor, target_var))
return(as.integer(target_var))
}
round_down <- function(target_var, flag_var, ceiling) {
target_var <- as.numeric(ifelse(flag_var == 1, ceiling, target_var))
return(as.integer(target_var))
}
# try putting it all together
no_way <- function(df, df_col_name, df_col_flagH, df_col_flagL) {
lo_perc <- get_low_perc(df_col_name)
hi_perc <- get_hi_perc(df_col_name)
df$df_col_flagH <- as.factor(ifelse(df_col_name < lo_perc, 1, 0))
df$df_col_flagL <- as.factor(ifelse(df_col_name > hi_perc, 1, 0))
df_col_name <- round_up(df_col_name, df_col_flagL, lo_perc)
df_col_name <- round_down(df_col_name, df_col_flagH, hi_perc)
# names(df)[names(df)=='df_col_flagH'] <-
# boxplot(df_col_name)
return(df)
}
我创建了 5 个自定义函数;前两个分别获得给定变量的第 1 个百分位和第 99 个百分位。最后两个根据它们与第 1 个百分位值和第 99 个百分位值的距离来向上或向下舍入这些变量中的值。最后一个函数试图将所有这些函数放在一起,以基本上输出一个新的数据帧,其中包含原始 df 中的相同列、更新的列和两个新列,这些列指示标记为低于第 1 个百分位和高于第 99 个百分位的值。我在下面生成了一个模拟数据框,因为我似乎无法在这里传递我的一些数据。
df2 = data.frame(col = c(1, 3, 4, 5, 8, 7, 67, 744, 876, 8, 8, 54, 9),
col1 = c(9, 6, 8, 3, 4, 5, 8, 7, 67, 744, 87, 33, 77),
col2 = c(8, 2, 8, 4, 87, 66, 54, 99, 77, 77, 88, 67, 102))
理想情况下,在我使用命令“no_way(df2, df2$col1, df2$new_col1, df2$new_col2)”调用该函数后,我希望输出数据帧如下所示:
df2 = data.frame(col = c(1, 3, 4, 5, 8, 7, 67, 744, 876, 8, 8, 54, 9),
col1 = c(9, 6, 8, 3, 4, 5, 8, 7, 67, 744, 87, 33, 77), # updated with appropriate values
col2 = c(8, 2, 8, 4, 87, 66, 54, 99, 77, 77, 88, 67, 102),
new_col1 = c(0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0),
new_col2 = c(0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0))
^ 其中 new_col1 和 new_col2 是用户在调用函数时给出的列名。我目前正在按预期获取数据框,但是创建的新列保留了函数参数的名称,如下所示:
df2 = data.frame(col = c(1, 3, 4, 5, 8, 7, 67, 744, 876, 8, 8, 54, 9),
col1 = c(9, 6, 8, 3, 4, 5, 8, 7, 67, 744, 87, 33, 77), # updated with appropriate values
col2 = c(8, 2, 8, 4, 87, 66, 54, 99, 77, 77, 88, 67, 102),
df_col_flagH = c(0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0),
df_col_flagL = c(0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0))
我不介意之后更改列的名称,但我将使用 17 列的此功能,因此这不是最佳选择。请帮忙。
解决方案
您应该将新列名作为字符串传递。
也ifelse(condition, 1, 0)
可以简化为as.integer(condition)
。
no_way <- function(df, df_col_name, df_col_flagH, df_col_flagL) {
lo_perc <- get_low_perc(df[[df_col_name]])
hi_perc <- get_hi_perc(df[[df_col_name]])
df[[df_col_flagH]] <- as.factor(as.integer(df[[df_col_name]] < lo_perc))
df[[df_col_flagL]] <- as.factor(as.integer(df[[df_col_name]] > hi_perc))
df[[df_col_name]] <- round_up(df[[df_col_name]], df_col_flagL, lo_perc)
df[[df_col_name]] <- round_down(df[[df_col_name]], df_col_flagH, hi_perc)
return(df)
}
df2 <- no_way(df2, "col1", "new_col1", "new_col2")
df2
# col col1 col2 new_col1 new_col2
#1 1 9 8 0 0
#2 3 9 2 0 0
#3 4 9 8 0 0
#4 5 9 4 1 0
#5 8 9 87 0 0
#6 7 9 66 0 0
#7 67 9 54 0 0
#8 744 9 99 0 0
#9 876 9 77 0 0
#10 8 9 77 0 1
#11 8 9 88 0 0
#12 54 9 67 0 0
#13 9 9 102 0 0
推荐阅读
- amazon-web-services - 对象存储不支持编辑/更新/追加文件的根本原因是什么?
- javascript - jQuery:无法使用 RegExp 精确搜索“+3.00”或“-3.00”
- r - 在数据框中找到相应的“父亲”行
- java - Oracle 查询 - 使用 JOIN 在两个表中过滤和限制
- google-cloud-platform - Google Cloud Platform 不会停止加载
- ruby-on-rails - rails 关联:用户必须存在问题
- android - 使用带有匕首刀柄的类型转换器时出错
- javascript - 每行的 ag 网格编辑颜色取决于其键
- python - 在 Python 头部姿态估计 MTCNN 中计算 Yaw 和 Roll
- php - 使用 PHP8 设置 Sylius:如何使用最新的 Sylius Core 使用 sylius 标准版设置新项目?