Error in hist.default(children) : 'x' must be numeric.


I have 16 variables/columns, and I'm trying to make a histogram for all of them at once to explore normality. The variables are all numeric which I have confirmed in R. However with my code below it gives me the above error.

hist(Dar[,c(uco_ugl,uni_ugl, uzn_ugl, ucu_ugl, usr_ugl, umo_ugl, ucs_ugl, uba_ugl, uhg_ugl, upb_ugl, uv_ugl, ucr_ugl, umn_ugl, ufe_ugl, use_ugl, uas_ugl, ucd_ugl)])

Could someone please help?


The hist() function plots a numeric vector not a data frame. You can use the unlist() function to make a vector from the data frame. The code below shows that plotting a data frame causes the error but that unlist() fixes the problem.

DAR <- data.frame(X=rnorm(20),Y=rnorm(20),Z=rnorm(20))
#> Error in hist.default(DAR[, c("X", "Y", "Z")]): 'x' must be numeric


Created on 2022-09-22 with reprex v2.0.2

Thanks for responding! I'm getting a single histogram plot for all the variables. I wanted separate plots for each.

I think that would be easier if you reshape the data with the tidyr package and plot it with ggplot. Something like this:

pivot_longer(Dar, cols =  uco_ugl:ucd_ugl,
             names_to = "Variable", values_to = "Value") |> 
  ggplot(aes(Value)) + geom_histogram(fill="skyblue", color="white")+

Great, this works perfectly. Thank you so much!

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.