Fill in matrix values from data frame in a vectorized manner - r

I have a dataframe in with 3 columns, two of which represent the i,j indices in a matrix. For each row of the dataframe, I would like to fill the corresponding i,j value in a matrix to 1.
Sharing the data and matrix below, which I think will make it easier to describe the problem:
data = structure(list(sale_id = c(0L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L,
5L, 5L, 5L, 5L, 5L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L, 11L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 14L, 15L, 16L, 16L,
17L, 17L, 17L, 17L, 17L, 18L, 18L, 19L, 19L, 20L, 20L, 21L, 22L,
22L, 23L, 23L, 23L, 24L, 24L, 25L, 25L, 26L, 26L, 27L, 27L, 28L,
28L, 29L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 31L, 31L, 32L, 32L,
33L, 33L, 33L, 33L, 33L, 33L, 34L, 34L), user_id = c(3219L, 144L,
2884L, 2884L, 2155L, 2155L, 2155L, 2155L, 2817L, 2817L, 2817L,
2817L, 2817L, 2817L, 144L, 144L, 2850L, 2850L, 2850L, 2850L,
2850L, 2850L, 2850L, 2850L, 2850L, 144L, 144L, 144L, 144L, 144L,
144L, 144L, 144L, 2817L, 2817L, 2075L, 2075L, 2546L, 2546L, 2546L,
2687L, 2687L, 2687L, 2687L, 2687L, 2687L, 2687L, 2687L, 170L,
2546L, 1963L, 144L, 144L, 1825L, 1825L, 1825L, 1825L, 1825L,
144L, 144L, 2155L, 2155L, 2546L, 2546L, 144L, 2155L, 2155L, 144L,
144L, 144L, 3182L, 3182L, 3343L, 3343L, 170L, 170L, 2155L, 2155L,
2793L, 2793L, 1564L, 2250L, 2250L, 2250L, 2250L, 2250L, 2250L,
2250L, 3083L, 3083L, 2075L, 2075L, 144L, 144L, 144L, 144L, 144L,
144L, 829L, 829L), item_id = c(174L, 10L, 179L, 162L, 171L, 182L,
179L, 185L, 199L, 179L, 195L, 174L, 162L, 198L, 144L, 69L, 57L,
47L, 83L, 80L, 10L, 117L, 14L, 90L, 88L, 186L, 167L, 192L, 142L,
162L, 173L, 151L, 134L, 191L, 166L, 118L, 128L, 98L, 95L, 119L,
130L, 154L, 155L, 181L, 120L, 118L, 77L, 120L, 101L, 31L, 139L,
10L, 30L, 182L, 179L, 139L, 173L, 171L, 80L, 39L, 26L, 69L, 163L,
151L, 175L, 150L, 148L, 121L, 147L, 88L, 183L, 177L, 132L, 167L,
176L, 172L, 57L, 78L, 98L, 99L, 118L, 102L, 141L, 97L, 99L, 79L,
32L, 17L, 16L, 30L, 66L, 54L, 57L, 91L, 81L, 39L, 92L, 123L,
87L, 62L)), .Names = c("sale_id", "user_id", "item_id"), row.names = c(NA,
100L), class = "data.frame")
M = matrix(0, nrow = max(data$user_id), ncol = max(data$item_id))
head(data, n = 6)
sale_id user_id item_id
1 0 3219 174
2 1 144 10
3 2 2884 179
4 2 2884 162
5 3 2155 171
6 3 2155 182
The i-column is user_id and the j-column is item_id. So for the first row, I would like for M[3219, 174] = 1, then I would like M[144, 10] = 1, etc. I would like to do this without a for-loop, which is too slow given the size of my matrix.
For reference, what I'm currently doing is:
for(i in 1:nrow(data)) {
M[data$user_id[i], data$item_id[i]] = 1
}
However, my problem scales quite large, and this is too slow for my problem. Any help is greatly appreciated! Thanks
EDIT: i tried something along the lines of:
apply(data, 1, FUN = function(x) M[x[2],x[3]] = 1)
but it didn't work as well as i would hope (takes even longer than the for-loop).

Try this:
M[cbind(data$user_id,data$item_id)] <- 1

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.

How do I store the output of a repeat loop in a dataframe

