下面是数据框的结构
Village <- c("Location A" , "Location B", "Location C", "Location C", "Location A")
Farmers_Name <- c("Mary", "John", "Grace","Steph", "Richard")
Practiced_MinimumTillage <- c(0,1,1,0,1)
Practiced_Intercropping <- c(1,1,1,0,0)
Practiced_CropRotation <- c(1,1,1,1,0)
Practiced_ApplicationOfManure <- c(0,1,0,1,0)
farmers <- data.frame(Farmers_Name,Village,Practiced_MinimumTillage,Practiced_Intercropping,Practiced_CropRotation,Practiced_ApplicationOfManure)
dataframe
农民的产出。
Farmers_Name Village Practiced_MinimumTillage Practiced_Intercropping Practiced_CropRotation Practiced_ApplicationOfManure
1 Mary Location A 0 1 1 0
2 John Location B 1 1 1 1
3 Grace Location C 1 1 1 0
4 Steph Location C 0 0 1 1
5 Richard Location A 1 0 0 0
总结农场实践以了解用法。农民在其农场中使用的做法的频率表。
practices <- select(farmers,Practiced_MinimumTillage,Practiced_Intercropping,Practiced_CropRotation,Practiced_ApplicationOfManure)
practices %>%
summarise_all(sum, na.rm=TRUE) %>%
gather(var,value) %>%
arrange(desc(value)) %>%
ggplot(aes(var, value)) + geom_bar(stat = "Identity") + coord_flip()
在farmers
数据框中,我想将列Village用于selectInput
的功能。由此,如果有人从下拉菜单中选择“位置A”或“位置B”,则基于频率表的上图将呈现在输出中。我如何重组数据框以适合此
使用dplyr
还是data.table
?
答案 0 :(得分:1)
这非常简单,但是如果您有任何疑问,请发表评论-
Village <- c("Location A" , "Location B", "Location C", "Location C", "Location A")
Farmers_Name <- c("Mary", "John", "Grace","Steph", "Richard")
Practiced_MinimumTillage <- c(0,1,1,0,1)
Practiced_Intercropping <- c(1,1,1,0,0)
Practiced_CropRotation <- c(1,1,1,1,0)
Practiced_ApplicationOfManure <- c(0,1,0,1,0)
farmers <- data.frame(Farmers_Name,Village,Practiced_MinimumTillage,Practiced_Intercropping,
Practiced_CropRotation,Practiced_ApplicationOfManure)
shinyApp(
ui = fluidPage(
selectInput("village", "Select Village", choices = unique(farmers$Village)),
plotOutput("some_plot")
),
server = function(input, output, session) {
output$some_plot <- renderPlot({
filter(farmers, Village == input$village) %>%
select(Practiced_MinimumTillage,Practiced_Intercropping,Practiced_CropRotation,
Practiced_ApplicationOfManure) %>%
summarise_all(sum, na.rm=TRUE) %>%
gather(var,value) %>%
arrange(desc(value)) %>%
ggplot(aes(var, value)) + geom_bar(stat = "Identity") + coord_flip()
})
}
)