Loop with multiple subset of data frame - r

I have a data.frame fish.test0 for which I want to grep specific variables (in varlist) matching the group column to create a sub-data.frame that will undergo a statistical test. The results of the test is saved in tests.res.t. I want to loop the varlist so that I get one results for each input in varlist
Script:
varlist <- c("Abiotrophia","Alphatorquevirus")
for (i in varlist) {
fish.test <- fish.test0[grep("i",fish.test0$group),]
column <- c("ACDC")
tests <- list()
dat_test <- sapply( column, function(colx)
lapply( unique(fish.test$Merge), function(x)
fisher.test( data.frame(
a=c(( fish.test[ which(fish.test$Merge %in% x)[2],"Present"] -
fish.test[ which(fish.test$Merge %in% x)[2], colx] ),fish.test[ which(fish.test$Merge %in% x)[2], colx]
),
b=c(( fish.test[ which(fish.test$Merge %in% x)[1],"NotPresent"] -
fish.test[ which(fish.test$Merge %in% x)[1], colx] ), fish.test[ which(fish.test$Merge %in% x)[1], colx]))) #,alternative = "greater"
) )
rownames(dat_test) <- unique(fish.test$Merge )
colnames(dat_test) <- column
tests.res <- sapply(dat_test[1:dim(dat_test)[1],1], function(x) {
c(x$estimate[1],
x$estimate[2],
ci.lower = x$conf.int[1],
ci.upper = x$conf.int[2],
p.value = x$p.value)
})
tests.res.t <- as.data.frame(t(tests.res))
}
test-data:
fish.test0 <- structure(list(Present = c(4L, 4L, 9L, 9L, 57L, 57L, 146L, 146L,
91L, 91L, 26L, 26L, 6L, 6L, 12L, 12L, 33L, 33L, 10L, 10L, 66L,
66L, 4L, 4L, 4L, 4L, 9L, 9L, 18L, 18L, 19L, 19L, 51L, 51L, 50L,
50L, 12L, 12L, 7L, 7L, 14L, 14L, 27L, 27L, 9L, 9L, 5L, 5L, 6L,
6L, 22L, 22L, 3L, 3L, 14L, 14L, 4L, 4L, 15L, 15L, 6L, 6L, 8L,
8L, 4L, 4L), NotPresent = c(11L, 11L, 44L, 44L, 126L, 126L, 532L,
532L, 382L, 382L, 97L, 97L, 14L, 14L, 43L, 43L, 85L, 85L, 41L,
41L, 336L, 336L, 19L, 19L, 27L, 27L, 67L, 67L, 108L, 108L, 81L,
81L, 240L, 240L, 258L, 258L, 47L, 47L, 31L, 31L, 82L, 82L, 110L,
110L, 63L, 63L, 178L, 178L, 672L, 672L, 451L, 451L, 120L, 120L,
104L, 104L, 47L, 47L, 387L, 387L, 94L, 94L, 300L, 300L, 133L,
133L), group = c("G__Abiotrophia_NotPresent_Anus", "G__Abiotrophia_Present_Anus",
"G__Abiotrophia_NotPresent_Bile duct", "G__Abiotrophia_Present_Bile duct",
"G__Abiotrophia_NotPresent_Bone/Soft tissue", "G__Abiotrophia_Present_Bone/Soft tissue",
"G__Abiotrophia_NotPresent_Breast", "G__Abiotrophia_Present_Breast",
"G__Abiotrophia_NotPresent_Colorectum", "G__Abiotrophia_Present_Colorectum",
"G__Abiotrophia_NotPresent_Esophagus", "G__Abiotrophia_Present_Esophagus",
"G__Abiotrophia_NotPresent_Gallbladder", "G__Abiotrophia_Present_Gallbladder",
"G__Abiotrophia_NotPresent_Head and neck", "G__Abiotrophia_Present_Head and neck",
"G__Abiotrophia_NotPresent_Kidney", "G__Abiotrophia_Present_Kidney",
"G__Abiotrophia_NotPresent_Liver", "G__Abiotrophia_Present_Liver",
"G__Abiotrophia_NotPresent_Lung", "G__Abiotrophia_Present_Lung",
"G__Abiotrophia_NotPresent_Lymphoid tissue", "G__Abiotrophia_Present_Lymphoid tissue",
"G__Abiotrophia_NotPresent_Mesothelium", "G__Abiotrophia_Present_Mesothelium",
"G__Abiotrophia_NotPresent_Nervous system", "G__Abiotrophia_Present_Nervous system",
"G__Abiotrophia_NotPresent_Ovary", "G__Abiotrophia_Present_Ovary",
"G__Abiotrophia_NotPresent_Pancreas", "G__Abiotrophia_Present_Pancreas",
"G__Abiotrophia_NotPresent_Prostate", "G__Abiotrophia_Present_Prostate",
"G__Abiotrophia_NotPresent_Skin", "G__Abiotrophia_Present_Skin",
"G__Abiotrophia_NotPresent_Small intestine", "G__Abiotrophia_Present_Small intestine",
"G__Abiotrophia_NotPresent_Stomach", "G__Abiotrophia_Present_Stomach",
"G__Abiotrophia_NotPresent_Unknown", "G__Abiotrophia_Present_Unknown",
"G__Abiotrophia_NotPresent_Urothelial tract", "G__Abiotrophia_Present_Urothelial tract",
"G__Abiotrophia_NotPresent_Uterus", "G__Abiotrophia_Present_Uterus",
"G__Alphatorquevirus_NotPresent_Bone/Soft tissue", "G__Alphatorquevirus_Present_Bone/Soft tissue",
"G__Alphatorquevirus_NotPresent_Breast", "G__Alphatorquevirus_Present_Breast",
"G__Alphatorquevirus_NotPresent_Colorectum", "G__Alphatorquevirus_Present_Colorectum",
"G__Alphatorquevirus_NotPresent_Esophagus", "G__Alphatorquevirus_Present_Esophagus",
"G__Alphatorquevirus_NotPresent_Kidney", "G__Alphatorquevirus_Present_Kidney",
"G__Alphatorquevirus_NotPresent_Liver", "G__Alphatorquevirus_Present_Liver",
"G__Alphatorquevirus_NotPresent_Lung", "G__Alphatorquevirus_Present_Lung",
"G__Alphatorquevirus_NotPresent_Pancreas", "G__Alphatorquevirus_Present_Pancreas",
"G__Alphatorquevirus_NotPresent_Skin", "G__Alphatorquevirus_Present_Skin",
"G__Alphatorquevirus_NotPresent_Urothelial tract", "G__Alphatorquevirus_Present_Urothelial tract"
), ABCD = c(3L, 2L, 17L, 6L, 34L, 18L, 240L, 53L, 321L, 73L,
87L, 25L, 6L, 3L, 20L, 8L, 15L, 7L, 19L, 4L, 265L, 42L, 6L, 1L,
4L, 2L, 22L, 4L, 70L, 13L, 54L, 12L, 116L, 33L, 58L, 11L, 6L,
2L, 26L, 6L, 42L, 8L, 74L, 18L, 19L, 3L, 52L, 0L, 288L, 5L, 377L,
17L, 110L, 2L, 19L, 3L, 21L, 2L, 298L, 9L, 60L, 6L, 68L, 1L,
89L, 3L), Total = c(15L, 15L, 53L, 53L, 183L, 183L, 678L, 678L,
473L, 473L, 123L, 123L, 20L, 20L, 55L, 55L, 118L, 118L, 51L,
51L, 402L, 402L, 23L, 23L, 31L, 31L, 76L, 76L, 126L, 126L, 100L,
100L, 291L, 291L, 308L, 308L, 59L, 59L, 38L, 38L, 96L, 96L, 137L,
137L, 72L, 72L, 183L, 183L, 678L, 678L, 473L, 473L, 123L, 123L,
118L, 118L, 51L, 51L, 402L, 402L, 100L, 100L, 308L, 308L, 137L,
137L), Merge = c("Abiotrophia_Anus", "Abiotrophia_Anus", "Abiotrophia_Bile duct",
"Abiotrophia_Bile duct", "Abiotrophia_Bone/Soft tissue", "Abiotrophia_Bone/Soft tissue",
"Abiotrophia_Breast", "Abiotrophia_Breast", "Abiotrophia_Colorectum",
"Abiotrophia_Colorectum", "Abiotrophia_Esophagus", "Abiotrophia_Esophagus",
"Abiotrophia_Gallbladder", "Abiotrophia_Gallbladder", "Abiotrophia_Head and neck",
"Abiotrophia_Head and neck", "Abiotrophia_Kidney", "Abiotrophia_Kidney",
"Abiotrophia_Liver", "Abiotrophia_Liver", "Abiotrophia_Lung",
"Abiotrophia_Lung", "Abiotrophia_Lymphoid tissue", "Abiotrophia_Lymphoid tissue",
"Abiotrophia_Mesothelium", "Abiotrophia_Mesothelium", "Abiotrophia_Nervous system",
"Abiotrophia_Nervous system", "Abiotrophia_Ovary", "Abiotrophia_Ovary",
"Abiotrophia_Pancreas", "Abiotrophia_Pancreas", "Abiotrophia_Prostate",
"Abiotrophia_Prostate", "Abiotrophia_Skin", "Abiotrophia_Skin",
"Abiotrophia_Small intestine", "Abiotrophia_Small intestine",
"Abiotrophia_Stomach", "Abiotrophia_Stomach", "Abiotrophia_Unknown",
"Abiotrophia_Unknown", "Abiotrophia_Urothelial tract", "Abiotrophia_Urothelial tract",
"Abiotrophia_Uterus", "Abiotrophia_Uterus", "Alphatorquevirus_Bone/Soft tissue",
"Alphatorquevirus_Bone/Soft tissue", "Alphatorquevirus_Breast",
"Alphatorquevirus_Breast", "Alphatorquevirus_Colorectum", "Alphatorquevirus_Colorectum",
"Alphatorquevirus_Esophagus", "Alphatorquevirus_Esophagus", "Alphatorquevirus_Kidney",
"Alphatorquevirus_Kidney", "Alphatorquevirus_Liver", "Alphatorquevirus_Liver",
"Alphatorquevirus_Lung", "Alphatorquevirus_Lung", "Alphatorquevirus_Pancreas",
"Alphatorquevirus_Pancreas", "Alphatorquevirus_Skin", "Alphatorquevirus_Skin",
"Alphatorquevirus_Urothelial tract", "Alphatorquevirus_Urothelial tract"
)), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 10L, 9L, 12L,
11L, 13L, 14L, 16L, 15L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L,
25L, 26L, 28L, 27L, 29L, 30L, 31L, 32L, 34L, 33L, 35L, 36L, 38L,
37L, 40L, 39L, 42L, 43L, 45L, 44L, 47L, 46L, 1011L, 1012L, 1014L,
1013L, 1015L, 1016L, 1017L, 1018L, 1019L, 1020L, 1022L, 1021L,
1023L, 1024L, 1026L, 1025L, 1027L, 1028L, 1029L, 1030L), class = "data.frame")

This is probably not an answer but it should help to improve you code. If I'm terribly wrong, I'll remove my answer right away. I have loeft out the test business which I don't understand, but your problem seems to be extraction.
The first thing is that you need to remove the quotation marks in your grep command, try:
varlist <- c("Abiotrophia","Alphatorquevirus")
for( i in varlist )
{
# extract rows which contain the variable
fish.test <- fish.test0[ grep( i, fish.test0$group ), ]
print( head( fish.test ) )
}
From what I understand, you need to define column and tests outside your loop. Does that give you more of what you want:
varlist <- c("Abiotrophia","Alphatorquevirus")
column <- "ACDC"
tests <- list()
for( i in 1 : length( varlist ) ) # index can be used later to fill the list
{
# extract rows which contain the variable
fish.test <- fish.test0[ grep( varlist[ i ], fish.test0$group ), ]
# add a column with your name of choice
fish.test <- cbind( fish.test, c( 1: length( fish.test$group ) ) )
colnames( fish.test )[ length( fish.test ) ] <- column
# write each result into your defined list
tests[[ i ]] <- fish.test
}

Related

Problem with Shiny filtered selectinput and ggplot graph

I have a shinyscript prepared where i want to show a graph based on two widgets.
The first widget(selectInput) controls for the area i want to show a diagnostic plot for.
The second widget (checkboxGroupInput) controls for the amount of data i want to show for the area selected with the first widget. So, the options for the checkboxes depend on what is selected with the selectInput.
I solved this with a htmlOutput("") in the UI and a corresponding renderUI in the server.
Everything works fine, but when i proceed to the plotting, something weird happens.
I can use a reactive filter to control for the area as selected with selectInput, but when i extend the filter to also work work with the checkboxGroupInputi get the following error when i run the app:
Warning: Error in : Problem with filter() input ..2.
x Input ..2 must be of size 611 or 1, not size 0.
i Input ..2 is Code == input$code.
202:
This only shows when all the checkboxes are unchecked and no graph is visible. I Can plot the graph that corresponds with the checkboxes, but it only shows 5 barcharts (when for example ten are to be plotted) and the error is given.
Can someone tell me if there is something wrong with m code? And how i can resolve the error and work with these dependand widgets?
Below my code and data
Code
#libraries needed
library(shiny)
library(ggplot2)
library(dplyr)
#data needed
df <- "load in data"
# user interface ----
ui <- fluidPage(
tabsetPanel(
tabPanel("diagnostische tabellen",fluid = TRUE,
titlePanel("PQ analyse"),
sidebarLayout(
sidebarPanel(
helpText("selecteer terrein waar je de PQ-data van wil bekijken"),
#make first dropdownmenu for area
selectInput("terrein",
label = "Kies een terrein",
choices = sort((unique(df$Terrein))),
selected = 1),
htmlOutput("code")
),
mainPanel(plotOutput("map1"))))
)
)
# Server logic ----------------------------
server <- function(input, output){
# ceate a reactive list of PQ-codes based on previous selection for area
output$code <- renderUI({
data_available <- df[df$Terrein == input$terrein, "Code"]
checkboxGroupInput("code",
label = "PQ-code",
choices = sort(unique(data_available)),
selected = unique(data_available))
})
## filter the data for the graph
filtered_data <- reactive({
filter(df, Terrein == input$terrein, Code == input$code)
})
## GGplot graph
output$map1 <- renderPlot({
ggplot(filtered_data(), aes( x = Code, fill = as.character(Jaar))) +
geom_bar(position = position_stack(reverse = TRUE))+
theme(axis.text.x = element_text(angle = 45, size = 15))+
scale_fill_brewer()+
labs(fill='Jaar')+
ggtitle(paste("Aantal herhalingen PQ's op",input$terrein))
})
}
# Run app
shinyApp(ui, server)
df
structure(list(Terrein = structure(c(25L, 25L, 25L, 25L, 1L,
1L, 1L, 1L, 1L, 1L, 29L, 29L, 13L, 13L, 13L, 7L, 7L, 7L, 7L,
7L, 7L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 8L, 8L, 8L, 13L, 8L, 8L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 15L, 15L, 15L, 15L,
16L, 16L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 13L, 13L,
13L, 13L, 24L, 24L, 24L, 24L, 28L, 28L, 28L, 28L, 2L, 2L, 2L,
2L, 2L, 2L, 23L, 23L, 23L, 23L, 23L, 22L, 21L, 21L, 21L, 21L,
21L, 7L, 7L, 7L, 7L, 7L, 7L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
14L, 14L, 14L, 14L, 14L, 14L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
18L, 18L, 18L, 18L, 30L, 30L, 30L, 30L, 20L, 10L, 10L, 10L, 10L,
10L, 13L, 13L, 13L, 6L, 6L, 6L, 6L, 6L, 5L, 5L, 5L, 3L, 3L, 3L,
3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 25L, 1L, 1L, 17L, 17L, 17L, 17L,
13L, 13L, 13L, 13L, 13L, 13L, 23L, 23L, 23L, 23L, 23L, 3L, 3L,
3L, 13L, 3L, 10L, 10L, 25L, 25L, 25L, 25L, 14L, 14L, 14L, 14L,
14L, 14L, 23L, 23L, 23L, 23L, 23L, 15L, 15L, 15L, 15L, 16L, 16L,
16L, 5L, 5L, 5L, 5L, 5L, 12L, 12L, 12L, 12L, 12L, 19L, 15L, 15L,
15L, 15L, 9L, 16L, 16L, 16L, 8L, 19L, 16L, 19L, 8L, 8L, 16L,
16L, 16L, 8L, 8L, 8L, 8L, 8L, 19L, 16L, 19L, 8L, 16L, 16L, 16L,
8L, 16L, 25L, 15L, 15L, 15L, 15L, 15L, 15L, 25L, 21L, 21L, 21L,
7L, 7L, 7L, 12L, 12L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 10L, 10L, 10L, 15L, 15L, 28L, 28L,
28L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 25L, 25L, 25L, 7L, 7L,
7L, 22L, 23L, 23L, 23L, 23L, 23L, 1L, 1L, 1L, 1L, 1L, 23L, 23L,
23L, 23L, 15L, 15L, 15L, 15L, 29L, 29L, 26L, 26L, 26L, 26L, 26L,
26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L,
26L, 7L, 7L, 7L, 5L, 5L, 5L, 5L, 5L, 20L, 12L, 12L, 8L, 20L,
20L, 20L, 20L, 7L, 7L, 7L, 12L, 25L, 25L, 25L, 24L, 24L, 24L,
20L, 20L, 15L, 15L, 15L, 15L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 15L, 15L,
15L, 15L, 14L, 14L, 14L, 14L, 14L, 14L, 12L, 8L, 8L, 8L, 8L,
21L, 21L, 21L, 12L, 10L, 2L, 1L, 1L, 1L, 1L, 1L, 10L, 10L, 15L,
15L, 15L, 15L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L,
26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 6L, 6L, 6L, 6L,
6L, 14L, 14L, 14L, 14L, 23L, 23L, 23L, 23L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 21L, 21L, 21L, 26L, 26L, 26L, 25L, 25L, 23L,
23L, 23L, 23L, 26L, 26L, 26L, 13L, 15L, 15L, 15L, 15L, 10L, 10L,
10L, 10L, 26L, 26L, 26L, 13L, 13L, 13L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 23L, 23L, 23L, 23L, 23L, 1L, 1L, 1L, 1L,
1L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 15L, 15L, 15L, 15L, 23L,
23L, 23L, 23L, 23L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L,
25L, 25L, 21L, 21L, 21L, 12L, 13L, 13L, 13L, 13L, 2L), .Label = c("Arnhemse Heide",
"ASK Doornspijkse Heide", "ASK Oldenbroekse Heide", "Balloërveld",
"Convooi AOCS Nieuw-Milligen", "De Dellen", "de Kom", "De Stompert & Vlasakkers",
"Deelen, VB", "Eder- en Ginkelse Heide", "Ermelosche Heide",
"Havelte", "ISK Harskamp", "Joost Dourleinkazerne", "Kruispeel en Achterbroek",
"Leusderheide", "Luitenant-Generaal Best Kazerne (vml. VB de Peel)",
"Olst-Welsum", "Oude Kamp", "Oude Molen", "Radiostation Noordwijk",
"Rucphense Heide", "Schinveldse Bossen", "Stroese Zand", "Uilenbosch (Waalsdorp)",
"Vliehors", "Vughtse Heide", "Weerter- en Bosoverheide", "Woensdrechtse Heide",
"Zwaluwenberg"), class = "factor"), Code = structure(c(230L,
228L, 228L, 231L, 4L, 5L, 6L, 1L, 2L, 3L, 239L, 240L, 100L, 101L,
102L, 116L, 117L, 118L, 119L, 120L, 121L, 10L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 26L, 27L, 28L, 30L, 29L, 14L, 15L, 16L, 23L, 24L,
25L, 17L, 18L, 19L, 20L, 21L, 22L, 44L, 45L, 46L, 47L, 48L, 49L,
216L, 217L, 218L, 102L, 214L, 215L, 31L, 42L, 35L, 36L, 37L,
38L, 43L, 32L, 33L, 34L, 39L, 40L, 41L, 71L, 71L, 72L, 59L, 60L,
61L, 62L, 57L, 65L, 63L, 64L, 58L, 55L, 56L, 67L, 68L, 68L, 69L,
70L, 70L, 91L, 92L, 78L, 79L, 80L, 73L, 74L, 75L, 76L, 77L, 103L,
100L, 105L, 108L, 102L, 101L, 104L, 109L, 107L, 106L, 94L, 95L,
93L, 96L, 99L, 97L, 98L, 122L, 123L, 124L, 125L, 135L, 136L,
225L, 222L, 219L, 220L, 221L, 223L, 226L, 224L, 227L, 106L, 105L,
107L, 104L, 188L, 189L, 186L, 187L, 236L, 235L, 237L, 238L, 55L,
56L, 57L, 58L, 59L, 60L, 176L, 177L, 178L, 179L, 180L, 175L,
143L, 144L, 145L, 146L, 147L, 116L, 119L, 117L, 118L, 121L, 120L,
163L, 165L, 160L, 161L, 162L, 164L, 166L, 111L, 110L, 112L, 113L,
114L, 115L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 167L, 168L, 169L,
170L, 241L, 242L, 242L, 242L, 160L, 73L, 74L, 77L, 80L, 75L,
103L, 105L, 107L, 50L, 51L, 52L, 53L, 54L, 138L, 139L, 140L,
151L, 152L, 153L, 158L, 159L, 1L, 2L, 3L, 4L, 5L, 6L, 231L, 1L,
2L, 171L, 172L, 173L, 174L, 100L, 102L, 108L, 101L, 109L, 104L,
176L, 177L, 178L, 179L, 180L, 154L, 155L, 156L, 106L, 157L, 79L,
78L, 230L, 229L, 228L, 230L, 115L, 114L, 113L, 112L, 110L, 111L,
176L, 177L, 178L, 179L, 180L, 122L, 123L, 124L, 125L, 137L, 135L,
136L, 141L, 142L, 138L, 139L, 140L, 97L, 95L, 96L, 99L, 98L,
150L, 126L, 127L, 128L, 129L, 190L, 133L, 134L, 132L, 213L, 148L,
131L, 149L, 211L, 212L, 133L, 134L, 132L, 210L, 213L, 210L, 212L,
211L, 148L, 131L, 149L, 210L, 134L, 133L, 132L, 213L, 130L, 231L,
125L, 128L, 129L, 127L, 126L, 124L, 231L, 145L, 144L, 143L, 118L,
120L, 117L, 93L, 94L, 160L, 161L, 166L, 165L, 164L, 163L, 162L,
89L, 88L, 85L, 84L, 90L, 86L, 87L, 79L, 78L, 91L, 123L, 122L,
238L, 237L, 235L, 92L, 80L, 75L, 74L, 76L, 77L, 73L, 232L, 233L,
234L, 119L, 121L, 116L, 175L, 176L, 177L, 179L, 180L, 178L, 2L,
3L, 5L, 4L, 1L, 176L, 178L, 179L, 180L, 126L, 127L, 128L, 129L,
239L, 240L, 191L, 192L, 193L, 194L, 195L, 196L, 197L, 198L, 199L,
200L, 201L, 202L, 203L, 204L, 205L, 206L, 207L, 208L, 209L, 116L,
121L, 119L, 138L, 142L, 141L, 139L, 140L, 161L, 94L, 95L, 183L,
166L, 165L, 160L, 163L, 117L, 120L, 118L, 93L, 233L, 234L, 232L,
189L, 187L, 186L, 162L, 164L, 128L, 126L, 129L, 127L, 74L, 75L,
80L, 76L, 77L, 73L, 79L, 78L, 91L, 92L, 100L, 103L, 108L, 101L,
109L, 106L, 105L, 104L, 123L, 124L, 125L, 122L, 115L, 114L, 113L,
112L, 111L, 110L, 97L, 182L, 184L, 185L, 181L, 145L, 144L, 143L,
96L, 82L, 66L, 2L, 3L, 4L, 5L, 1L, 83L, 81L, 128L, 129L, 126L,
127L, 209L, 206L, 207L, 208L, 191L, 192L, 193L, 194L, 203L, 204L,
205L, 198L, 197L, 196L, 195L, 202L, 201L, 199L, 200L, 52L, 51L,
53L, 50L, 54L, 112L, 115L, 114L, 110L, 180L, 179L, 176L, 178L,
122L, 124L, 126L, 127L, 128L, 129L, 123L, 125L, 145L, 144L, 143L,
192L, 195L, 195L, 233L, 234L, 178L, 176L, 180L, 179L, 191L, 194L,
197L, 103L, 128L, 129L, 126L, 127L, 80L, 76L, 79L, 78L, 193L,
198L, 200L, 101L, 100L, 108L, 81L, 83L, 82L, 73L, 74L, 75L, 77L,
91L, 92L, 176L, 177L, 178L, 180L, 179L, 1L, 2L, 3L, 4L, 5L, 93L,
94L, 95L, 96L, 99L, 98L, 97L, 128L, 129L, 126L, 127L, 176L, 178L,
177L, 179L, 180L, 94L, 97L, 95L, 96L, 105L, 107L, 106L, 109L,
104L, 233L, 234L, 143L, 144L, 145L, 93L, 108L, 101L, 100L, 103L,
58L), .Label = c("AhQ001", "AhQ002", "AhQ003", "AhQ004", "AhQ005",
"AhQ006", "BvB001", "BvB002", "BvB003", "BvB028", "BvB029", "BvB033",
"BvB034", "BvExA1", "BvExA2", "BvExA3", "BvExB1", "BvExB2", "BvExB3",
"BvExC1", "BvExC2", "BvExC3", "BvExD1", "BvExD2", "BvExD3", "BvQ004",
"BvQ005", "BvQ006", "BvQ008", "BvQ009", "BvQ028", "BvQ029", "BvQ030",
"BvQ031", "BvQ056", "BvQ057", "BvQ061", "BvQ062", "BvQ074", "BvQ075",
"BvQ076", "BvQ077", "BvQ078", "BvQ104", "BvQ105", "BvQ120", "BvQ121",
"BvQ182", "BvQ183", "DeQ001", "DeQ002", "DeQ003", "DeQ004", "DeQ005",
"DsQ001", "DsQ002", "DsQ003", "DsQ004", "DsQ005", "DsQ006", "DsQ007",
"DsQ008", "DsQ009", "DsQ010", "DsQ011", "DsQ023", "DsQB01", "DsQB02",
"DsQB03", "DsQB04", "DsQB05", "DsQB06", "EhQ001", "EhQ002", "EhQ003",
"EhQ004", "EhQ005", "EhQ006", "EhQ007", "EhQ008", "EhQJ01", "EhQJ02",
"EhQJ03", "ErQ001", "ErQ002", "ErQ003", "ErQ004", "ErQ005", "ErQ006",
"ErQ007", "GiQ001", "GiQ002", "HaQ001", "HaQ002", "HaQ003", "HaQ004",
"HaQ005", "HaQ006", "HaQ007", "HkQ001", "HkQ002", "HkQ003", "HkQ004",
"HkQ005", "HkQ006", "HkQ007", "HkQ008", "HkQ009", "HkQ010", "JdQ001",
"JdQ002", "JdQ003", "JdQ004", "JdQ005", "JdQ006", "KoQ001", "KoQ002",
"KoQ003", "KoQ004", "KoQ005", "KoQ006", "KrQ001", "KrQ002", "KrQ003",
"KrQ004", "KrQ005", "KrQ006", "KrQ007", "KrQ008", "LhH004", "LhPro1",
"LhPro2", "LhPro4", "LhPRro3", "LhQ001", "LhQ002", "LhX031",
"NmQ001", "NmQ002", "NmQ003", "NmQ004", "NmQ005", "NrQ001", "NrQ002",
"NrQ003", "NrQ004", "NrQ005", "OkPro1", "OkPro2", "OkQ001", "OlQ001",
"OlQ002", "OlQ003", "OlQ004", "OlQ005", "OlQ006", "OlQ007", "OlR001",
"OlR002", "OmQ001", "OmQ002", "OmQ003", "OmQ004", "OmQ005", "OmQ006",
"OmQ007", "OwQ001", "OwQ002", "OwQ003", "OwQ004", "PeH011", "PeH012",
"PeH013", "PeH014", "RhQ001", "SbQ001", "SbQ002", "SbQ003", "SbQ004",
"SbQ005", "StQ001", "StQ002", "StQ003", "StQ004", "StQ005", "SzQ001",
"SzQ002", "SzQ003", "SzQ004", "VdR070", "VhQ001", "VhQ002", "VhQ003",
"VhQ004", "VhQ005", "VhQ006", "VhQ007", "VhQ008", "VhQ009", "VhQ010",
"VhQ011", "VhQ012", "VhQ013", "VhQ014", "VhQ015", "VhQ016", "VhQ017",
"VhQ018", "VhQ019", "VlPro1", "VlPro2", "VlPro3", "VlPro4", "VlQ001",
"VlQ002", "VlQ003", "VlQ004", "VlQ005", "VuQ001", "VuQ002", "VuQ003",
"VuQ004", "VuQ005", "VuQ006", "VuT001", "VuT002", "VuT003", "WaQ001",
"WaQ002", "WaQ003", "WaQ004", "WaQ005", "WaQ006", "WaQ007", "WeQ001",
"WeQ002", "WeQ003", "WeQ004", "WhQ001", "WhQ002", "ZwQ001", "ZwQ002"
), class = "factor")), row.names = c(NA, -611L), class = "data.frame")
As you have multiple Codes for each Terrein, you should use %in%. Also, you need to define each bar count. Try this
# user interface ----
ui <- fluidPage(
tabsetPanel(
tabPanel("diagnostische tabellen",fluid = TRUE,
titlePanel("PQ analyse"),
sidebarLayout(
sidebarPanel(
helpText("selecteer terrein waar je de PQ-data van wil bekijken"),
#make first dropdownmenu for area
selectInput("terrein",
label = "Kies een terrein",
choices = sort((unique(df$Terrein))),
selected = 1),
uiOutput("mycode")
),
mainPanel(plotOutput("map1"))))
)
)
# Server logic ----------------------------
server <- function(input, output){
# ceate a reactive list of PQ-codes based on previous selection for area
output$mycode <- renderUI({
data_available <- df[df$Terrein == input$terrein, "Code"]
checkboxGroupInput("code",
label = "PQ-code",
choices = sort(unique(data_available)),
selected = unique(data_available))
})
## filter the data for the graph
filtered_data <- reactive({
dat <- filter(df, Terrein == input$terrein & Code %in% input$code)
data <- dat %>% group_by(Code) %>%
dplyr::summarise(n=n())
data
})
## GGplot graph
output$map1 <- renderPlot({
ggplot(filtered_data(), aes( x = Code, y=n, fill = Code )) +
geom_bar(position = position_stack(reverse = TRUE), stat = "identity")+
theme(axis.text.x = element_text(angle = 45, size = 15))+
scale_fill_brewer()+
labs(fill=NULL)+
ggtitle(paste("Aantal herhalingen PQ's op",input$terrein))
})
}
# Run app
shinyApp(ui, server)
You will get this output:
Please note that there is no Jaar defined, so you may need to define it.

