继续收到错误消息:
没有适用于“ summarise_”的适用于“字符”类对象的方法 追溯:
我试图将'count ='更改为:
(计数=计数(forest_cover $ Cover_Type)) count = count(Cover_Type)
library(ggplot2)
# # forest_cover data is supplied to group_by() function to group the data based on Cover_Type.
# # This 7 groups of data formed above is supplied as input to summarize() where we are getting the
# # count of observations stored in the variable count. cover_type_ratio is created to store the
# # ratio of cover type.
coverType_count = forest_cover %>%
group_by(Cover_Type) %>% # data is grouped according to labels (1,2,3,4,5,6,7) of cover type
dplyr::summarize(count = count(forest_cover$Cover_Type)) %>% # count the number of observations in each group
mutate(cover_type_ratio=count/sum(count)) # divide the counts obtained in above step to get the ratio.
# # Display the contents of the variable coverType_count
coverType_count ```
I expect the code to plot a bar graph for the observations of each Cover_Type.
答案 0 :(得分:1)
count
需要一个tibble/data.frame
,并且不在summarise
内使用
library(dplyr)
forest_cover %>%
count(Cover_Type) %>%
dplyr::mutate(cover_type_ratio = n/sum(n))
如果我们要使用summarise
,请在group_by
之后使用n()
forest_cover %>%
group_by(Cover_Type) %>%
dplyr::summarise(Count = n()) %>%
dplyr::mutate(cover_type_ratio = Count/sum(Count))
答案 1 :(得分:0)
Base R解决方案:
# Count by the group:
forest_cover$count <- ave(forest_cover$Cover_Type, forest_cover$Cover_Type, FUN = length)
# Calculate the ratio:
forest_cover$cover_type_ratio <- forest_cover$count/nrow(forest_cover)