Not able to pass variable names correctly in for loop or use lapply functions.
When I try this command without loop/laaply it works and I get values:
> boxplot.stats(df$price)$out
[1] 38.7 43.8 41.3 50.0 50.0 50.0 50.0 37.2 39.8 37.9 50.0
[12] 50.0 42.3 48.5 50.0 44.8 50.0 37.6 46.7 41.7 48.3 42.8
[23] 44.0 50.0 43.1 48.8 50.0 43.5 45.4 46.0 50.0 37.3 50.0
[34] 50.0 50.0 50.0 50.0
But when I put this under a lapply or for-loop
then I get Null, why ?
df_numeric_names <- names(select_if(df, is.numeric))
df_numeric_names
[1] "price" "resid_area" "air_qual" "room_num" "age" "dist1" "dist2" "dist3"
[9] "dist4" "teachers" "poor_prop" "n_hos_beds" "n_hot_rooms" "rainfall" "parks" "Sold"
loop
for (feature in df_numeric_names){
outlier_values <- boxplot.stats(df$feature)$out
print(outlier_values)
}
- Output:
NULL
NULL
NULL
lapply
lapply(df_numeric_names, function(x) {
boxplot.stats(df$x)$out
})
- output
[[1]]
NULL
[[2]]
NULL
[[3]]
NULL
[[4]]
NULL
[[5]]
NULL
This is a fairly simple thing but I am not sure what am I doing wrong and how do I fix.