2017-05-12 41 views
0

我試圖根據過濾列更新函數內的數據框列。使用列參數在函數內過濾數據框

#example dataframe 
my.df = data.frame(A=1:10) 

#define function to classify column passed as argument 2 based on argument 3 
classify = function(df, col, threshold){ 
    df[df$col<threshold, 2] <- "low" 
    df[df$col>=threshold, 2] <- "high" 

    return(df) 
} 

#assign output to new.df 
new.df = classify(my.df, A, 5) 

我期望新列包含「低」或「高」的字符值,而是他們都<NA>

+0

發生變異(my.df,B = ifelse(A <2, '低', '高'))? –

回答

0

這將在mutate/group_by/filter等內進行評估簡單地傳遞字符串文字列名的,"A",然後在函數內部接收參數與單或雙托架[[...]]索引和不與$

# example dataframe 
my.df = data.frame(A=1:10) 

# define function to classify column passed as argument 2 based on argument 3 
classify = function(df, col, threshold){ 
    df[df[[col]] < threshold, 2] <- "low" 
    df[df[[col]] >= threshold, 2] <- "high" 

    return(df) 
} 

# assign output to new.df 
new.df = classify(my.df, "A", 5) 

new.df  
#  A V2 
# 1 1 low 
# 2 2 low 
# 3 3 low 
# 4 4 low 
# 5 5 high 
# 6 6 high 
# 7 7 high 
# 8 8 high 
# 9 9 high 
# 10 10 high 
0

我們可以使用dplyr的devel版本(即將發佈0.6.0)來執行此操作。所述enquo獲取輸入參數,並將其轉換爲quosure由unquoting(UQ

library(dplyr) 
classify <- function(df, col, threshold){ 
    col <- enquo(col) 

    df %>% 
     mutate(categ = ifelse(UQ(col) < threshold, "low", "high")) 

} 

classify(my.df, A, 5) 
# A categ 
#1 1 low 
#2 2 low 
#3 3 low 
#4 4 low 
#5 5 high 
#6 6 high 
#7 7 high 
#8 8 high 
#9 9 high 
#10 10 high