Replacing missing values with group_by median or mean values [duplicate]

This question already has answers here:
replace NA with groups mean in a non specified number of columns [duplicate]
(2 answers)
Closed 3 years ago.
I have been trying various time series imputation models to impute the missing time series using the imputeTS but the test results are quite poor. Since it looks like seasonal time series data I thought about simply imputing the missing values with the median or average over a number of months.
The data looks like:
> head(data, 10)
v1 v2 v3 v4
1 1908 1 118 10
2 1908 2 138 33
3 1908 3 128 17
4 1908 4 NA 60
5 1908 5 NA 114
6 1908 6 72 124
7 1908 7 NA 44
8 1908 8 70 134
9 1908 9 58 121
10 1908 10 42 117
I calculated the median values:
data %>%
as_tibble() %>%
group_by(v2) %>%
summarise(imp_v3 = median(v3, na.rm = TRUE),
imp_v4 = median(v4, na.rm = TRUE))
Which gives:
# A tibble: 12 x 3
v2 imp_v3 imp_v4
<int> <dbl> <dbl>
1 1 126 23
2 2 132 27.5
3 3 138 33
4 4 22.5 76
5 5 42.5 102.
6 6 72 127
7 7 82 50
8 8 80 51
9 9 57 121
10 10 24 98
11 11 145 71
12 12 130. 31
All I want to do now is impute the missing values for each v1 and v2 with the corresponding medians.
That is all missing values in v3 which has a 1 in v2 will be replaced with 126. All missing values in v4 which has a 5 in column v2 would be replaced with 102..
The numbers in v1 are years and the numbers in v2 are months.
Data:
data <- structure(list(v1 = c(1908L, 1908L, 1908L, 1908L, 1908L, 1908L,
1908L, 1908L, 1908L, 1908L, 1908L, 1908L, 1909L, 1909L, 1909L,
1909L, 1909L, 1909L, 1909L, 1909L, 1909L, 1909L, 1909L, 1909L,
1910L, 1910L, 1910L, 1910L, 1910L, 1910L, 1910L, 1910L, 1910L,
1910L, 1910L, 1910L, 1911L, 1911L, 1911L, 1911L, 1911L, 1911L,
1911L, 1911L, 1911L, 1911L, 1911L, 1911L, 1912L, 1912L, 1912L,
1912L, 1912L, 1912L, 1912L, 1912L, 1912L, 1912L, 1912L, 1912L,
1913L, 1913L, 1913L, 1913L, 1913L, 1913L, 1913L, 1913L, 1913L,
1913L, 1913L, 1913L, 1914L, 1914L, 1914L, 1914L, 1914L, 1914L,
1914L, 1914L, 1914L, 1914L, 1914L, 1914L, 1915L, 1915L, 1915L,
1915L, 1915L, 1915L, 1915L, 1915L, 1915L, 1915L, 1915L, 1915L,
1916L, 1916L, 1916L, 1916L, 1916L, 1916L, 1916L, 1916L, 1916L,
1916L, 1916L, 1916L, 1917L, 1917L, 1917L, 1917L, 1917L, 1917L,
1917L, 1917L, 1917L, 1917L, 1917L, 1917L, 1918L, 1918L, 1918L,
1918L, 1918L, 1918L, 1918L, 1918L, 1918L, 1918L, 1918L, 1918L,
1919L, 1919L, 1919L, 1919L, 1919L, 1919L, 1919L, 1919L, 1919L,
1919L, 1919L, 1919L, 1920L, 1920L, 1920L, 1920L, 1920L, 1920L,
1920L, 1920L, 1920L, 1920L, 1920L, 1920L, 1921L, 1921L, 1921L,
1921L, 1921L, 1921L, 1921L, 1921L, 1921L, 1921L, 1921L, 1921L,
1922L, 1922L, 1922L, 1922L, 1922L, 1922L, 1922L, 1922L, 1922L,
1922L, 1922L, 1922L, 1923L, 1923L, 1923L, 1923L, 1923L, 1923L,
1923L, 1923L, 1923L, 1923L, 1923L, 1923L, 1924L, 1924L, 1924L,
1924L, 1924L, 1924L, 1924L, 1924L, 1924L, 1924L, 1924L, 1924L,
1925L, 1925L, 1925L, 1925L, 1925L, 1925L, 1925L, 1925L, 1925L,
1925L, 1925L, 1925L, 1926L, 1926L, 1926L, 1926L, 1926L, 1926L,
1926L, 1926L, 1926L, 1926L, 1926L, 1926L, 1927L, 1927L, 1927L,
1927L, 1927L, 1927L, 1927L, 1927L, 1927L, 1927L, 1927L, 1927L,
1928L, 1928L, 1928L, 1928L, 1928L, 1928L, 1928L, 1928L, 1928L,
1928L, 1928L, 1928L, 1929L, 1929L, 1929L, 1930L, 1930L, 1930L,
1930L, 1930L, 1930L, 1930L, 1930L, 1930L, 1930L, 1930L, 1930L,
1931L, 1931L, 1931L, 1931L, 1931L, 1931L, 1931L, 1931L, 1931L,
1931L, 1931L, 1931L, 1932L, 1932L, 1932L, 1932L, 1932L, 1932L,
1932L, 1932L, 1932L, 1932L, 1932L, 1932L, 1933L, 1933L, 1933L,
1933L, 1933L, 1933L, 1933L, 1933L, 1933L, 1933L, 1933L, 1933L,
1934L, 1934L, 1934L, 1934L, 1934L, 1934L, 1934L, 1934L, 1934L,
1934L, 1934L, 1934L, 1935L, 1935L, 1935L, 1935L, 1935L, 1935L,
1935L, 1935L, 1935L, 1935L, 1935L, 1935L, 1936L, 1936L, 1936L,
1936L, 1936L, 1936L, 1936L, 1936L, 1936L, 1936L, 1936L, 1936L,
1937L, 1937L, 1937L, 1937L, 1937L, 1937L, 1937L, 1937L, 1937L,
1937L, 1937L, 1937L, 1938L, 1938L, 1938L, 1938L, 1938L, 1938L,
1938L, 1938L, 1938L, 1938L, 1938L, 1938L, 1939L, 1939L, 1939L,
1939L, 1939L, 1939L, 1939L, 1939L, 1939L, 1939L, 1939L, 1939L,
1940L, 1940L, 1940L, 1940L, 1940L, 1940L, 1940L, 1940L, 1940L,
1940L, 1940L, 1940L, 1941L, 1941L, 1941L, 1941L, 1941L, 1941L,
1941L, 1941L, 1941L, 1941L, 1941L, 1941L, 1942L, 1942L, 1942L,
1942L, 1942L, 1942L, 1942L, 1942L, 1942L, 1942L, 1942L, 1942L,
1943L, 1943L, 1943L, 1943L, 1943L, 1943L, 1943L, 1943L, 1943L,
1943L, 1943L, 1943L, 1944L, 1944L, 1944L, 1944L, 1944L, 1944L,
1944L, 1944L, 1944L, 1944L, 1944L, 1944L, 1945L, 1945L, 1945L,
1945L, 1945L, 1945L, 1945L, 1945L), v2 = c(1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 10L, 11L, 12L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L), v3 = c(118L, 138L, 128L, NA, NA, 72L,
NA, 70L, 58L, 42L, 159L, 125L, 118L, 122L, 123L, 22L, 43L, 45L,
68L, 82L, 41L, 29L, 140L, 120L, 119L, NA, 156L, 161L, 39L, 72L,
65L, 75L, 45L, 26L, 122L, 145L, 125L, 135L, 130L, 3L, 58L, 73L,
103L, 101L, 68L, 16L, 144L, 147L, NA, 132L, NA, 24L, NA, NA,
77L, 46L, 34L, 18L, 148L, 154L, 121L, NA, 148L, 9L, 43L, 69L,
70L, 79L, 64L, 27L, 4L, 134L, 126L, 158L, NA, 37L, 34L, 78L,
86L, 91L, 65L, NA, 156L, 130L, 121L, 129L, 144L, 12L, 36L, 80L,
NA, 77L, 63L, NA, 125L, NA, 160L, 121L, 112L, 16L, 44L, 40L,
NA, 96L, NA, NA, 153L, 112L, 105L, 107L, 122L, 148L, 58L, NA,
92L, 76L, 68L, 5L, 159L, 113L, 127L, 145L, 147L, NA, 64L, 61L,
NA, NA, NA, 20L, 144L, 153L, NA, 106L, 121L, 4L, 68L, 72L, 67L,
83L, NA, 14L, 117L, 137L, 139L, 148L, 4L, 160L, 44L, 70L, 58L,
59L, 54L, 27L, 152L, 128L, 154L, 130L, 162L, 17L, 47L, 80L, 104L,
NA, 72L, 57L, 127L, 155L, 116L, 127L, 129L, 151L, 59L, 65L, NA,
56L, 44L, NA, 153L, 140L, 147L, 128L, 155L, 161L, 17L, NA, 95L,
70L, 47L, 18L, 122L, NA, 126L, 119L, 132L, 160L, 35L, 62L, 75L,
62L, NA, 24L, 154L, 155L, 138L, 134L, 138L, 6L, 42L, 83L, 98L,
82L, 39L, 30L, 125L, 114L, 133L, 147L, 150L, 23L, NA, 67L, 97L,
87L, 66L, 8L, 142L, 130L, 133L, 132L, 158L, 5L, 36L, NA, 81L,
74L, 45L, 31L, 145L, NA, NA, 142L, 143L, 14L, 36L, 49L, 82L,
80L, 54L, 24L, 161L, 124L, 21L, NA, 137L, 141L, 109L, NA, 162L,
34L, 81L, 70L, 80L, 52L, 26L, 150L, 126L, 122L, 119L, 130L, NA,
41L, 62L, 75L, NA, 39L, 22L, 158L, 138L, 153L, 125L, 141L, 158L,
28L, 73L, 84L, 90L, 54L, 16L, 147L, 138L, 112L, NA, 4L, 23L,
51L, 89L, 100L, 99L, 83L, 23L, 141L, 111L, 138L, 136L, 138L,
10L, 48L, 77L, 104L, 80L, 79L, 26L, NA, 157L, 131L, 140L, 154L,
10L, 33L, 81L, 99L, NA, 60L, 20L, 152L, 113L, 118L, 110L, 153L,
NA, 41L, 76L, 78L, 88L, 66L, 25L, 144L, 142L, 135L, 134L, 118L,
16L, 50L, 66L, 90L, NA, 59L, 28L, 142L, 113L, 143L, 133L, 25L,
15L, 38L, 69L, 74L, 87L, 61L, 24L, 11L, 127L, 121L, 141L, 144L,
NA, 45L, 76L, 78L, 87L, 62L, 12L, 3L, 119L, 1L, NA, 154L, 7L,
64L, 102L, 78L, 79L, 53L, 19L, 151L, 126L, 2L, 115L, NA, 146L,
23L, 85L, 103L, 74L, 71L, NA, 151L, 140L, 94L, 93L, 136L, 22L,
40L, 79L, 82L, 85L, 56L, 28L, 141L, 152L, NA, 150L, 162L, 35L,
56L, 72L, 92L, 75L, 51L, 31L, 149L, 126L, 147L, NA, 146L, 26L,
40L, 55L, 82L, 88L, 47L, 13L, 149L, 131L, 108L, 159L, 17L, 32L,
49L, 72L, 95L, 83L), v4 = c(10L, 33L, 17L, 60L, 114L, 124L, 44L,
134L, 121L, 117L, 73L, NA, 15L, 3L, 3L, 72L, 86L, 112L, 42L,
41L, 118L, 106L, 31L, 18L, 5L, 29L, NA, 63L, NA, 133L, 135L,
52L, 122L, 110L, 4L, 75L, NA, 27L, 33L, 78L, 116L, 127L, 51L,
NA, 123L, 94L, 71L, 72L, 15L, NA, 74L, 73L, 103L, 136L, 51L,
129L, 114L, 81L, NA, 63L, 25L, 26L, 60L, 77L, 105L, 132L, 38L,
NA, 37L, NA, 89L, 62L, 21L, 74L, 65L, 82L, 96L, 128L, 49L, 45L,
121L, 109L, NA, 31L, 27L, 22L, 29L, 74L, 87L, 119L, 37L, 36L,
126L, 89L, 6L, 26L, 82L, 19L, NA, 70L, 100L, 114L, 45L, 54L,
126L, 98L, 80L, 6L, 5L, 12L, NA, 22L, 103L, 131L, 44L, 53L, 37L,
75L, NA, 1L, 16L, 68L, 59L, 68L, 110L, NA, 39L, 48L, 118L, 98L,
31L, 76L, 15L, 10L, NA, NA, 110L, 131L, NA, 39L, 120L, 70L, 16L,
59L, NA, NA, 70L, 79L, 107L, 127L, 38L, 134L, 122L, 105L, 83L,
31L, 77L, 33L, NA, NA, 96L, 123L, 53L, 49L, 130L, 125L, 23L,
72L, NA, 19L, 27L, 21L, NA, 123L, 133L, 136L, 121L, 87L, 72L,
72L, 69L, 33L, 67L, 71L, 85L, 120L, NA, 41L, NA, 102L, 19L, 19L,
25L, NA, 9L, NA, 107L, 131L, 43L, 34L, 36L, 104L, 83L, 86L, 61L,
28L, 32L, 73L, 109L, 129L, 54L, 51L, 112L, 101L, 2L, 8L, 29L,
74L, 70L, 87L, 84L, 120L, NA, 44L, NA, 80L, 61L, 30L, 33L, 20L,
74L, 77L, 93L, 111L, 50L, 51L, 129L, 102L, 67L, 5L, 23L, 62L,
62L, 72L, 94L, 115L, 47L, 44L, 122L, 98L, 86L, 1L, 90L, 64L,
63L, 27L, 5L, 24L, 81L, 95L, 133L, NA, NA, 136L, 107L, NA, 29L,
14L, 24L, 7L, 77L, 105L, 135L, 52L, 136L, 120L, 84L, 88L, 63L,
70L, 18L, 26L, 63L, NA, 125L, 56L, 56L, 124L, 85L, NA, 75L, 3L,
24L, 60L, 86L, 114L, 136L, 56L, 57L, NA, NA, 71L, 1L, 29L, 29L,
25L, 78L, 102L, 132L, 58L, 41L, 34L, 111L, 76L, 90L, 33L, 66L,
72L, 78L, 86L, 37L, 52L, 48L, 129L, 98L, NA, NA, 19L, 9L, 71L,
60L, 101L, 133L, 50L, NA, 46L, 96L, NA, 59L, 31L, 30L, 6L, 88L,
113L, NA, 56L, 51L, 126L, 107L, 61L, 17L, NA, 62L, 91L, 74L,
100L, 35L, 43L, 52L, 134L, 108L, 97L, 26L, 23L, 63L, 66L, 80L,
NA, 125L, 49L, 56L, 40L, 86L, 88L, 28L, 13L, 11L, 33L, 75L, 109L,
40L, 43L, 39L, 119L, 99L, 80L, 29L, 12L, NA, 23L, 65L, NA, 130L,
57L, NA, 46L, 109L, 69L, 66L, 10L, NA, 20L, 78L, 97L, 126L, 43L,
58L, 120L, 107L, 66L, 76L, 23L, 69L, 64L, 98L, 96L, 135L, 41L,
47L, 127L, 97L, 74L, 26L, 65L, 20L, 27L, 92L, 98L, 124L, 55L,
53L, 121L, 96L, NA, 27L, NA, 73L, NA, 85L, 106L, 135L, 54L, NA
)), class = "data.frame", row.names = c(NA, -443L))
We can use replace to replace the NA elements with the median of the column
library(dplyr)
data %>%
group_by(v2) %>%
mutate_at(vars(v3, v4), list(~ replace(., is.na(.), median(., na.rm = TRUE))))
# A tibble: 443 x 4
# Groups: v2 [12]
# v1 v2 v3 v4
# <int> <int> <dbl> <dbl>
# 1 1908 1 118 10
# 2 1908 2 138 33
# 3 1908 3 128 17
# 4 1908 4 22.5 60
# 5 1908 5 42.5 114
# 6 1908 6 72 124
# 7 1908 7 82 44
# 8 1908 8 70 134
# 9 1908 9 58 121
#10 1908 10 42 117
# … with 433 more rows
Or another option is na.aggregate from zoo
library(zoo)
data %>%
group_by(v2) %>%
mutate_at(vars(v3, v4), na.aggregate)
By default, the FUN is mean. If we want median specify the FUN
data %>%
group_by(v2) %>%
mutate_at(vars(v3, v4), na.aggregate, FUN = median)

Create new column in R yes/no based off another column [duplicate]

This question already has answers here:
Create binary column (0/1) based on condition in another column
(2 answers)
Closed 3 years ago.
I am trying to create a new column in R (yes/no indicator) where if the data in X3 is >= 50 it would = 1(yes) or if <= 49 it would = 0(no).
I have tried various combinations of ifelse statements, I just cannot get it work. I need this step in order to construct my confidence interval.
dput (crime)
structure(list(Y = c(478L, 494L, 643L, 341L, 773L, 603L, 484L,
546L, 424L, 548L, 506L, 819L, 541L, 491L, 514L, 371L, 457L, 437L,
570L, 432L, 619L, 357L, 623L, 547L, 792L, 799L, 439L, 867L, 912L,
462L, 859L, 805L, 652L, 776L, 919L, 732L, 657L, 1419L, 989L,
821L, 1740L, 815L, 760L, 936L, 863L, 783L, 715L, 1504L, 1324L,
940L), X1 = c(184L, 213L, 347L, 565L, 327L, 260L, 325L, 102L,
38L, 226L, 137L, 369L, 109L, 809L, 29L, 245L, 118L, 148L, 387L,
98L, 608L, 218L, 254L, 697L, 827L, 693L, 448L, 942L, 1017L, 216L,
673L, 989L, 630L, 404L, 692L, 1517L, 879L, 631L, 1375L, 1139L,
3545L, 706L, 451L, 433L, 601L, 1024L, 457L, 1441L, 1022L, 1244L
), X2 = c(40L, 32L, 57L, 31L, 67L, 25L, 34L, 33L, 36L, 31L, 35L,
30L, 44L, 32L, 30L, 16L, 29L, 36L, 30L, 23L, 33L, 35L, 38L, 44L,
28L, 35L, 31L, 39L, 27L, 36L, 38L, 46L, 29L, 32L, 39L, 44L, 33L,
43L, 22L, 30L, 86L, 30L, 32L, 43L, 20L, 55L, 44L, 37L, 82L, 66L
), X3 = c(74L, 72L, 70L, 71L, 72L, 68L, 68L, 62L, 69L, 66L, 60L,
81L, 66L, 67L, 65L, 64L, 64L, 62L, 59L, 56L, 46L, 54L, 54L, 45L,
57L, 57L, 61L, 52L, 44L, 43L, 48L, 57L, 47L, 50L, 48L, 49L, 72L,
59L, 49L, 54L, 62L, 47L, 45L, 48L, 69L, 42L, 49L, 57L, 72L, 67L
), X4 = c(11L, 11L, 18L, 11L, 9L, 8L, 12L, 13L, 7L, 9L, 13L,
4L, 9L, 11L, 12L, 10L, 12L, 7L, 15L, 15L, 22L, 14L, 20L, 26L,
12L, 9L, 19L, 17L, 21L, 18L, 19L, 14L, 19L, 19L, 16L, 13L, 13L,
14L, 9L, 13L, 22L, 17L, 34L, 26L, 23L, 23L, 18L, 15L, 22L, 26L
), X5 = c(31L, 43L, 16L, 25L, 29L, 32L, 24L, 28L, 25L, 58L, 21L,
77L, 37L, 37L, 35L, 42L, 21L, 81L, 31L, 50L, 24L, 27L, 22L, 18L,
23L, 60L, 14L, 31L, 24L, 23L, 22L, 25L, 25L, 21L, 32L, 31L, 13L,
21L, 46L, 27L, 18L, 39L, 15L, 23L, 7L, 23L, 30L, 35L, 15L, 18L
), X6 = c(20L, 18L, 16L, 19L, 24L, 15L, 14L, 11L, 12L, 15L, 9L,
36L, 12L, 16L, 11L, 14L, 10L, 27L, 16L, 15L, 8L, 13L, 11L, 8L,
11L, 18L, 12L, 10L, 9L, 8L, 10L, 12L, 9L, 9L, 11L, 14L, 22L,
13L, 13L, 12L, 15L, 11L, 10L, 12L, 12L, 11L, 12L, 13L, 16L, 16L
), X7 = structure(list(Y = c(478L, 494L, 643L, 341L, 773L, 603L,
484L, 546L, 424L, 548L, 506L, 819L, 541L, 491L, 514L, 371L, 457L,
437L, 570L, 432L, 619L, 357L, 623L, 547L, 792L, 799L, 439L, 867L,
912L, 462L, 859L, 805L, 652L, 776L, 919L, 732L, 657L, 1419L,
989L, 821L, 1740L, 815L, 760L, 936L, 863L, 783L, 715L, 1504L,
1324L, 940L), X1 = c(184L, 213L, 347L, 565L, 327L, 260L, 325L,
102L, 38L, 226L, 137L, 369L, 109L, 809L, 29L, 245L, 118L, 148L,
387L, 98L, 608L, 218L, 254L, 697L, 827L, 693L, 448L, 942L, 1017L,
216L, 673L, 989L, 630L, 404L, 692L, 1517L, 879L, 631L, 1375L,
1139L, 3545L, 706L, 451L, 433L, 601L, 1024L, 457L, 1441L, 1022L,
1244L), X2 = c(40L, 32L, 57L, 31L, 67L, 25L, 34L, 33L, 36L, 31L,
35L, 30L, 44L, 32L, 30L, 16L, 29L, 36L, 30L, 23L, 33L, 35L, 38L,
44L, 28L, 35L, 31L, 39L, 27L, 36L, 38L, 46L, 29L, 32L, 39L, 44L,
33L, 43L, 22L, 30L, 86L, 30L, 32L, 43L, 20L, 55L, 44L, 37L, 82L,
66L), X3 = c(74L, 72L, 70L, 71L, 72L, 68L, 68L, 62L, 69L, 66L,
60L, 81L, 66L, 67L, 65L, 64L, 64L, 62L, 59L, 56L, 46L, 54L, 54L,
45L, 57L, 57L, 61L, 52L, 44L, 43L, 48L, 57L, 47L, 50L, 48L, 49L,
72L, 59L, 49L, 54L, 62L, 47L, 45L, 48L, 69L, 42L, 49L, 57L, 72L,
67L), X4 = c(11L, 11L, 18L, 11L, 9L, 8L, 12L, 13L, 7L, 9L, 13L,
4L, 9L, 11L, 12L, 10L, 12L, 7L, 15L, 15L, 22L, 14L, 20L, 26L,
12L, 9L, 19L, 17L, 21L, 18L, 19L, 14L, 19L, 19L, 16L, 13L, 13L,
14L, 9L, 13L, 22L, 17L, 34L, 26L, 23L, 23L, 18L, 15L, 22L, 26L
), X5 = c(31L, 43L, 16L, 25L, 29L, 32L, 24L, 28L, 25L, 58L, 21L,
77L, 37L, 37L, 35L, 42L, 21L, 81L, 31L, 50L, 24L, 27L, 22L, 18L,
23L, 60L, 14L, 31L, 24L, 23L, 22L, 25L, 25L, 21L, 32L, 31L, 13L,
21L, 46L, 27L, 18L, 39L, 15L, 23L, 7L, 23L, 30L, 35L, 15L, 18L
), X6 = c(20L, 18L, 16L, 19L, 24L, 15L, 14L, 11L, 12L, 15L, 9L,
36L, 12L, 16L, 11L, 14L, 10L, 27L, 16L, 15L, 8L, 13L, 11L, 8L,
11L, 18L, 12L, 10L, 9L, 8L, 10L, 12L, 9L, 9L, 11L, 14L, 22L,
13L, 13L, 12L, 15L, 11L, 10L, 12L, 12L, 11L, 12L, 13L, 16L, 16L
), X7 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0,
1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0)), row.names = c(NA, -50L), .Names = c("Y",
"X1", "X2", "X3", "X4", "X5", "X6", "X7"), class = "data.frame")), .Names = c("Y",
"X1", "X2", "X3", "X4", "X5", "X6", "X7"), row.names = c(NA,
-50L), class = "data.frame")
The dput didn't work. But I'd managed to dump the data from column Y and X3 in a dataset (called data) and use dplyr::mutate to do the transformation with a straightforward ifelse condition.
library(dplyr)
data <- data %>% mutate(X3_cat = ifelse(X3 >= 50, 1, 0))

polynomial fitting and plotting regression line in R

I am trying to find orthogonal polynomials of degree 3 for my data. The purpose of this is that i would like to visualise different polynomials fittings on my data: degree 3 and degree 7. I am using the same code as our profesor in class, however I cannot obtain nice results.
orthpoly <- poly(Air_reduced$Temp, order=3)
Air_reduced$xo1 <- orthpoly[,1]
Air_reduced$xo2 <- orthpoly[,2]
Air_reduced$xo3 <- orthpoly[,3]
polymodel1 <- lm(Ozone ~ xo1 + xo2 + xo3, data=Air_reduced)
Air_reduced$fitted1 <- fitted(polymodel1)
?plot
plot(Air_reduced$Temp,Air_reduced$Ozone,xlab="x",ylab="f(x)",
cex.lab=1.5,cex.axis=1.3,col="red",cex=1.3,
main="Polynomial of degree 3", xlim = c(50,97), ylim = c(0,100))
lines(Air_reduced$Temp, Air_reduced$fitted1,col="blue",lwd=3)
however this produces an ugly graph. There seem to be numerous regression lines.
What am i doing wrong?
Data:
structure(list(Ozone = c(41L, 36L, 12L, 18L, 23L, 19L, 8L, 16L,
11L, 14L, 18L, 14L, 34L, 6L, 30L, 11L, 1L, 11L, 4L, 32L, 23L,
45L, 37L, 29L, 71L, 39L, 23L, 21L, 37L, 20L, 12L, 13L, 49L, 32L,
64L, 40L, 77L, 97L, 97L, 85L, 10L, 27L, 7L, 48L, 35L, 61L, 79L,
63L, 16L, 80L, 108L, 20L, 52L, 82L, 50L, 64L, 59L, 39L, 9L, 16L,
122L, 89L, 110L, 44L, 28L, 65L, 22L, 59L, 23L, 31L, 44L, 21L,
9L, 45L, 73L, 76L, 118L, 84L, 85L, 96L, 78L, 73L, 91L, 47L, 32L,
20L, 23L, 21L, 24L, 44L, 21L, 28L, 9L, 13L, 46L, 18L, 13L, 24L,
16L, 13L, 23L, 36L, 7L, 14L, 30L, 14L, 18L, 20L), Solar.R = c(190L,
118L, 149L, 313L, 299L, 99L, 19L, 256L, 290L, 274L, 65L, 334L,
307L, 78L, 322L, 44L, 8L, 320L, 25L, 92L, 13L, 252L, 279L, 127L,
291L, 323L, 148L, 191L, 284L, 37L, 120L, 137L, 248L, 236L, 175L,
314L, 276L, 267L, 272L, 175L, 264L, 175L, 48L, 260L, 274L, 285L,
187L, 220L, 7L, 294L, 223L, 81L, 82L, 213L, 275L, 253L, 254L,
83L, 24L, 77L, 255L, 229L, 207L, 192L, 273L, 157L, 71L, 51L,
115L, 244L, 190L, 259L, 36L, 212L, 215L, 203L, 225L, 237L, 188L,
167L, 197L, 183L, 189L, 95L, 92L, 252L, 220L, 230L, 259L, 236L,
259L, 238L, 24L, 112L, 237L, 224L, 27L, 238L, 201L, 238L, 14L,
139L, 49L, 20L, 193L, 191L, 131L, 223L), Wind = c(7.4, 8, 12.6,
11.5, 8.6, 13.8, 20.1, 9.7, 9.2, 10.9, 13.2, 11.5, 12, 18.4,
11.5, 9.7, 9.7, 16.6, 9.7, 12, 12, 14.9, 7.4, 9.7, 13.8, 11.5,
8, 14.9, 20.7, 9.2, 11.5, 10.3, 9.2, 9.2, 4.6, 10.9, 5.1, 6.3,
5.7, 7.4, 14.3, 14.9, 14.3, 6.9, 10.3, 6.3, 5.1, 11.5, 6.9, 8.6,
8, 8.6, 12, 7.4, 7.4, 7.4, 9.2, 6.9, 13.8, 7.4, 4, 10.3, 8, 11.5,
11.5, 9.7, 10.3, 6.3, 7.4, 10.9, 10.3, 15.5, 14.3, 9.7, 8, 9.7,
2.3, 6.3, 6.3, 6.9, 5.1, 2.8, 4.6, 7.4, 15.5, 10.9, 10.3, 10.9,
9.7, 14.9, 15.5, 6.3, 10.9, 11.5, 6.9, 13.8, 10.3, 10.3, 8, 12.6,
9.2, 10.3, 10.3, 16.6, 6.9, 14.3, 8, 11.5), Temp = c(67L, 72L,
74L, 62L, 65L, 59L, 61L, 69L, 66L, 68L, 58L, 64L, 66L, 57L, 68L,
62L, 59L, 73L, 61L, 61L, 67L, 81L, 76L, 82L, 90L, 87L, 82L, 77L,
72L, 65L, 73L, 76L, 85L, 81L, 83L, 83L, 88L, 92L, 92L, 89L, 73L,
81L, 80L, 81L, 82L, 84L, 87L, 85L, 74L, 86L, 85L, 82L, 86L, 88L,
86L, 83L, 81L, 81L, 81L, 82L, 89L, 90L, 90L, 86L, 82L, 80L, 77L,
79L, 76L, 78L, 78L, 77L, 72L, 79L, 86L, 97L, 94L, 96L, 94L, 91L,
92L, 93L, 93L, 87L, 84L, 80L, 78L, 75L, 73L, 81L, 76L, 77L, 71L,
71L, 78L, 67L, 76L, 68L, 82L, 64L, 71L, 81L, 69L, 63L, 70L, 75L,
76L, 68L), Month = c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L), Day = c(1L, 2L, 3L, 4L, 7L, 8L, 9L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 28L, 29L, 31L, 7L,
9L, 10L, 13L, 16L, 17L, 18L, 19L, 20L, 2L, 3L, 5L, 6L, 7L, 8L,
9L, 10L, 12L, 13L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 24L, 25L,
26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 7L, 8L, 9L, 12L, 13L,
14L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 24L, 26L, 28L, 29L, 30L,
31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L,
28L, 29L, 30L), ID = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 24L,
25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 35L, 36L, 37L, 38L,
39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L,
52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L,
65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 78L,
79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L,
92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L,
104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L)), .Names = c("Ozone",
"Solar.R", "Wind", "Temp", "Month", "Day", "ID"), row.names = c(NA,
-108L), class = c("tbl_df", "tbl", "data.frame"))
Order your data by your x-axis before plotting, and your plot will be pretty:
Air_reduced = Air_reduced[order(Air_reduced$Temp), ]
As a side note, I'd encourage you to try out ggplot2 for plotting. It can fit simple models and plot all at once, and it's smart about defaults (default labels, default ordering the points when plotting a line...). In this case, if you just want a plot with both polynomials, it takes just a few lines of code:
library(ggplot2)
ggplot(Air_reduced, aes(x = Temp, y = Ozone)) +
geom_point(color = "red") +
stat_smooth(method = "lm",
formula = y ~ poly(x, 3),
aes(color = "3rd")) +
stat_smooth(method = "lm",
formula = y ~ poly(x, 7),
aes(color = "7th")) +
scale_color_manual(
name = "Polynomial Degree",
breaks = c("3rd", "7th"),
values = c("blue", "green4")
)

Why Does this ggplot with POSIXct dates give an Error in cut.default, cannot allocate vector

I'm running a report, and have pulled out some data around something happening over time. I have then summarised that data into a dataframe of date/times and counts. When I try to plot it via ggplot2 I get an error
> ggplot(foo, aes(x=Date, y=Count))+
+ geom_line()
Error in cut.default(unclass(x), unclass(breaks), labels = labels, right = right, :
cannot allocate vector of length 1317423601
>
> ggplot(foo[2:349,], aes(x=Date, y=Count))+
+ geom_line()
Plot produced...
My data is using POSIXct dates, which do seem to cover the end of British Summer Time. I also note that if I excldue the first row of data it works !. Any ideas ?
Here's my data
> dput(foo)
structure(list(Date = structure(c(1317423600, 1317445200, 1317466800,
1317488400, 1317510000, 1317553200, 1317574800, 1317596400, 1317618000,
1317639600, 1317661200, 1317682800, 1317704400, 1317726000, 1317747600,
1317769200, 1317790800, 1317812400, 1317834000, 1317855600, 1317877200,
1317898800, 1317920400, 1317942000, 1317963600, 1317985200, 1318006800,
1318028400, 1318050000, 1318071600, 1318093200, 1318114800, 1318136400,
1318158000, 1318179600, 1318201200, 1318222800, 1318244400, 1318266000,
1318287600, 1318309200, 1318330800, 1318352400, 1318374000, 1318395600,
1318417200, 1318438800, 1318460400, 1318503600, 1318525200, 1318546800,
1318568400, 1318590000, 1318611600, 1318633200, 1318654800, 1318676400,
1318698000, 1318719600, 1318762800, 1318784400, 1318806000, 1318827600,
1318849200, 1318870800, 1318892400, 1318914000, 1318935600, 1318957200,
1318978800, 1319000400, 1319022000, 1319043600, 1319065200, 1319086800,
1319108400, 1319130000, 1319151600, 1319173200, 1319194800, 1319216400,
1319238000, 1319259600, 1319281200, 1319302800, 1319324400, 1319346000,
1319367600, 1319410800, 1319432400, 1319454000, 1319475600, 1319497200,
1319518800, 1319540400, 1319562000, 1319583600, 1319605200, 1319626800,
1319648400, 1319670000, 1319691600, 1319713200, 1319734800, 1319756400,
1319778000, 1319799600, 1319821200, 1319842800, 1319864400, 1319886000,
1319907600, 1319929200, 1319994000, 1320015600, 1320037200, 1320058800,
1320080400, 1320102000, 1320123600, 1320145200, 1320166800, 1320188400,
1320210000, 1320231600, 1320253200, 1320274800, 1320296400, 1320318000,
1320339600, 1320361200, 1320382800, 1320404400, 1320426000, 1320447600,
1320469200, 1320490800, 1320512400, 1320534000, 1320577200, 1320598800,
1320620400, 1320642000, 1320663600, 1320685200, 1320706800, 1320750000,
1320771600, 1320793200, 1320814800, 1320836400, 1320858000, 1320879600,
1320901200, 1320922800, 1320944400, 1320966000, 1320987600, 1321009200,
1321030800, 1321052400, 1321074000, 1321095600, 1321117200, 1321138800,
1321182000, 1321203600, 1321225200, 1321246800, 1321268400, 1321290000,
1321311600, 1321333200, 1321354800, 1321376400, 1321398000, 1321419600,
1321441200, 1321462800, 1321484400, 1321506000, 1321527600, 1321549200,
1321570800, 1321592400, 1321614000, 1321635600, 1321657200, 1321678800,
1321700400, 1321722000, 1321743600, 1321765200, 1321786800, 1321808400,
1321830000, 1321851600, 1321873200, 1321894800, 1321916400, 1321938000,
1321959600, 1321981200, 1322002800, 1322024400, 1322046000, 1322067600,
1322089200, 1322110800, 1322132400, 1322154000, 1322175600, 1322197200,
1322218800, 1322240400, 1322262000, 1322305200, 1322326800, 1322370000,
1322391600, 1322413200, 1322434800, 1322456400, 1322478000, 1322499600,
1322521200, 1322542800, 1322564400, 1322586000, 1322607600, 1322629200,
1322650800, 1322672400, 1322694000, 1322715600, 1322737200, 1322758800,
1322780400, 1322802000, 1322823600, 1322845200, 1322866800, 1322888400,
1322910000, 1322931600, 1322953200, 1322974800, 1322996400, 1323018000,
1323039600, 1323061200, 1323082800, 1323104400, 1323126000, 1323147600,
1323169200, 1323190800, 1323212400, 1323234000, 1323255600, 1323277200,
1323298800, 1323320400, 1323342000, 1323363600, 1323385200, 1323406800,
1323428400, 1323450000, 1323471600, 1323493200, 1323514800, 1323558000,
1323579600, 1323601200, 1323622800, 1323644400, 1323666000, 1323687600,
1323709200, 1323730800, 1323752400, 1323774000, 1323795600, 1323817200,
1323838800, 1323860400, 1323882000, 1323903600, 1323925200, 1323946800,
1323968400, 1323990000, 1324011600, 1324033200, 1324054800, 1324076400,
1324098000, 1324119600, 1324141200, 1324162800, 1324206000, 1324227600,
1324249200, 1324270800, 1324292400, 1324314000, 1324335600, 1324357200,
1324378800, 1324400400, 1324422000, 1324443600, 1324465200, 1324486800,
1324508400, 1324530000, 1324551600, 1324573200, 1324594800, 1324616400,
1324638000, 1324659600, 1324681200, 1324702800, 1324724400, 1324767600,
1324832400, 1324854000, 1324875600, 1324897200, 1324918800, 1324940400,
1324962000, 1324983600, 1325005200, 1325026800, 1325048400, 1325070000,
1325091600, 1325113200, 1325134800, 1325156400, 1325178000, 1325199600,
1325221200, 1325242800, 1325264400, 1325286000), class = c("POSIXct",
"POSIXt"), tzone = ""), Count = c(3L, 0L, 9L, 1L, 0L, 1L, 6L,
4L, 4L, 52L, 19L, 7L, 5L, 59L, 30L, 3L, 2L, 50L, 25L, 8L, 4L,
41L, 22L, 4L, 8L, 57L, 12L, 14L, 3L, 10L, 2L, 6L, 0L, 1L, 7L,
10L, 12L, 44L, 19L, 11L, 3L, 47L, 31L, 7L, 9L, 56L, 21L, 11L,
54L, 20L, 10L, 6L, 54L, 17L, 0L, 1L, 11L, 2L, 0L, 2L, 4L, 14L,
9L, 52L, 19L, 11L, 10L, 56L, 33L, 12L, 9L, 57L, 20L, 12L, 5L,
51L, 23L, 14L, 5L, 50L, 9L, 1L, 2L, 5L, 7L, 1L, 0L, 3L, 9L, 13L,
57L, 27L, 10L, 7L, 62L, 29L, 5L, 6L, 53L, 22L, 8L, 10L, 53L,
12L, 7L, 9L, 38L, 8L, 1L, 0L, 9L, 3L, 0L, 6L, 9L, 3L, 81L, 27L,
6L, 5L, 67L, 45L, 6L, 3L, 63L, 43L, 10L, 2L, 57L, 38L, 19L, 12L,
54L, 22L, 1L, 0L, 5L, 4L, 0L, 4L, 2L, 10L, 4L, 53L, 44L, 7L,
65L, 41L, 11L, 7L, 61L, 46L, 7L, 4L, 64L, 48L, 10L, 11L, 56L,
39L, 6L, 1L, 4L, 3L, 0L, 7L, 3L, 10L, 4L, 46L, 45L, 16L, 6L,
69L, 46L, 17L, 1L, 67L, 43L, 15L, 5L, 57L, 40L, 14L, 4L, 56L,
36L, 3L, 0L, 11L, 3L, 0L, 1L, 5L, 2L, 5L, 9L, 59L, 45L, 9L, 7L,
71L, 35L, 19L, 10L, 65L, 23L, 5L, 7L, 10L, 2L, 5L, 6L, 5L, 1L,
0L, 3L, 2L, 0L, 5L, 7L, 10L, 8L, 58L, 46L, 16L, 6L, 70L, 52L,
14L, 8L, 84L, 42L, 10L, 6L, 62L, 44L, 11L, 3L, 58L, 28L, 3L,
0L, 9L, 8L, 1L, 0L, 4L, 2L, 10L, 11L, 65L, 53L, 14L, 11L, 73L,
42L, 14L, 8L, 74L, 33L, 15L, 13L, 75L, 53L, 11L, 10L, 61L, 31L,
1L, 2L, 9L, 2L, 0L, 3L, 1L, 15L, 16L, 114L, 52L, 10L, 14L, 75L,
50L, 14L, 9L, 69L, 52L, 12L, 15L, 77L, 35L, 10L, 5L, 69L, 37L,
5L, 1L, 12L, 10L, 0L, 9L, 4L, 14L, 18L, 90L, 35L, 12L, 9L, 87L,
49L, 10L, 22L, 83L, 41L, 15L, 14L, 79L, 48L, 17L, 6L, 40L, 8L,
2L, 0L, 2L, 0L, 1L, 7L, 2L, 1L, 2L, 6L, 2L, 54L, 31L, 11L, 6L,
54L, 32L, 7L, 9L, 63L, 23L, 16L, 6L, 43L, 17L, 3L)), .Names = c("Date",
"Count"), row.names = c(1L, 2L, 3L, 4L, 5L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L,
24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L,
37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L,
51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 63L, 64L,
65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L,
78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L,
91L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L,
104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L,
115L, 116L, 117L, 120L, 121L, 122L, 123L, 124L, 125L, 126L, 127L,
128L, 129L, 130L, 131L, 132L, 133L, 134L, 135L, 136L, 137L, 138L,
139L, 140L, 141L, 142L, 143L, 144L, 145L, 147L, 148L, 149L, 150L,
151L, 152L, 153L, 155L, 156L, 157L, 158L, 159L, 160L, 161L, 162L,
163L, 164L, 165L, 166L, 167L, 168L, 169L, 170L, 171L, 172L, 173L,
175L, 176L, 177L, 178L, 179L, 180L, 181L, 182L, 183L, 184L, 185L,
186L, 187L, 188L, 189L, 190L, 191L, 192L, 193L, 194L, 195L, 196L,
197L, 198L, 199L, 200L, 201L, 202L, 203L, 204L, 205L, 206L, 207L,
208L, 209L, 210L, 211L, 212L, 213L, 214L, 215L, 216L, 217L, 218L,
219L, 220L, 221L, 222L, 223L, 224L, 225L, 227L, 228L, 230L, 231L,
232L, 233L, 234L, 235L, 236L, 237L, 238L, 239L, 240L, 241L, 242L,
243L, 244L, 245L, 246L, 247L, 248L, 249L, 250L, 251L, 252L, 253L,
254L, 255L, 256L, 257L, 258L, 259L, 260L, 261L, 262L, 263L, 264L,
265L, 266L, 267L, 268L, 269L, 270L, 271L, 272L, 273L, 274L, 275L,
276L, 277L, 278L, 279L, 280L, 281L, 282L, 283L, 285L, 286L, 287L,
288L, 289L, 290L, 291L, 292L, 293L, 294L, 295L, 296L, 297L, 298L,
299L, 300L, 301L, 302L, 303L, 304L, 305L, 306L, 307L, 308L, 309L,
310L, 311L, 312L, 313L, 315L, 316L, 317L, 318L, 319L, 320L, 321L,
322L, 323L, 324L, 325L, 326L, 327L, 328L, 329L, 330L, 331L, 332L,
333L, 334L, 335L, 336L, 337L, 338L, 339L, 341L, 344L, 345L, 346L,
347L, 348L, 349L, 350L, 351L, 352L, 353L, 354L, 355L, 356L, 357L,
358L, 359L, 360L, 361L, 362L, 363L, 364L, 365L), class = "data.frame")
and here's my original code
ggplot(foo, aes(x=Date, y=Count))+
geom_line()

Resources