-1

I have the following dataenter image description here

I have tried plotting one column with the following command and it works. However, when attempting to plot a few columns, the command does not work

#this works but only for one column boxplot(as.numeric(new[[2]]), horizontal = T, col = "lightblue", notch = T, main="Heart Failure Mortality")

#does not work, I am trying to get the first 3 columns boxplot(as.numeric(new[[c(1:3)]]))

smci
  • 32,567
  • 20
  • 113
  • 146
Kode.Error404
  • 573
  • 5
  • 24
  • Screenshot is not so helpful. Please use `dput(head(new, 10))` and dump us a [Minimum Complete Reproducible Example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). And when you say *"does not work"*, show us the error you get. – smci Nov 20 '16 at 05:41
  • `new[[2]]` extracts one dataframe column as a vector, but you can't generalize that to `new[[c(1:3)]]`, you want `new[,c(1:3)]`. Please read the [`?'['` page](https://stat.ethz.ch/R-manual/R-devel/library/base/html/Extract.html) on the `[..]` operator. – smci Nov 20 '16 at 05:44
  • Also, if these columns are non-numeric due to `read.csv()`, see the use of `read.csv(..., colClasses)` so they read in right the first time and you don't even have to do conversion. – smci Nov 20 '16 at 05:47

1 Answers1

0

You don't show what the structure of yournew is, and your syntax indicates that you are trying to treat its columns as list elements.

Just make sure that it is a data frame with numeric columns.

boxplot(iris[,1:4])

gives boxplots of the 4 numeric columns of this data frame

user101089
  • 3,756
  • 1
  • 26
  • 53
  • This works on iris but it does not work on mine `boxplot(as.data.frame(new[,1:3]))` – Kode.Error404 Nov 20 '16 at 04:27
  • 1
    `boxplot(sapply(new[,1:3], as.numeric))`. If the columns need to be converted to numeric format, it needs to be done on each column individually. You can use `sapply` to iterate over each column. – eipi10 Nov 20 '16 at 04:36