My basic idea is to compute the Means of chunks (column-wise) of a large matrix and store these Means as rows of a data frame. Note, the chunks have different sizes (number of rows) and these are stored in a vector vec1. Below is my code:
df <- setNames(data.frame(matrix(nrow = 4000, ncol = 3)),
c("Age","Weight", "height"))
#
i <- 1
j <- vec1[1] - 1
k <- 0
repeat {
elements <- as.vector(apply(mydata[i : (j + 1), 3:5], 2, mean))
df <- rbind(df, elements)
k <- k + 1
i = i + vec1[k]
j = j + vec1[k + 1]
if (j + 1 >= l){
break
}
}
N.B.: When I perform the computations manually without looping it works. But the result of the loop yields a 4000 * 3 matrix filled with NA apart from the first row.
vec1 is a vector with 4000 entries, and whose first 500 elements - head(vec1, 500) -are below:
c(15L, 45L, 111L, 32L, 25L, 13L, 144L, 31L, 150L, 124L, 22L,
94L, 60L, 156L, 4L, 30L, 12L, 12L, 16L, 23L, 242L, 58L, 65L,
17L, 63L, 193L, 148L, 162L, 79L, 6L, 22L, 30L, 188L, 44L, 7L,
130L, 49L, 10L, 87L, 11L, 6L, 113L, 113L, 100L, 42L, 5L, 64L,
127L, 73L, 36L, 13L, 120L, 44L, 34L, 153L, 10L, 35L, 205L, 31L,
102L, 181L, 26L, 105L, 75L, 42L, 122L, 42L, 221L, 216L, 120L,
50L, 171L, 56L, 1L, 89L, 11L, 103L, 167L, 96L, 31L, 67L, 182L,
114L, 45L, 4L, 118L, 19L, 243L, 241L, 48L, 36L, 64L, 94L, 63L,
16L, 8L, 213L, 26L, 127L, 139L, 71L, 91L, 133L, 23L, 88L, 31L,
28L, 70L, 112L, 6L, 25L, 82L, 17L, 24L, 196L, 39L, 78L, 23L,
73L, 110L, 64L, 87L, 84L, 11L, 101L, 19L, 6L, 25L, 39L, 59L,
68L, 31L, 183L, 52L, 142L, 63L, 41L, 214L, 19L, 120L, 85L, 104L,
3L, 8L, 38L, 11L, 12L, 21L, 12L, 53L, 37L, 85L, 106L, 12L, 31L,
106L, 75L, 10L, 121L, 60L, 137L, 96L, 177L, 102L, 97L, 145L,
52L, 11L, 112L, 73L, 67L, 8L, 235L, 203L, 182L, 168L, 101L, 144L,
238L, 73L, 38L, 85L, 56L, 14L, 162L, 131L, 14L, 154L, 28L, 30L,
75L, 88L, 268L, 169L, 255L, 127L, 111L, 63L, 42L, 156L, 12L,
22L, 71L, 140L, 110L, 33L, 99L, 79L, 47L, 7L, 131L, 69L, 10L,
61L, 2L, 57L, 96L, 111L, 41L, 250L, 77L, 22L, 198L, 187L, 15L,
108L, 130L, 76L, 190L, 249L, 68L, 117L, 79L, 2L, 13L, 108L, 9L,
39L, 42L, 43L, 149L, 62L, 47L, 66L, 85L, 197L, 109L, 21L, 263L,
54L, 13L, 61L, 72L, 73L, 80L, 46L, 7L, 110L, 128L, 236L, 27L,
240L, 61L, 23L, 82L, 157L, 92L, 95L, 6L, 137L, 237L, 2L, 20L,
45L, 48L, 200L, 20L, 127L, 21L, 64L, 49L, 38L, 108L, 11L, 16L,
108L, 18L, 62L, 15L, 61L, 81L, 28L, 20L, 33L, 50L, 222L, 267L,
29L, 3L, 44L, 46L, 3L, 212L, 53L, 67L, 131L, 43L, 3L, 123L, 134L,
106L, 91L, 194L, 2L, 97L, 43L, 39L, 65L, 96L, 233L, 36L, 81L,
6L, 57L, 29L, 10L, 17L, 10L, 92L, 28L, 168L, 78L, 52L, 227L,
86L, 134L, 58L, 65L, 175L, 20L, 113L, 33L, 143L, 11L, 87L, 101L,
19L, 106L, 63L, 68L, 38L, 263L, 140L, 45L, 169L, 268L, 182L,
114L, 88L, 39L, 6L, 53L, 244L, 84L, 99L, 46L, 53L, 1L, 111L,
88L, 115L, 93L, 35L, 124L, 145L, 262L, 47L, 10L, 84L, 20L, 159L,
207L, 102L, 48L, 79L, 28L, 51L, 77L, 3L, 58L, 20L, 81L, 54L,
46L, 29L, 12L, 74L, 28L, 4L, 18L, 18L, 38L, 29L, 157L, 108L,
94L, 56L, 23L, 92L, 60L, 86L, 39L, 59L, 85L, 14L, 53L, 23L, 88L,
130L, 8L, 149L, 65L, 71L, 88L, 31L, 67L, 83L, 106L, 44L, 35L,
23L, 76L, 90L, 271L, 12L, 167L, 30L, 87L, 3L, 7L, 15L, 159L,
199L, 7L, 35L, 193L, 207L, 6L, 98L, 61L, 81L, 95L, 66L, 2L, 65L,
242L, 221L, 51L, 6L, 5L, 265L, 119L, 126L, 7L, 159L, 74L, 63L,
188L, 15L, 42L, 26L, 41L, 116L, 50L, 62L, 121L, 67L, 1L, 10L,
192L, 59L, 42L, 84L, 187L, 26L, 32L, 35L, 60L, 117L, 227L, 20L,
20L, 125L, 191L, 24L, 270L, 13L, 14L, 59L, 214L, 96L, 100L, 15L,
22L, 100L, 49L, 146L, 137L, 257L, 93L, 91L, 23L, 234L, 108L,
52L, 7L, 124L, 48L, 2L, 42L, 82L, 99L, 85L, 11L, 141L, 185L,
30L, 1L, 269L, 83L, 25L, 187L, 122L, 222L, 11L, 201L, 95L, 40L,
146L, 75L, 218L, 3L, 39L, 76L, 205L, 21L, 23L, 36L, 43L, 105L,
89L, 10L, 155L, 32L, 144L, 160L, 181L, 144L, 139L, 5L, 2L, 26L,
48L, 55L, 177L, 178L, 108L, 221L, 149L, 32L, 77L, 29L, 160L,
115L, 23L, 193L, 113L, 1L, 154L, 87L, 239L, 221L, 36L, 100L,
34L, 42L, 77L, 62L, 20L, 73L, 81L, 17L, 21L, 33L, 3L, 33L, 84L,
92L, 31L, 9L, 65L, 187L, 62L, 87L, 48L, 218L, 6L, 41L, 90L, 102L,
67L, 27L, 1L, 270L, 159L, 46L, 31L, 50L, 19L, 2L, 30L, 35L, 211L,
103L, 12L, 99L, 75L, 37L, 99L, 83L, 49L, 38L, 125L, 53L, 29L,
11L, 23L, 50L, 41L, 114L, 72L, 44L, 32L, 105L, 25L, 67L, 203L,
24L, 82L, 167L, 205L, 28L, 89L, 75L, 52L, 36L, 29L, 16L, 137L,
95L, 230L, 43L, 4L, 194L, 12L, 21L, 25L, 6L, 176L, 48L, 6L, 142L,
24L, 15L, 101L, 160L, 43L, 9L, 125L, 122L, 53L, 55L, 226L, 241L,
259L, 150L, 142L, 47L, 89L, 13L, 2L, 173L, 147L, 5L, 15L, 159L,
7L, 27L, 117L, 97L, 38L, 71L, 7L, 35L, 91L, 172L, 149L, 103L,
51L, 117L, 67L, 142L, 63L, 53L, 87L, 105L, 2L, 1L, 17L, 30L,
114L, 55L, 202L, 34L, 70L, 50L, 37L, 167L, 45L, 7L, 102L, 238L,
176L, 27L, 7L, 86L, 43L, 269L, 88L, 1L, 18L, 41L, 14L, 71L, 88L,
144L, 44L, 19L, 189L, 258L, 76L, 13L, 44L, 20L, 152L, 133L, 86L,
32L, 1L, 56L, 140L, 65L, 74L, 131L, 155L, 40L, 40L, 112L, 186L,
178L, 249L, 42L, 184L, 43L, 5L, 13L, 90L, 111L, 173L, 220L, 71L,
223L, 5L, 178L, 42L, 126L, 56L, 6L, 15L, 249L, 254L, 148L, 60L,
133L, 218L, 111L, 29L, 77L, 16L, 71L, 128L, 100L, 4L, 13L, 72L,
21L, 133L, 130L, 51L, 62L, 14L, 189L, 99L, 32L, 211L, 5L, 15L,
35L, 72L, 153L, 59L, 85L, 165L, 18L, 51L, 21L, 123L, 15L, 93L,
53L, 2L, 210L, 126L, 196L, 62L, 156L, 57L, 179L, 79L, 27L, 22L,
52L, 167L, 33L, 150L, 72L, 30L, 3L, 65L, 36L, 89L, 54L, 18L,
55L, 137L, 119L, 258L, 33L, 21L, 32L, 116L, 12L, 176L, 91L, 168L,
74L, 6L, 4L, 138L, 149L, 39L, 47L, 49L, 81L, 35L, 61L, 4L, 58L,
31L, 172L, 30L, 27L, 184L, 41L, 51L, 24L, 115L, 81L, 71L, 61L,
154L, 206L, 182L, 149L, 42L, 49L, 6L, 104L, 2L, 217L, 27L, 148L,
37L, 159L, 182L, 139L, 49L, 30L, 41L, 20L, 2L, 15L, 35L, 157L,
86L, 261L, 161L, 145L, 105L, 87L, 220L, 12L, 99L, 233L, 190L,
59L, 95L, 151L, 38L, 46L, 32L, 56L, 48L, 71L, 22L, 44L, 143L,
34L, 34L, 7L, 20L, 87L, 106L, 114L, 26L, 7L, 110L, 93L, 113L,
83L, 76L, 43L, 22L, 2L, 101L, 22L, 65L, 17L, 112L, 116L, 138L,
122L, 68L, 5L, 247L, 155L, 149L, 4L, 49L, 130L, 46L, 13L, 223L,
74L, 15L, 175L, 24L, 2L, 96L, 114L, 125L, 56L, 27L, 67L, 30L,
206L, 38L, 42L, 9L, 118L, 24L, 11L, 156L, 109L, 154L, 40L, 175L,
107L, 193L, 30L, 75L, 72L, 44L, 232L, 37L, 130L, 47L, 81L, 18L,
120L, 126L, 93L, 51L, 138L, 6L, 47L, 76L, 65L, 91L, 14L, 92L,
45L, 73L, 107L, 42L, 87L, 158L, 124L, 14L, 151L, 11L, 148L, 122L,
36L, 169L, 149L, 41L, 152L, 116L, 122L, 39L, 196L, 124L, 142L,
12L, 21L, 107L, 4L, 236L, 18L, 193L, 225L, 31L, 147L, 151L, 14L,
63L, 12L, 79L, 55L, 198L, 7L, 84L, 101L, 22L, 194L, 150L, 5L,
20L, 153L, 45L, 231L, 33L, 44L, 174L, 171L, 74L, 9L, 114L, 97L,
107L, 7L, 87L, 113L, 49L, 14L, 32L, 1L, 43L, 131L, 43L, 22L,
32L, 36L, 201L, 206L, 18L, 170L, 79L, 55L, 218L, 198L, 10L, 51L,
35L, 144L, 163L, 255L, 23L, 180L, 20L, 40L, 89L, 107L, 82L, 67L,
115L, 255L, 14L, 155L, 9L, 53L, 55L, 16L, 38L, 16L, 26L, 155L,
4L, 154L, 147L, 223L, 57L, 75L, 54L, 50L, 104L, 79L, 145L, 71L,
39L, 110L, 20L, 23L, 10L, 110L, 67L, 171L, 16L, 5L, 28L, 163L,
204L, 250L, 144L, 101L, 18L, 36L, 139L, 10L, 102L, 57L, 125L,
66L, 33L, 20L, 188L, 15L, 41L, 20L, 112L, 109L, 64L, 28L, 10L,
149L, 196L, 108L, 26L, 173L, 1L, 58L, 185L, 35L, 44L, 37L, 106L,
45L, 58L, 162L, 34L, 151L, 122L, 48L, 8L, 9L, 33L, 4L, 21L, 105L,
36L, 32L, 133L, 55L, 87L, 18L, 18L, 6L, 46L, 79L, 113L, 17L,
70L, 138L, 22L, 42L, 104L, 43L, 9L, 24L, 94L, 142L, 31L, 241L,
23L, 2L, 86L, 62L, 36L, 80L, 2L, 76L, 89L, 160L, 13L, 12L, 4L,
57L, 25L, 85L, 22L, 88L, 170L, 120L, 218L, 14L, 75L, 12L, 9L,
198L, 225L, 139L, 75L, 1L, 6L, 35L, 23L, 67L, 19L, 157L, 68L,
69L, 9L, 6L, 57L, 18L, 169L, 255L, 3L, 20L, 8L, 54L, 94L, 154L,
34L, 151L, 52L, 68L, 85L, 107L, 9L, 232L, 165L, 50L, 153L, 14L,
200L, 78L, 94L, 140L, 222L, 143L, 56L, 37L, 101L, 83L, 48L, 53L,
38L, 155L, 8L, 132L, 148L, 39L, 53L, 151L, 3L, 5L, 59L, 3L, 56L,
100L, 37L, 65L, 192L, 30L, 212L, 70L, 149L, 10L, 43L, 92L, 28L,
97L, 20L, 105L, 133L, 134L, 4L, 65L, 83L, 16L, 158L, 168L, 119L,
47L, 55L, 51L, 38L, 80L, 16L, 124L, 105L, 68L, 178L, 23L, 15L,
177L, 146L, 71L, 7L, 2L, 36L, 7L, 3L, 89L, 54L, 42L, 67L, 133L,
64L, 44L, 39L, 119L, 64L, 15L, 44L, 73L, 41L, 49L, 92L, 8L, 110L,
167L, 59L, 224L, 102L, 23L, 6L, 69L, 126L, 97L, 240L, 21L, 32L,
52L, 59L, 34L, 17L, 12L, 270L, 60L, 119L, 103L, 92L, 218L, 62L,
127L, 15L, 65L, 64L, 63L, 17L, 135L, 67L, 49L, 149L, 24L, 24L,
24L, 54L, 27L, 167L, 7L, 8L, 53L, 72L, 85L, 47L, 92L, 36L, 158L,
113L, 26L, 126L, 3L, 127L, 19L, 27L, 98L, 34L, 82L, 217L, 44L,
105L, 104L, 65L, 35L, 63L, 82L, 41L, 167L, 12L, 136L, 52L, 205L,
18L, 96L, 136L, 74L, 163L, 52L, 194L, 32L, 74L, 217L, 11L, 54L,
228L, 33L, 22L, 51L, 42L, 52L, 8L, 235L, 250L, 38L, 130L, 126L,
57L, 18L, 53L, 108L, 126L, 54L, 128L, 17L, 230L, 40L, 49L, 31L,
38L, 42L, 18L, 14L, 203L, 114L, 73L, 226L, 4L, 4L, 271L, 48L,
86L, 221L, 18L, 55L, 176L, 119L, 255L, 18L, 124L, 63L, 58L, 77L,
159L, 118L, 116L, 71L, 123L, 22L, 38L, 61L, 114L, 114L, 1L, 104L,
115L, 9L, 192L, 4L, 199L, 118L, 199L, 4L, 13L, 114L, 175L, 11L,
39L, 189L, 30L, 113L, 112L, 13L, 102L, 11L, 26L, 130L, 2L, 47L,
90L, 77L, 184L, 76L, 15L, 116L, 166L, 20L, 21L, 3L, 136L, 108L,
106L, 87L, 60L, 78L, 106L, 18L, 45L, 85L, 41L, 11L, 85L, 46L,
33L, 244L, 26L, 35L, 14L, 8L, 45L, 98L, 7L, 203L, 9L, 118L, 70L,
85L, 178L, 23L, 8L, 29L, 221L, 171L, 67L, 106L, 118L, 95L, 216L,
32L, 177L, 72L, 16L, 21L, 161L, 49L, 52L, 80L, 174L, 5L, 70L,
41L, 43L, 13L, 238L, 5L, 70L, 128L, 152L, 53L, 128L, 18L, 19L,
107L, 70L, 94L, 119L, 63L, 2L, 7L, 2L, 208L, 128L, 37L, 73L,
8L, 166L, 243L, 216L, 137L, 115L, 178L, 32L, 31L, 49L, 13L, 4L,
217L, 4L, 40L, 48L, 24L, 127L, 25L, 46L, 238L, 107L, 28L, 76L,
54L, 97L, 104L, 9L, 142L, 4L, 32L, 21L, 46L, 36L, 11L, 75L, 175L,
46L, 109L, 25L, 106L, 115L, 78L, 69L, 152L, 2L, 51L, 10L, 63L,
142L, 66L, 168L, 78L, 11L, 147L, 271L, 90L, 88L, 10L, 143L, 71L,
202L, 259L, 133L, 23L, 71L, 238L, 37L, 38L, 24L, 64L, 133L, 8L,
194L, 24L, 92L, 25L, 230L, 195L, 34L, 162L, 18L, 69L, 75L, 18L,
20L, 34L, 99L, 24L, 152L, 83L, 24L, 4L, 41L, 103L, 77L, 86L,
23L, 46L, 53L, 63L, 98L, 54L, 17L, 122L, 9L, 25L, 237L, 71L,
82L, 42L, 259L, 37L, 35L, 21L, 77L, 2L, 5L, 2L, 41L, 46L, 26L,
100L, 265L, 224L, 45L, 68L, 263L, 136L, 243L, 109L, 122L, 25L,
186L, 1L, 7L, 135L, 116L, 18L, 32L, 94L, 192L, 29L, 184L, 174L,
41L, 71L, 14L, 125L, 61L, 70L, 178L, 90L, 7L, 14L, 194L, 167L,
5L, 2L, 21L, 100L, 60L, 230L, 66L, 10L, 162L, 39L, 99L, 91L,
65L, 22L, 162L, 139L, 43L, 230L, 59L, 61L, 168L, 14L, 23L, 73L,
35L, 141L, 73L, 71L, 44L, 59L, 131L, 127L, 68L, 122L, 164L, 2L,
17L, 111L, 4L, 34L, 147L, 33L, 11L, 33L, 54L, 48L, 235L, 136L,
27L, 57L, 8L, 86L, 63L, 86L, 24L, 212L, 92L, 131L, 113L, 47L,
132L, 5L, 175L, 12L, 51L, 81L, 29L, 232L, 126L, 20L, 157L, 158L,
17L, 16L, 62L, 25L, 74L, 58L, 25L, 35L, 85L, 61L, 112L, 241L,
135L, 183L, 77L, 41L, 12L, 101L, 12L, 25L, 113L, 38L, 28L, 95L,
232L, 6L, 98L, 67L, 13L, 46L, 9L, 107L, 88L, 164L, 79L, 18L,
13L, 200L, 20L, 152L, 107L, 40L, 31L, 146L, 121L, 75L, 6L, 237L,
153L, 150L, 161L, 198L, 174L, 167L, 15L, 154L, 160L, 171L, 169L,
23L, 22L, 187L, 226L, 40L, 213L, 87L, 269L, 136L, 153L, 103L,
141L, 21L, 79L, 22L, 144L, 119L, 1L, 11L, 13L, 7L, 128L, 43L,
77L, 50L, 142L, 79L, 5L, 182L, 19L, 39L, 5L, 63L, 228L, 13L,
5L, 49L, 58L, 14L, 145L, 129L, 102L, 211L, 152L, 43L, 269L, 67L,
36L, 10L, 103L, 98L, 83L, 13L, 25L, 155L, 11L, 33L, 127L, 79L,
46L, 64L, 40L, 88L, 23L, 52L, 204L, 125L, 39L, 10L, 184L, 38L,
113L, 123L, 68L, 69L, 126L, 7L, 36L, 43L, 3L, 243L, 82L, 50L,
109L, 122L, 44L, 40L, 41L, 140L, 134L, 168L, 122L, 16L, 2L, 61L,
37L, 73L, 163L, 70L, 18L, 9L, 205L, 12L, 89L, 1L, 17L, 119L,
17L, 54L, 31L, 13L, 185L, 157L, 113L, 53L, 156L, 157L, 72L, 61L,
29L, 52L, 69L, 23L, 261L, 51L, 118L, 48L, 98L, 49L, 250L, 29L,
222L, 55L, 14L, 130L, 72L, 27L, 23L, 45L, 27L, 5L, 62L, 46L,
208L, 183L, 32L, 37L, 168L, 39L, 47L, 3L, 88L, 74L, 40L, 254L,
5L, 28L, 165L, 109L, 181L, 209L, 142L, 107L, 21L, 14L, 42L, 58L,
198L, 30L, 91L, 175L, 108L, 18L, 60L, 86L, 6L, 82L, 26L, 8L,
85L, 202L, 261L, 113L, 142L, 19L, 67L, 96L, 116L, 262L, 60L,
55L, 47L, 56L, 33L, 39L, 196L, 77L, 10L, 86L, 142L, 11L, 49L,
7L, 56L, 38L, 26L, 180L, 74L, 60L, 236L, 7L, 37L, 81L, 119L,
26L, 7L, 103L, 38L, 6L, 184L, 153L, 90L, 42L, 22L, 140L, 57L,
50L, 97L, 14L, 42L, 3L, 14L, 16L, 66L, 56L, 89L, 21L, 58L, 7L,
101L, 16L, 125L, 224L, 64L, 110L, 20L, 5L, 67L, 57L, 161L, 271L,
13L, 18L, 51L, 119L, 42L, 122L, 51L, 116L, 41L, 2L, 89L, 229L,
2L, 45L, 22L, 180L, 3L, 127L, 195L, 8L, 230L, 203L, 72L, 203L,
61L, 7L, 61L, 253L, 37L, 46L, 59L, 161L, 110L, 5L, 223L, 195L,
45L, 1L, 48L, 163L, 3L, 56L, 76L, 77L, 107L, 183L, 7L, 30L, 145L,
4L, 26L, 174L, 76L, 83L, 73L, 172L, 226L, 2L, 18L, 1L, 8L, 90L,
36L, 8L, 44L, 36L, 90L, 64L, 89L, 127L, 24L, 67L, 7L, 263L, 71L,
178L, 21L, 21L, 28L, 236L, 116L, 46L, 82L, 79L, 17L, 18L, 131L,
49L, 90L, 65L, 168L, 93L, 2L, 267L, 59L, 35L, 126L, 35L, 185L,
6L, 45L, 31L, 42L, 71L, 67L, 85L, 11L, 9L, 30L, 22L, 24L, 123L,
119L, 14L, 98L, 31L, 101L, 137L, 81L, 47L, 79L, 4L, 167L, 78L,
11L, 30L, 9L, 115L, 32L, 12L, 80L, 33L, 68L, 36L, 130L, 31L,
7L, 169L, 54L, 9L, 155L, 61L, 250L, 89L, 149L, 2L, 101L, 66L,
166L, 41L, 4L, 62L, 9L, 160L, 189L, 144L, 101L, 190L, 129L, 11L,
124L, 22L, 13L, 151L, 1L, 58L, 173L, 195L, 47L, 3L, 3L, 24L,
26L, 27L, 177L, 43L, 29L, 27L, 7L, 3L, 154L, 100L, 125L, 91L,
212L, 224L, 77L, 53L, 135L, 2L, 11L, 65L, 60L, 115L, 78L, 55L,
66L, 31L, 88L, 72L, 87L, 181L, 198L, 75L, 239L, 111L, 10L, 128L,
103L, 68L, 27L, 127L, 4L, 24L, 102L, 3L, 19L, 103L, 268L, 5L,
153L, 216L, 9L, 56L, 154L, 3L, 13L, 128L, 252L, 17L, 10L, 78L,
65L, 245L, 53L, 166L, 11L, 28L, 43L, 85L, 11L, 179L, 200L, 127L,
235L, 61L, 7L, 4L, 35L, 28L, 85L, 118L, 69L, 92L, 158L, 40L,
91L, 104L, 165L, 135L, 30L, 230L, 121L, 204L, 44L, 106L, 5L,
51L, 19L, 145L, 34L, 184L, 16L, 217L, 62L, 67L, 44L, 16L, 5L,
39L, 13L, 16L, 95L, 158L, 43L, 93L, 37L, 47L, 33L, 18L, 178L,
13L, 65L, 123L, 54L, 165L, 265L, 9L, 118L, 93L, 10L, 3L, 114L,
13L, 8L, 48L, 103L, 160L, 92L, 135L, 50L, 7L, 38L, 16L, 64L,
85L, 215L, 13L, 251L, 41L, 10L, 67L, 13L, 56L, 202L, 72L, 156L,
249L, 56L, 38L, 27L, 15L, 177L, 39L, 36L, 62L, 53L, 86L, 62L,
126L, 177L, 46L, 30L, 81L, 6L, 74L, 37L, 65L, 54L, 67L, 123L,
66L, 144L, 90L, 48L, 173L, 47L, 49L, 108L, 22L, 103L, 22L, 144L,
23L, 233L, 78L, 181L, 136L, 27L, 3L, 135L, 46L, 34L, 30L, 42L,
6L, 53L, 49L, 180L, 247L, 106L, 22L, 124L, 9L, 161L, 43L, 82L,
112L, 225L, 153L, 124L, 53L, 90L, 64L, 86L, 35L, 121L, 118L,
129L, 39L, 3L, 16L, 24L, 224L, 128L, 145L, 108L, 124L, 32L, 9L,
7L, 22L, 16L, 207L, 51L, 27L, 22L, 6L, 132L, 154L, 26L, 223L,
145L, 105L, 78L, 44L, 171L, 29L, 53L, 229L, 89L, 47L, 41L, 81L,
62L, 169L, 102L, 241L, 35L, 6L, 174L, 51L, 181L, 83L, 52L, 92L,
31L, 110L, 148L, 52L, 7L, 73L, 136L, 25L, 29L, 42L, 84L, 190L,
49L, 139L, 62L, 7L, 86L, 13L, 182L, 203L, 68L, 127L, 13L, 27L,
244L, 69L, 65L, 92L, 14L, 257L, 7L, 49L, 20L, 44L, 17L, 13L,
73L, 20L, 43L, 33L, 242L, 4L, 66L, 70L, 99L, 193L, 12L, 179L,
63L, 14L, 53L, 49L, 105L, 59L, 113L, 79L, 124L, 35L, 9L, 7L,
44L, 6L, 21L, 8L, 114L, 36L, 90L, 121L, 113L, 96L, 26L, 253L,
14L, 53L, 10L, 25L, 18L, 18L, 10L, 87L, 4L, 159L, 179L, 17L,
9L, 222L, 68L, 268L, 120L, 197L, 21L, 67L, 59L, 250L, 221L, 233L,
41L, 114L, 20L, 136L, 136L, 94L, 19L, 29L, 11L, 81L, 179L, 154L,
20L, 29L, 148L, 249L, 34L, 246L, 212L, 46L, 4L, 33L, 118L, 47L,
246L, 116L, 42L, 91L, 60L, 49L, 186L, 37L, 85L, 8L, 26L, 5L,
30L, 44L, 22L, 28L, 48L, 144L, 200L, 33L, 29L, 77L, 15L, 120L,
33L, 27L, 53L, 126L, 183L, 79L, 62L, 102L, 61L, 112L, 56L, 77L,
201L, 74L, 7L, 99L, 120L, 110L, 148L, 35L, 48L, 18L, 4L, 16L,
84L, 53L, 39L, 20L, 36L, 159L, 30L, 3L, 46L, 247L, 31L, 127L,
61L, 127L, 238L, 109L, 154L, 178L, 78L, 31L, 5L, 77L, 69L, 3L,
49L, 165L, 91L, 29L, 72L, 24L, 30L, 105L, 55L, 225L, 28L, 36L,
13L, 18L, 106L, 56L, 143L, 105L, 55L, 33L, 4L, 100L, 215L, 59L,
169L, 103L, 70L, 76L, 189L, 42L, 94L, 101L, 41L, 83L, 52L, 231L,
120L, 111L, 37L, 198L, 69L, 57L, 51L, 13L, 14L, 55L, 24L, 74L,
136L, 1L, 218L, 110L, 125L, 26L, 106L, 203L, 46L, 57L, 16L, 90L,
186L, 209L, 64L, 254L, 1L, 103L, 175L, 3L, 5L, 41L, 51L, 232L,
89L, 73L, 67L, 260L, 85L, 189L, 249L, 166L, 72L, 250L, 56L, 2L,
66L, 232L, 33L, 259L, 12L, 47L, 7L, 106L, 193L, 63L, 132L, 3L,
21L, 76L, 195L, 15L, 43L, 171L, 29L, 108L, 84L, 199L, 189L, 98L,
43L, 83L, 28L, 67L, 47L, 195L, 62L, 57L, 53L, 163L, 48L, 65L,
188L, 3L, 52L, 257L, 62L, 62L, 114L, 38L, 128L, 26L, 205L, 100L,
75L, 104L, 56L, 146L, 105L, 35L, 26L, 18L, 46L, 25L, 96L, 61L,
1L, 91L, 13L, 169L, 35L, 54L, 77L, 35L, 9L, 213L, 124L, 22L,
29L, 52L, 203L, 98L, 61L, 8L, 33L, 14L, 11L, 13L, 48L, 105L,
76L, 22L, 136L, 123L, 18L, 39L, 39L, 9L, 212L, 11L, 37L, 9L,
59L, 254L, 18L, 85L, 38L, 180L, 159L, 94L, 42L, 15L, 230L, 38L,
35L, 19L, 98L, 185L, 10L, 24L, 103L, 67L, 8L, 63L, 200L, 135L,
34L, 39L, 19L, 62L, 175L, 13L, 9L, 1L, 37L, 116L, 41L, 42L, 105L,
54L, 17L, 90L, 47L, 38L, 34L, 23L, 105L, 23L, 57L, 115L, 107L,
58L, 50L, 121L, 123L, 23L, 99L, 31L, 148L, 9L, 106L, 4L, 76L,
55L, 102L, 66L, 135L, 43L, 73L, 7L, 255L, 15L, 24L, 229L, 115L,
55L, 52L, 18L, 22L, 39L, 181L, 1L, 135L, 45L, 103L, 24L, 180L,
118L, 228L, 219L, 116L, 90L, 154L, 35L, 73L, 65L, 48L, 58L, 35L,
26L, 166L, 66L, 128L, 15L, 28L, 109L, 154L, 3L, 24L, 52L, 89L,
50L, 53L, 69L, 17L, 15L, 124L, 50L, 134L, 267L, 11L, 194L, 6L,
143L, 40L, 35L, 223L, 12L, 27L, 45L, 181L, 60L, 37L, 19L, 6L,
24L, 57L, 75L, 12L, 93L, 38L, 27L, 140L, 32L, 57L, 115L, 82L,
262L, 5L, 185L, 223L, 10L, 72L, 7L, 110L, 12L, 81L, 61L, 29L,
91L, 12L, 85L, 62L, 34L, 73L, 27L, 16L, 85L, 216L, 228L, 157L,
66L, 73L, 38L, 88L, 26L, 83L, 184L, 10L, 108L, 43L, 11L, 3L,
47L, 61L, 139L, 10L, 8L, 69L, 11L, 63L, 224L, 82L, 5L, 22L, 3L,
51L, 39L, 5L, 232L, 150L, 93L, 89L, 174L, 5L, 85L, 159L, 49L,
150L, 187L, 101L, 29L, 20L, 48L, 4L, 142L, 44L, 57L, 105L, 79L,
51L, 91L, 89L, 115L, 14L, 67L, 2L, 165L, 114L, 2L, 17L, 67L,
38L, 108L, 23L, 103L, 223L, 1L, 34L, 21L, 41L, 73L, 186L, 55L,
14L, 61L, 81L, 75L, 15L, 95L, 85L, 145L, 222L, 139L, 231L, 162L,
79L, 67L, 80L, 75L, 17L, 27L, 48L, 38L, 27L, 71L, 100L, 51L,
132L, 2L, 183L, 110L, 23L, 37L, 103L, 30L, 43L, 138L, 1L, 13L,
83L, 180L, 27L, 21L, 236L, 78L, 118L, 93L, 95L, 83L, 28L, 15L,
236L, 41L, 51L, 11L, 181L, 91L, 4L, 40L, 86L, 165L, 24L, 115L,
252L, 28L, 35L, 13L, 15L, 7L, 9L, 27L, 33L, 9L, 40L, 5L, 105L,
28L, 5L, 16L, 117L, 153L, 27L, 141L, 52L, 168L, 10L, 84L, 17L,
47L, 56L, 233L, 140L, 69L, 221L, 19L, 8L, 71L, 37L, 123L, 137L,
10L, 55L, 146L, 14L, 41L, 69L, 142L, 89L, 4L, 37L, 170L, 37L,
35L, 182L, 70L, 24L, 158L, 83L, 25L, 38L, 116L, 132L, 209L, 69L,
221L, 41L, 114L, 28L, 20L, 42L, 132L, 83L, 168L, 87L, 64L, 249L,
155L, 66L, 113L, 44L, 35L, 100L, 133L, 31L, 126L, 10L, 184L,
53L, 64L, 57L, 22L, 2L, 30L, 25L, 39L, 151L, 164L, 42L, 72L,
2L, 38L, 29L, 8L, 22L, 9L, 91L, 58L, 58L, 78L, 82L, 117L, 104L,
29L, 80L, 70L, 137L, 137L, 115L, 10L, 87L, 66L, 1L, 11L, 21L,
118L, 262L, 70L, 5L, 153L, 118L, 35L, 249L, 68L, 38L, 79L, 30L,
39L, 39L, 158L, 17L, 145L, 5L, 8L, 47L, 177L, 77L, 203L, 94L,
107L, 96L, 68L, 7L, 12L, 24L, 18L, 146L, 13L, 164L, 54L, 73L,
143L, 96L, 22L, 5L, 100L, 71L, 65L, 1L, 16L, 22L, 13L, 39L, 101L,
39L, 75L, 148L, 45L, 257L, 67L, 18L, 50L, 62L, 29L, 222L, 96L,
7L, 7L, 130L, 108L, 44L, 48L, 109L, 67L, 112L, 100L, 169L, 260L,
130L, 169L, 79L, 111L, 121L, 15L, 21L, 240L, 220L, 56L, 8L, 18L,
4L, 37L, 98L, 46L, 247L, 66L, 69L, 19L, 66L, 112L, 42L, 103L,
122L, 155L, 36L, 4L, 60L, 39L, 25L, 2L, 182L, 105L, 157L, 5L,
70L, 16L, 55L, 52L, 39L, 156L, 14L, 118L, 88L, 91L, 132L, 52L,
18L, 38L, 31L, 35L, 75L, 186L, 45L, 110L, 232L, 52L, 135L, 33L,
11L, 29L, 129L, 147L, 20L, 20L, 59L, 46L, 6L, 53L, 251L, 120L,
192L, 41L, 87L, 38L, 134L, 5L, 120L, 130L, 71L, 121L, 84L, 183L,
166L, 20L, 8L, 20L, 74L, 201L, 35L, 176L, 189L, 17L, 231L, 48L,
38L, 3L, 142L, 53L, 199L, 135L, 6L, 38L, 256L, 76L, 6L, 56L,
154L, 25L, 76L, 69L, 149L, 107L, 113L, 246L, 61L, 23L, 6L, 76L,
3L, 68L, 70L, 89L, 130L, 226L, 31L, 157L, 24L, 80L, 170L, 169L,
64L, 12L, 110L, 47L, 141L, 159L, 22L, 53L, 167L, 61L, 81L, 98L,
172L, 261L, 99L, 9L, 13L, 132L, 103L, 16L, 97L, 186L, 35L, 128L,
73L, 136L, 62L, 187L, 30L, 31L, 26L, 115L, 76L, 260L, 54L, 11L,
169L, 227L, 43L, 6L, 23L, 212L, 23L, 68L, 119L, 181L, 34L, 137L,
144L, 48L, 101L, 25L, 10L, 92L, 5L, 92L, 132L, 206L, 44L, 113L,
9L, 25L, 249L, 69L, 250L, 67L, 35L, 6L, 60L, 251L, 6L, 32L, 94L,
13L, 224L, 21L, 43L, 81L, 9L, 9L, 95L, 11L, 7L, 26L, 172L, 46L,
17L, 3L, 2L, 39L, 26L, 7L, 18L, 57L, 88L, 16L, 47L, 136L, 135L,
73L, 26L, 60L, 56L, 77L, 158L, 23L, 1L, 139L, 234L, 76L, 99L,
28L, 22L, 83L, 114L, 6L, 122L, 7L, 36L, 59L, 4L, 33L, 79L, 25L,
26L, 8L, 28L, 19L, 33L, 2L, 23L, 44L, 158L, 56L, 14L, 8L, 56L,
16L, 36L, 90L, 18L, 22L, 7L, 74L, 70L, 2L, 51L, 13L, 130L, 25L,
17L, 23L, 48L, 37L, 60L, 17L, 58L, 15L, 41L, 261L, 245L, 35L,
17L, 41L, 234L, 13L, 11L, 192L, 3L, 5L, 29L, 14L, 34L, 4L, 110L,
63L, 47L, 157L, 9L, 116L, 120L, 29L, 126L, 26L, 106L, 219L, 209L,
93L, 255L, 137L, 88L, 96L, 87L, 229L, 23L, 128L, 101L, 62L, 2L,
193L, 58L, 1L, 8L, 146L, 44L, 12L, 27L, 99L, 270L, 54L, 41L,
161L, 231L, 53L, 126L, 139L, 77L, 55L, 32L, 6L, 159L, 131L, 54L,
266L, 87L, 13L, 205L, 154L, 3L, 82L, 35L, 11L, 2L, 56L, 84L,
110L, 116L, 28L, 30L, 60L, 74L, 12L, 147L, 31L, 206L, 31L, 56L,
209L, 115L, 149L, 33L, 198L, 205L, 71L, 28L, 40L, 201L, 32L,
3L, 40L, 75L, 91L, 32L, 9L, 4L, 192L, 11L, 41L, 30L, 46L, 57L,
44L, 243L, 67L, 118L, 108L, 181L, 83L, 45L, 93L, 13L, 2L, 104L,
163L, 92L, 8L, 17L, 14L, 150L, 5L, 60L, 123L, 100L, 105L, 110L,
225L, 249L, 207L, 100L, 188L, 138L, 6L, 176L, 68L, 91L, 8L, 20L,
18L, 21L, 79L, 20L, 4L, 99L, 136L, 28L, 156L, 7L, 36L, 226L,
33L, 42L, 1L, 28L, 227L, 11L, 9L, 157L, 206L, 34L, 17L, 61L,
113L, 112L, 158L, 24L, 18L, 36L, 75L, 40L, 18L, 183L, 3L, 37L,
92L, 69L, 13L, 213L, 48L, 163L, 188L, 251L, 59L, 75L, 1L, 12L,
46L, 232L, 13L, 74L, 32L, 149L, 219L, 22L, 59L, 109L, 264L, 25L,
141L, 5L, 67L, 41L, 5L, 71L, 19L, 63L, 114L, 28L, 76L, 80L, 86L,
71L, 18L, 166L, 40L, 57L, 185L, 88L, 115L)
The problem is that you initially created 4000 * 3 data.frame filled in with NA. Please see the corrected code. I did not put your actual data vec1 (too long) and simulated vec1 with sampling from exponential distribution. Additionally I used colMeans as more effective than apply. See the code below:
# vec1, mydata, l - simulation
set.seed(123)
vec1 <- (sample(1:271, 4000, replace = TRUE, prob = dexp(1:271, rate = .01)))
mydata <- matrix(1:(300 * 300), nrow = 300)
l <- 300
# data given by OP
df <- data.frame(Age = 1, Weight = 1, height = 1 )
df <- df[-1, ]
i <- 1
j <- vec1[1] - 1
k <- 0
repeat{
elements <- as.vector(colMeans(mydata[i:(j + 1), 3:5]))
df <- rbind(df, elements)
k <- k + 1
i = i + vec1[k]
j = j + vec1[k + 1]
if (j + 1 >= l){
break
}
}
df <- setNames(df, c("Age","Weight", "height"))
df
Output:
Age Weight height
1 608.0 908.0 1208.0
2 638.0 938.0 1238.0
3 716.0 1016.0 1316.0
4 787.5 1087.5 1387.5
5 816.0 1116.0 1416.0
6 835.0 1135.0 1435.0

