-2

for the below code, i am getting an error:

boxplot(diamonds$carat, diamonds$cut, diamonds$depth, diamonds$table, diamonds$x,
        diamonds$y, diamonds$z)
boxplot(diamonds$carat, diamonds$cut, diamonds$depth, diamonds$table, diamonds$x, 
        diamonds$y, diamonds$z)$out
outliers <- boxplot(diamonds$carat, diamonds$cut, diamonds$depth, diamonds$table, diamonds$x, 
                    diamonds$y, diamonds$z, plot = FALSE)$out diamonds[which(diamonds$carat, diamonds$cut, diamonds$depth, diamonds$table, diamonds$x, diamonds$y, diamonds$z %in% outliers),]
Newdata <- diamonds[-which(diamonds$carat, diamonds$cut, diamonds$depth, diamonds$table,
                           diamonds$x, diamonds$y, diamonds$z %in% outliers),] 

error is

Error in which(diamonds$carat, diamonds$depth, diamonds$table, diamonds$x, : unused arguments (diamonds$x, diamonds$y, diamonds$z %in% outliers)

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 1
    `which` returns column indices and you're subsetting rows. Also it's unclear what is contained in `outliers`. Too many arguments. Could you post the full workflow? Regarding the usage of `which`: https://stackoverflow.com/questions/6918657/whats-the-use-of-which – NelsonGon Feb 07 '19 at 03:45
  • boxplot(diamonds$carat,diamonds$cut,diamonds$depth,diamonds$table,diamonds$x,diamonds$y,diamonds$z) boxplot(diamonds$carat,diamonds$cut,diamonds$depth,diamonds$table,diamonds$x,diamonds$y,diamonds$z)$out outliers <-boxplot(diamonds$carat,diamonds$cut,diamonds$depth,diamonds$table,diamonds$x,diamonds$y,diamonds$z, plot = FALSE)$out diamonds[which(diamonds$carat,diamonds$cut,diamonds$depth,diamonds$table,diamonds$x,diamonds$y,diamonds$z %in% outliers),] Newdata <- diamonds[-which(diamonds$carat,diamonds$cut,diamonds$depth,diamonds$table,diamonds$x,diamonds$y,diamonds$z %in% outliers),] – Rishabh Garg Feb 07 '19 at 03:51
  • `outliers` is a vector, you need to select only one variable when using `%in% outliers`. `Newdata <- diamonds[-which(diamonds$carat %in% outliers),]` – demarsylvain Feb 07 '19 at 04:02
  • 1
    Rishabh, generally posting code (especially that long) in a comment is not very useful. Whenever you add some context or meaning to the question, it is typically better to edit the question directly, either replacing/modifying or adding to it. – r2evans Feb 07 '19 at 04:06
  • You want to display the outliers or u want to check the outlier values? – sai saran Feb 07 '19 at 04:42

1 Answers1

0

Here is a sample solution: First we select our columns of interest

subset_df<diamonds[,which(names(diamonds)%in%c("carat","cut","depth","x","y","z","table"))]

Next, we define what defines an outlier. I've set that to any value greater than 5.

outliers<-subset_df[which(subset_df$z>=5),]  

Finally, we make our boxplot.

boxplot(outliers)

Non-Outliers: I would go with ggplot2 for reasons of preference.

non_outliers<-subset_df[which(subset_df$z<5),]
boxplot(non_outliers)

NOTE:

Considering the data is from ggplot2, you might want to consider dplyr and ggplot2 for a "smoother" workflow(opinion based).

NelsonGon
  • 13,015
  • 7
  • 27
  • 57