12

I'm a new user in R and I've just started to work with it to see the distribution of my data but I got stuck on this error. I have a data frame and I would like to plot histograms of it's numeric columns. So what I did is as bellow :

num_data <-my_data[, sapply(my_data, is.numeric)] 
for (i in 1:length(names(num_data))){
  print(i)
  hist( num_data[i], main='hist', breaks=20, prob=TRUE)
}

But I get the error 'Error in hist.default(num_data[i], main = "hist", breaks = 20, prob = TRUE) : 'x' must be numeric ' I checked the type of num_data[i] and it is a list of numeric values. SO I have no idea of what is the problem. Can any one please give me hint?

IRTFM
  • 258,963
  • 21
  • 364
  • 487
Alex
  • 1,914
  • 6
  • 26
  • 47

3 Answers3

11

A side by side ggplot solution.

library(ggplot2)
library(tidyr)
ggplot(gather(num_data, cols, value), aes(x = value)) + 
       geom_histogram(binwidth = 20) + facet_grid(.~cols)
Psidom
  • 209,562
  • 33
  • 339
  • 356
  • 4
    This fails if there are factors in the df as well. To fix this you could do instead `iris %>% select_if(is.numeric) %>% gather(cols, value) %>% ggplot(aes(x = value)) + geom_histogram() + facet_grid(.~cols)` – Holger Brandl May 01 '19 at 06:19
4

More reliable than hist() is the histogram function from the Hmisc package:

library(Hmisc)
hist.data.frame(num_data)

This should print histograms for all columns in your dataframe.

Agile Bean
  • 6,437
  • 1
  • 45
  • 53
-1

Convert the Dataframe in the matrix. Suppose that you have data frame file say my data then use the following command:

new_data=data.matrix(mydata)
hist(new_data)  
  • When I tried this it just gathered all the columns into a single dataset with no way to distinguish them. – abalter Dec 14 '18 at 09:23