Exact number of every value next to the bar [duplicate]

This question already has answers here:
How to put labels over geom_bar in R with ggplot2
(4 answers)
Closed 5 years ago.
Having a dataset like this:
df <- structure(list(word = structure(c(1L, 12L, 23L, 34L, 43L, 44L,
45L, 46L, 47L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 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), .Label = c("word1", "word10", "word11", "word12", "word13",
"word14", "word15", "word16", "word17", "word18", "word19", "word2",
"word20", "word21", "word22", "word23", "word24", "word25", "word26",
"word27", "word28", "word29", "word3", "word30", "word31", "word32",
"word33", "word34", "word35", "word36", "word37", "word38", "word39",
"word4", "word40", "word41", "word42", "word43", "word44", "word45",
"word46", "word47", "word5", "word6", "word7", "word8", "word9"
), class = "factor"), frq = c(1975L, 1665L, 1655L, 1469L, 1464L,
1451L, 1353L, 1309L, 1590L, 1545L, 1557L, 1556L, 1130L, 1153L,
1151L, 1150L, 1144L, 1141L, 1115L, 194L, 195L, 135L, 135L, 130L,
163L, 167L, 164L, 159L, 153L, 145L, 143L, 133L, 133L, 153L, 153L,
150L, 119L, 115L, 115L, 115L, 114L, 113L, 113L, 113L, 115L, 102L,
101L)), .Names = c("word", "frq"), class = "data.frame", row.names = c(NA,
-47L))
With this command lines I produce a bar plot graph
dat2 = transform(df,word = reorder(word,frq))
df2 <- head(dat2, 10)
p = ggplot(df2, aes(x = word, y = frq)) + geom_bar(stat = "identity", fill = "yellow")
p2 <- p +coord_flip()
How is it possible to have the number of frq in the end of every bar?
I would use annotate..
p2 + annotate(geom = "text",x = df2$word, y= df2$frq, label = df2$frq)

