首页 > 解决方案 > 返回新数据框,其中包含在 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 列的此功能,因此这不是最佳选择。请帮忙。

标签: rdataframe

解决方案


您应该将新列名作为字符串传递。

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

推荐阅读