CSV file to Histogram in R

28,159

Solution 1

You should really start to read some basic R manual... CRAN offers a lot of them (look into the Manuals and Contributed sections)

In any case:

setwd("path/to/csv/file")
myvalues <- read.csv("filename.csv")
hist(myvalues, 100) # Example: 100 breaks, but you can specify them at will

See the manual pages for those functions for more help (accessible through ?read.table, ?read.csv and ?hist).

Solution 2

I'm also an R newbie, and I ran into the same thing. I made two separate mistakes, actually, so I'll describe them both here.

Mistake 1: Passing a frequency table to hist(). Originally I was trying to pass a frequency table to hist() instead of passing in the raw data. One way to fix this is to use the rep() ("replicate") function to explode your frequency table back into a raw dataset, as described here:

Instead of that, though, I just decided to read in my original dataset instead of the frequency table.

Mistake 2: Wrong data type. My raw data CSV file contains two columns: hostname and bookings (idea is to count the number of bookings each host generated during some given time period). I read it into a table.

> tbl <- read.csv('bookingsdata.csv')

Then when I tried to generate a histogram off the second column, I did this:

> hist(tbl[2])

This gave me the "'x' must be numeric" error you mention in a comment. (It was trying to read the "bookings" column header in as a data value.)

This fixed it:

> hist(tbl$bookings)

Solution 3

To plot the histogram, the values must be of numeric class i.e the data must be of numeric value. Here the value of x seems to be of some other class.

Run the following command and see:

sapply(myvalues[1,],class)
Share:
28,159
zaloo
Author by

zaloo

Updated on January 31, 2020

Comments

  • zaloo
    zaloo over 4 years

    I'm a total newbie with R, and I'm trying to create a histogram (with value and frequency as the axises) from a csv file (just one row of values). Any idea how I can do this?

  • zaloo
    zaloo over 10 years
    Thanks! I'm getting an error "Error in hist.default(myvalues, 100) : 'x' must be numeric". I tried converting myvalues to asnumeric but it doesn't seem to be working (gives error "Error in hist.default(myvalues) : invalid number of 'breaks'"). Any idea what's up?
  • Paul Hiemstra
    Paul Hiemstra over 10 years
    Without a reproducible example (code + example data) it is hard to say what is going wrong. Please paste the output of dput(myvalues) into your question above.
  • nico
    nico over 10 years
    Agree with @Paul, we really need an example of your data to say more.
  • con
    con over 5 years
    you would need something like hist(myvalues[,1])