user1723699
user1723699

Reputation: 1091

Can't draw histogram using dplyr data manipulation: 'x' must be numeric

I am new to using R for data manipulation, specifically dplyr and I was trying to write a line of code to produce a histogram and it gives me an error that 'x' must be numeric but from what I can tell 'x' is numeric.

A version of the code I am trying to run is as follows with the corresponding error:

mydata %>% filter(Type==1) %>% select(Amount) %>% hist()
Error in hist.default(.) : 'x' must be numeric

If I change this to a boxplot like as follows it works perfectly fine:

mydata %>% filter(Type==1) %>% select(Amount) %>% boxplot()

If I just run:

mydata %>% filter(Type==1) %>% select(Amount)

I get the results below showing the value as a dbl:

# A tibble: 898 x 1
Amount
  <dbl>
 1 -1304.  
 2  -741.  
 3   -38.0 
 4    -1.13
 5     0.  
 6     0.  
 7     0.  
 8     0.  
 9     0.  
10     0.  
# ... with 888 more rows**strong text**

Also, if I run the following code I get the histogram I am looking for but I am not sure why my original line of code does not work.

tmp <- mydata %>% filter(Type==1) %>% select(Amount)
hist(tmp$Amount)

I am sure this is something simple that I am missing but I thought that using the select(Amount) statement would pass that value into the hist() function but that does not appear to be happening. The weird part to me is I can change it to a boxplot and it works fine. Any ideas of what is wrong with my original code?

Upvotes: 1

Views: 911

Answers (1)

akuiper
akuiper

Reputation: 215057

From ?hist and ?boxplot, hist accepts a numeric vector as data while boxplot can accept both numeric vector or lists.

To return a numeric vector from pipe, you can use pull to extract the column as a vector, which can be then passed to hist for plotting:

mydata %>% filter(Type==1) %>% pull(Amount) %>% hist()

Upvotes: 2

Related Questions