0
$\begingroup$

Using ggplot2 I am attempting to create a histogram. I have a column that is full on the continents. I need to add all the continents which I attempted to do with the aggregate function.

data <- aggregate(country$continent,country["continent"], sum)
hist(data)

Error in Summary.factor(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, : ‘sum’ not meaningful for factors

$\endgroup$
2
  • $\begingroup$ I do not understand what you want to achieve. You currently group by continent and sum continent names for each country. You cannot do "USA" + "Russia" = "Love" $\endgroup$
    – keiv.fly
    Commented Dec 5, 2018 at 21:04
  • $\begingroup$ Ok I have a data_frame called country which has a column called continent. I wish to groupby and count the continent column. So, for example USA = 5, Russia = 2, etc. After that is done, I am going to make a histogram. $\endgroup$
    – Chris Kehl
    Commented Dec 5, 2018 at 21:20

1 Answer 1

1
$\begingroup$

The following code :

data.frame(table(country$continent))

will create a dataframe where one column is the continent and the other is the number of countries in the continent.

$\endgroup$
15
  • $\begingroup$ working on histogram hist(data$Freq) $\endgroup$
    – Chris Kehl
    Commented Dec 5, 2018 at 23:22
  • $\begingroup$ not looking as expected. $\endgroup$
    – Chris Kehl
    Commented Dec 5, 2018 at 23:24
  • 1
    $\begingroup$ barplot(table(country$continent)) $\endgroup$
    – keiv.fly
    Commented Dec 5, 2018 at 23:29
  • $\begingroup$ Closing in on this problem, thanks, it did look 100% better. $\endgroup$
    – Chris Kehl
    Commented Dec 5, 2018 at 23:40
  • 1
    $\begingroup$ Histogram is not a barplot. data['percen'] creates another column in the same dataframe. Calling hist(data) means you want to create a frequency histogram from the data frame. And data frame is not one column but many. So what you want is probably barplot(data$Freq/sum(data$Freq)) or barplot(data$percen) $\endgroup$
    – keiv.fly
    Commented Dec 6, 2018 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.