r - 如何在R中的函数内索引数据框列

标签 r dataframe indexing dplyr

我有一个函数,它接受一个数据框、一个百分位阈值和给定列的名称,并将给定列中高于该阈值的所有值计算为一个新列(0 表示 <,1 表示>=).但是,它不允许我在 quantile 函数中执行 df$column_name 因为 column_name 实际上不是列名,而是一个存储实际列名的变量。因此 df$column_name 将返回 NULL。有没有办法解决这个问题并使代码格式与当前的格式有些相似?或者我是否必须指定实际的数字列值而不是名称?虽然我可以这样做,但它绝对不如仅传递列名那么方便/易于理解。

func1 <- function(df, threshold, column_name) {
  threshold_value <- quantile(df$column_name, c(threshold)) 
  new_df <- df %>%
    mutate(ifelse(column_name > threshold_value, 1, 0)) 
  return(new_df)
}

非常感谢您的帮助!

最佳答案

我修改了你的函数如下。现在该函数可以采用数据框、阈值和列名。这个函数只需要基数R。

# Modified function
func1 <- function(df, threshold, column_name) {
  threshold_value <- quantile(df[[column_name]], threshold) 
  new_df <- df
  new_df[["new_col"]] <- ifelse(df[[column_name]] > threshold_value, 1, 0) 
  return(new_df)
}

# Take the trees data frame as an example
head(trees)
#   Girth Height Volume
# 1   8.3     70   10.3
# 2   8.6     65   10.3
# 3   8.8     63   10.2
# 4  10.5     72   16.4
# 5  10.7     81   18.8
# 6  10.8     83   19.7

# Apply the function
func1(trees, 0.5, "Volume")
#    Girth Height Volume new_col
# 1    8.3     70   10.3       0
# 2    8.6     65   10.3       0
# 3    8.8     63   10.2       0
# 4   10.5     72   16.4       0
# 5   10.7     81   18.8       0
# 6   10.8     83   19.7       0
# 7   11.0     66   15.6       0
# 8   11.0     75   18.2       0
# 9   11.1     80   22.6       0
# 10  11.2     75   19.9       0
# 11  11.3     79   24.2       0
# 12  11.4     76   21.0       0
# 13  11.4     76   21.4       0
# 14  11.7     69   21.3       0
# 15  12.0     75   19.1       0
# 16  12.9     74   22.2       0
# 17  12.9     85   33.8       1
# 18  13.3     86   27.4       1
# 19  13.7     71   25.7       1
# 20  13.8     64   24.9       1
# 21  14.0     78   34.5       1
# 22  14.2     80   31.7       1
# 23  14.5     74   36.3       1
# 24  16.0     72   38.3       1
# 25  16.3     77   42.6       1
# 26  17.3     81   55.4       1
# 27  17.5     82   55.7       1
# 28  17.9     80   58.3       1
# 29  18.0     80   51.5       1
# 30  18.0     80   51.0       1
# 31  20.6     87   77.0       1

如果你还想用 ,学习如何处理不规范的评估是必不可少的。请参阅此处以了解更多信息 (https://cran.r-project.org/web/packages/dplyr/vignettes/programming.html)。以下代码也适用。

library(dplyr)

func2 <- function(df, threshold, column_name) {
  col_en <- enquo(column_name)
  threshold_value <- quantile(df %>% pull(!!col_en), threshold)
  new_df <- df %>%
    mutate(new_col := ifelse(!!col_en >= threshold_value, 1, 0))
  return(new_df)
}

func2(trees, 0.5, Volume)
#    Girth Height Volume new_col
# 1    8.3     70   10.3       0
# 2    8.6     65   10.3       0
# 3    8.8     63   10.2       0
# 4   10.5     72   16.4       0
# 5   10.7     81   18.8       0
# 6   10.8     83   19.7       0
# 7   11.0     66   15.6       0
# 8   11.0     75   18.2       0
# 9   11.1     80   22.6       0
# 10  11.2     75   19.9       0
# 11  11.3     79   24.2       1
# 12  11.4     76   21.0       0
# 13  11.4     76   21.4       0
# 14  11.7     69   21.3       0
# 15  12.0     75   19.1       0
# 16  12.9     74   22.2       0
# 17  12.9     85   33.8       1
# 18  13.3     86   27.4       1
# 19  13.7     71   25.7       1
# 20  13.8     64   24.9       1
# 21  14.0     78   34.5       1
# 22  14.2     80   31.7       1
# 23  14.5     74   36.3       1
# 24  16.0     72   38.3       1
# 25  16.3     77   42.6       1
# 26  17.3     81   55.4       1
# 27  17.5     82   55.7       1
# 28  17.9     80   58.3       1
# 29  18.0     80   51.5       1
# 30  18.0     80   51.0       1
# 31  20.6     87   77.0       1

关于r - 如何在R中的函数内索引数据框列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49725527/

相关文章:

r - 在 R 中使用粘贴

r - 对 R 中的数据帧中的重复列求和

python - 如何扫描 pandas 数据帧中所有大于某个值的值并返回与该值对应的行号和列号?

firebase - 我是否正确使用Firestore索引?

r - 基于整数按因子排序 data.frame

r - XGBoost 上的 AUC 指标

r - 在 TO 覆盖一个函数中,我如何取回它?

python - 将具有不同名称的多列上的数据帧从宽格式转换为长格式

mongodb - 如何在 MongoDB 集合中存储描述文档的元数据?

mysql - 如何使用 MySQL 有条件地创建索引?