angarato angarato - 1 month ago 13
R Question

How to use hist() function with count data as input

What is the criteria for using the hist() function in R?

I have two columns of data, which looks something like this:

1 8764

2 604

3 150

4 50

5 21

6 7

7 2

8 5

10 3

11 2

12 1

14 1

16 1

17 2

18 3

20 1

23 1

24 1

25 1

28 1

29 1

When I put that into a data frame in R, and try to plot that using the hist() function, it gives me an error "x: must be numerical". How do I go about solving this?

I'm trying to get the first column on the x-axis and the second column on the y-axis.

Pardon if the question sounds stupid, its my first time using R.

Answer

you can do like this if you absolutely want to use the hist function:

hist(rep(df[[1]], df[[2]]))

df being your data.frame (well, if I understand as Roland said in the comments that the first column might be your values and the second column your frequency counts)

Edit it appears that your data.frame only has one column. In this case this will work:

hist(rep(seq_along(df[[1]]), df[[1]]))

In case of presence of NA do this before:

df <- na.omit(df)
Comments