EDIT: My question was not clear enough. I apologize.
The problem was to define groups and assign values of a column of a dataframe to it. I solved the question myself with a chain of ifelse and the comments here. Thanks for that. I then did it manually for each column seperately.
data %>%
mutate(group = ifelse(richness <= -0.6, "1",
ifelse(richness > -0.6 & richness <= -0.2, "2",
ifelse(richness >-0.2 & richness <= 0.2, "3",
ifelse(richness >0.2 & richness <= 0.6, "4",
ifelse(richness >0.6, "5", NA)))))) %>%
group_by(group) %>%
summarise(percentage=n()*100/"No.of.values")
Using
carb
variable frommtcars
data set as example:If you want to define groups your self you can use the
cut
function: