r语言 - 汇总闪亮选择输入的字符和数字列



下面是数据帧的结构

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数据帧中,我想使用"村庄"列来实现selectInput函数。因此,如果一个人从下拉列表中选择"位置 A"或"位置 B",则基于频率表的上述图将在输出中呈现。如何重构数据帧以适应此情况 使用dplyrdata.table

这很简单,但如果您有任何问题,请发表评论 -

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()
})
}
)

最新更新