'x must be numeric' when plotting histogram

Hi everyone,

When trying to plot a histogram for a class project, I keep getting an error message saying 'x must be numeric.' I am trying to plot a histogram of Democratic vote share (which I created a variable for as dem_pct) in open-seat races (df$inc_D==0 & df$inc_R==0) and races with a Democratic incumbent (df$inc_D==1). I checked to see which variables are coming back as non-number, and that is the "state" column, which I expected. I have been trying to create a new variable "Dem_Inc" to represent the vote share during years with Dem Incumbents, with OpenSeat meant to represent years without an incumbent.

I have the following code for a histogram I am trying to use:

hist(my_data$my_variable, breaks = 20, main= "PLOT TITLE", xlab= "X-AXIS TITLE")

Here are the variables in this dataset:

> names(df)
[1] "state"  "year"   "dist"  
[4] "vote_D" "inc_D"  "vote_R"
[7] "inc_R" 
``
Here is my full code
`library(tidyverse)
library(dplyr)
df <- read.csv("C:/Users/brand/downloads/house.csv")
dem_pct <- 100*df$vote_D/(df$vote_D + df$vote_R)
mean(dem_pct)
mean(dem_pct[df$inc_D==1])
mean(dem_pct[df$inc_R==1])
mean(dem_pct[df$inc_D==0 & df$inc_R==0])
filter(df, df$inc_D==1,)
filter(df, df$inc_R==0 & df$inc_D==0)
Inc_Dem <- filter(df, df$inc_D==1)
OpenSeat <- filter(df, df$inc_R==0 & df$inc_D==0)
sapply(df, class)
sapply(df, is.factor)
sapply(df, is.numeric)
names(df)
Dem_Inc <- df$inc_D==1
OpenSeat <- df$inc_R==0 & df$inc_D==0``

the histogram snippet you shared relies on my_data$my_variable
after that you wrote a lot of code, none of which even mentions my_data$my_variable.

I think there are some issues with your code. there are a lot of statements that calculate, but then don't store their results in any other variable to be referenceable again later. i.e. the various mean() calculations you perform. They dont seem to go anywhere...

Could you produce a proper reprex ?

my_data$my_variable is just a template given to me. My data would be df, and I believe my variable would be some combination of dem_pct and the two variables I created (inc_D==0 & inc_R==0 and then inc_D==1). Here is the code I have with a lot of the junk taken out.

library(tidyverse)
library(dplyr)
df <- read.csv("C:/Users/brand/downloads/house.csv")
dem_pct <- 100*df$vote_D/(df$vote_D + df$vote_R)
mean(dem_pct)
mean(dem_pct[df$inc_D==1])
mean(dem_pct[df$inc_R==1])
mean(dem_pct[df$inc_D==0 & df$inc_R==0])

I read the instructions on how to produce the reprex and I am honestly super confused. This is my 2nd week using R, so I've had a hard time with this assignment to say the least. Tried to go a different avenues my professor recommended and I still feel lost.

datapasta::df_paste(head(df, 6)[, c('vote_R', 'vote_D')])
#> Error in unclass(x)[...]: subscript out of bounds
data.frame(vote_R = c(20699, 25517, 26638, 32566, 34040, 43577),vote_D = c(27945, 33546, 31953, 34605, 31867, 36785))
#>   vote_R vote_D
#> 1  20699  27945
#> 2  25517  33546
#> 3  26638  31953
#> 4  32566  34605
#> 5  34040  31867
#> 6  43577  36785
hist(df$inc_D==0)
#> Error in df$inc_D: object of type 'closure' is not subsettable
Open_Seat <- df$inc_D==0 & df$inc_R==0
#> Error in df$inc_D: object of type 'closure' is not subsettable
Inc_Dem <- df$inc_D==1
#> Error in df$inc_D: object of type 'closure' is not subsettable
library(tidyverse)
hist(df$inc_D==0, breaks= 20, main= "PLOT TITLE", xlab= "X-AXIS TITLE")
#> Error in df$inc_D: object of type 'closure' is not subsettable
install.packages("reprex")
#> Installing package into 'C:/Users/brand/OneDrive/Documents/R/win-library/3.6'
#> (as 'lib' is unspecified)
#> package 'reprex' successfully unpacked and MD5 sums checked
#> 
#> The downloaded binary packages are in
#>  C:\Users\brand\AppData\Local\Temp\RtmpwZiUcg\downloaded_packages
library(reprex)
reprex::reprex()
#> No input provided and clipboard is not available.
#> Rendering reprex...

Created on 2020-02-11 by the reprex package (v0.3.0)
I was able to get this out of the reprex. I don't think its overly hopeful, but its my shot at it.

I think the most important thing to learn when you are starting with R is how to properly and effectively ask for help, so even if you are confused you should at least try to make a reproducible example. If we had access to sample data on a proper format we could pretty easily give you a working solution for your problem.

It is a big step forward, but you are sharing sample data just for two variables, you have to at least include all the variables used in your code.

Thanks, I'll fix that.

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