Mr. Dulla
Mr. Dulla

Reputation: 29

Count values in a column and place in variable in R

So I am new to R and I've managed to get a box plot going. But what I did was messy and pretty inefficient. To help me learn, I was hoping to learn an easier way to do what I did.

I'll leave my code below.

food_coded <- subset(data, select=c("comfort_food_reasons_coded"))
head(food_coded)
stress <- length(which(food_coded == 1))
boredom <- length(which(food_coded == 2))
depression <- length(which(food_coded == 3))
print(depression)
hunger<- length(which(food_coded == 4))
laziness<- length(which(food_coded == 5))
cold_weather<- length(which(food_coded == 6))
happiness <- length(which(food_coded == 7))
watching_tv<- length(which(food_coded == 8))
none <- length(which(food_coded == 9))

axis <- c(stress, boredom, depression, hunger, laziness, cold_weather, happiness, watching_tv, none) 
#Time to make a bar graph
barplot(axis, depression)

Upvotes: 1

Views: 24

Answers (1)

akrun
akrun

Reputation: 886938

We need to get the frequency using table

barplot(table(food_coded))

Upvotes: 1

Related Questions