How to sum numerical and non-numerical values (rows) in R?

Viewed 28

I have a sample that consists of 2787 rows and 259 columns.

To put this into context, it is a survey of how people in Spain perceive their current economic situation versus the one that was 6 months ago, carried out by a Spanish research institute, CIS. Thus, I have a column (P3) with 5 answers: "better", "worse", "same", "don't know", and "don't answer". Also, I have another column (P19) with 2 options ("1" for males and "2" for females). What I would like to get to know is how people rate their current versus past economic situation based on their gender (and plot this). In other words, to see how many males fare their situation "better", how many rate it "worse", and so on; and the same, with females. To achieve this, I've been trying to run the following code:


CVSPastIndividualSituationMales<- aggregate(CIS$P3 ~ CIS$P19 == 1, CIS, sum)
CVSPastSpainSituationFemales<- aggregate(CIS$P3 ~ CIS$P19 == 2, CIS, sum)


CurrentVSPastIndividualSituationMales<-ggplot(CIS,mapping=aes(x=CVSPastIndividualSituationMales))+geom_bar(fill="LightGreen")+xlab("Current VS Past Individual Situation for Males")
CurrentVSPastSpainSituationFemales <-ggplot(CIS,mapping=aes(CVSPastSpainSituationFemales))+geom_bar(fill="Green") + xlab("Current VS Past Spain Situation for Females")

ggarrange(CurrentVSPastIndividualSituationMales, CurrentVSPastSpainSituationFemales, ncol = 1, nrow = 1)

However, I get the following error:

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

Does anyone know what I am doing wrong and how I could possible get to the desired outcome?

Many thanks in advance!

P.S. Actually, I've made a mistake. In case of 'P19', it is not so much numerical data, as binary data (which takes the value of 1 in case of males, and the value of 2 in case of females).

Come what may, I'm stuck with this problem, and any help would be much appreciated indeed.

0 Answers
Related