I am working with "survey" data frame that contains data for university students. I have split the students into 3 age groups (0-20), (20 - 25), (25 ).
And now I am trying to present graphically the age groups and their smoke habits.
I tried the following script
Age_Group <- cut(survey$Age, breaks=c(0, 20, 25, Inf),
labels=c("0-20", "20-25", "25 "))
survey2 <- survey
survey2 <- cbind(survey2, Age_Group)
barplot(table(survey2$Age, survey2$Smoke, legend.text=TRUE, beside=TRUE))
The error that I get is:
Error in table(survey2$Age_Group, survey2$Smoke, legend.text = TRUE, beside = TRUE):
all arguments must have the same length
CodePudding user response:
The issue is that you have a parenthesis in the wrong place.
barplot(table(survey2$Age, survey2$Smoke, legend.text=TRUE, beside=TRUE))
should be:
barplot(table(survey2$Age, survey2$Smoke), legend.text=TRUE, beside=TRUE)
It is reading table as having four columns, with the last two have only one element. I think you want below. I created a sample data set to test the code with.
survey <- data.frame(Age=(30*rnorm(1000) 45), Smoke=as.numeric(runif(1000) < .2))
Age_Group <- cut(survey$Age, breaks=c(0, 20, 25, Inf), labels=c("0-20", "20-25", "25 "))
survey2 <- survey
survey2 <- cbind(survey2, Age_Group)
barplot(table(survey2$Smoke, survey2$Age_Group), legend.text=TRUE, beside=TRUE)