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>
。
發生變異(my.df,B = ifelse(A <2, '低', '高'))? –