Scale in word cloud

I have this dataframe
df <- structure(list(word = structure(c(1L, 12L, 23L, 34L, 43L, 44L,
45L, 46L, 47L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 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), .Label = c("word1", "word10", "word11", "word12", "word13",
"word14", "word15", "word16", "word17", "word18", "word19", "word2",
"word20", "word21", "word22", "word23", "word24", "word25", "word26",
"word27", "word28", "word29", "word3", "word30", "word31", "word32",
"word33", "word34", "word35", "word36", "word37", "word38", "word39",
"word4", "word40", "word41", "word42", "word43", "word44", "word45",
"word46", "word47", "word5", "word6", "word7", "word8", "word9"
), class = "factor"), frq = c(1975L, 1665L, 1655L, 1469L, 1464L,
1451L, 1353L, 1309L, 1590L, 1545L, 1557L, 1556L, 1130L, 1153L,
1151L, 1150L, 1144L, 1141L, 1115L, 194L, 195L, 135L, 135L, 130L,
163L, 167L, 164L, 159L, 153L, 145L, 143L, 133L, 133L, 153L, 153L,
150L, 119L, 115L, 115L, 115L, 114L, 113L, 113L, 113L, 115L, 102L,
101L)), .Names = c("word", "frq"), class = "data.frame", row.names = c(NA,
-47L))
And I would like to create a word cloud based on the frequency.
In order to make this word cloud I use this lines of code:
library(wordcloud2)
wordcloud2(df, color = "random-light", backgroundColor = "green")
However an issue I face is that the word cloud doesn't have all the words. When I refer all words I mean these words with the lowest frequency.
Is there any scale in order to have all words in word cloud and the most frequency word be a little smaller in order to see all words?
As I can understand this happens because the highest frequencies have big difference with the others in the list
You should directly rescale your frequencies. You could for example do :
p_scaled = p^(1/n) with n sufficiently big so that even the words with the lowest frequencies would appear on the word cloud.

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