My original data set has 62 variables. For variables c(3:56) I would like to loop the function boxplot.with.outlier.label, see
source(“http://www.r-statistics.com/wp-content/uploads/2011/01/boxplot-with-outlier-label-r.txt”)
But I am already stuck on building a function which would allow me to built the loop. Here is some mock up data (which, of course doesn’t show outliers, but to my knowledge this is not part of the problem – proof me wrong!)
x <- rnorm(1000)
y <- rnorm(1000)
z <- sample(letters, 1000)
df <- as.data.frame(cbind(x,y,z))
df$x<- as.numeric(df$x)
df$y<- as.numeric(df$y)
df$z<- as.character(df$z)
This works fine:
boxplot.with.outlier.label(df$x, df$z)
And this does not:
boxplotlabel <- function (data, var, name) {
datvar <- data[["var"]]
namevar <- data[["name"]]
boxplot.with.outlier.label(datvar, namevar)
}
boxplotlabel(df, x, z)
Error in plot.window(xlim = xlim, ylim = ylim, log = log, yaxs = pars$yaxs) :need finite 'ylim' values
In addition: Warning messages:
1: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'
2: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'
3: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'
4: In min(x) : no non-missing arguments to min; returning Inf
5: In max(x) : no non-missing arguments to max; returning -Inf
Where am I going wrong? Or is there a different way of achieving my desired loop for the function boxplot.with.outlier.label?
I appreciate any help! Gerit
The issue lies in the quotes.
varandnameare variables. But when you calldata[["var"]](with the quotes) you are not using the variablevarbut rather a string and the value of that string are the characters “var”.If you remove the quotes you will be halfway there. Var itself should have a string value. so make sure to pass it the name of the column, not the column itself.
eg:
Therefore, if we’re using a variable for
x: