How to make the graph easier to read ? Editing plot - r

I would like to ask you for the suggestions how I can edit my plot function to make my graph more clear ?
Here I show you the code which I use for plotting:
# open the pdf file
pdf(file='LSF1_PWD_GWD.pdf')
a <- c('LSF1', 'PWD', 'GWD')
rowsToPlot<-c(1066,2269,109)
matplot(as.matrix(t(tbl_alles[rowsToPlot,])),type=rep("l", length(rowsToPlot)), col=rainbow(length(rowsToPlot)),xlab = 'Fraction Size', ylab = 'Intensity')
legend('topright',a,lty=1, bty='n', cex=.75, col = rainbow(length(rowsToPlot)))
# close the pdf file
dev.off()
and that's how the graph looks like:
It's just a basic plot because I have no idea how to edit it. The arrow indicates three lines on one position which you can't see because they overlap... and that's the most important part of this graph for me. Maybe I shouldn't use dotted line ? How to change it ?
Data:
tbl_alles <-
structure(list("10" = c(0, 0, 0, 0, 0, 0),
"20" = c(0, 0, 0, 0, 0, 0),
"52.5" = c(0, 0, 0, 0, 0, 0),
"81" = c(0, 0, 1, 0, 0, 0),
"110" = c(0, 0, 0, 0, 0, 0),
"140.5" = c(0, 0, 0, 0, 0, 0),
"189" = c(0, 0, 0, 0, 0, 0),
"222.5" = c(0, 0, 0, 0, 0, 0 ),
"278" = c(0, 0, 0, 0, 0, 0),
"340" = c(0, 0, 0, 0, 0, 0),
"397" = c(0, 1, 0, 0, 0, 0),
"453.5" = c(0, 0.66069369, 0, 0, 0, 1),
"529" = c(0, 0.521435654, 0, 0, 1, 0),
"580" = c(0, 0.437291195, 0, 0, 1, 0),
"630.5" = c(0, 0.52204783, 0, 0, 0, 0),
"683.5" = c(0, 0.52429838, 0, 0, 0, 0),
"735.5" = c(1, 0.3768651, 0, 1, 0, 0),
"784" = c(0, 0, 0, 0, 0, 0),
"832" = c(0, 0, 0, 0, 0, 0),
"882.5" = c(0, 0, 0, 0, 0, 0),
"926.5" = c(0, 0, 0, 0, 0, 0),
"973" = c(0, 0, 0, 0, 0, 0),
"1108" = c(0, 0, 0, 0, 0, 0),
"1200" = c(0, 0, 0, 0, 0, 0)),
.Names = c("10", "20", "52.5", "81",
"110", "140.5","189", "222.5",
"278", "340", "397", "453.5",
"529", "580", "630.5", "683.5",
"735.5", "784", "832", "882.5",
"926.5", "973", "1108", "1200"),
row.names = c("at1g01050.1", "at1g01080.1",
"at1g01090.1","at1g01220.1",
"at1g01420.1", "at1g01470.1"),
class = "data.frame")
RowsToPlot:
> dput(tbl_alles[rowsToPlot,])
structure(list(`10` = c(0, 0, 0), `20` = c(0, 0, 0), `52.5` = c(0,
0, 0), `81` = c(0, 0, 0), `110` = c(0, 0, 0), `140.5` = c(0,
0, 0), `189` = c(0, 0, 0), `222.5` = c(0, 0, 0), `278` = c(0,
0, 0), `340` = c(0, 0, 0), `397` = c(0, 0, 0), `453.5` = c(0,
0, 0), `529` = c(0, 0, 0), `580` = c(0, 0, 0), `630.5` = c(0,
0, 0), `683.5` = c(0, 0, 0.57073483), `735.5` = c(0, 1, 0.85691826
), `784` = c(0, 0, 0.90706982), `832` = c(1, 1, 1), `882.5` = c(0,
0, 0), `926.5` = c(0, 0, 0), `973` = c(0, 0, 0), `1108` = c(0,
0, 0), `1200` = c(0, 0, 0)), .Names = c("10", "20", "52.5", "81",
"110", "140.5", "189", "222.5", "278", "340", "397", "453.5",
"529", "580", "630.5", "683.5", "735.5", "784", "832", "882.5",
"926.5", "973", "1108", "1200"), row.names = c("at3g01510.1",
"at5g26570.1", "at1g10760.1"), class = "data.frame")

Okay, here's a way to distinguish the lines clearly, while keeping everything on one plot. I use non solid linetypes and different sizes to 'make room' for the overlayed lines.
library(reshape2)
library(ggplot2)
dat <- as.data.frame(as.matrix(t(tbl_alles)))
dat$x <- as.numeric(row.names(dat))
ggplot(melt(dat, id.vars='x'), aes(x=x, y=value, group=variable)) +
geom_line(aes(color=variable, linetype=variable, size=variable)) +
scale_linetype_manual(values=c('solid', 'dotted', 'dashed')) +
scale_size_manual(values=c(1,3,1)) +
scale_color_manual(values=c('black', 'red', 'white')) +
theme(axis.text = element_text(color='black'),
panel.background = element_rect('grey'),
legend.key = element_rect('grey'),
panel.grid = element_blank()) +
labs(title='This is not a pretty chart, but you can make out the lines')
I took as a starting point your data from the dput you pasted above:
tbl_alles <- structure(list(`10` = c(0, 0, 0), `20` = c(0, 0, 0), `52.5` = c(0, 0, 0), `81` = c(0, 0, 0), `110` = c(0, 0, 0), `140.5` = c(0, 0, 0), `189` = c(0, 0, 0), `222.5` = c(0, 0, 0), `278` = c(0, 0, 0), `340` = c(0, 0, 0), `397` = c(0, 0, 0), `453.5` = c(0, 0, 0), `529` = c(0, 0, 0), `580` = c(0, 0, 0), `630.5` = c(0, 0, 0), `683.5` = c(0, 0, 0.57073483), `735.5` = c(0, 1, 0.85691826), `784` = c(0, 0, 0.90706982), `832` = c(1, 1, 1), `882.5` = c(0, 0, 0), `926.5` = c(0, 0, 0), `973` = c(0, 0, 0), `1108` = c(0, 0, 0), `1200` = c(0, 0, 0)), .Names = c("10", "20", "52.5", "81", "110", "140.5", "189", "222.5", "278", "340", "397", "453.5", "529", "580", "630.5", "683.5", "735.5", "784", "832", "882.5", "926.5", "973", "1108", "1200"), row.names = c("at3g01510.1", "at5g26570.1", "at1g10760.1"), class = "data.frame")

This is most certainly not what you need, but perhaps it can give you another idea.
X=structure(list(`10` = c(0, 0, 0), `20` = c(0, 0, 0), `52.5` = c(0,
0, 0), `81` = c(0, 0, 0), `110` = c(0, 0, 0), `140.5` = c(0,
0, 0), `189` = c(0, 0, 0), `222.5` = c(0, 0, 0), `278` = c(0,
0, 0), `340` = c(0, 0, 0), `397` = c(0, 0, 0), `453.5` = c(0,
0, 0), `529` = c(0, 0, 0), `580` = c(0, 0, 0), `630.5` = c(0,
0, 0), `683.5` = c(0, 0, 0.57073483), `735.5` = c(0, 1, 0.85691826
), `784` = c(0, 0, 0.90706982), `832` = c(1, 1, 1), `882.5` = c(0,
0, 0), `926.5` = c(0, 0, 0), `973` = c(0, 0, 0), `1108` = c(0,
0, 0), `1200` = c(0, 0, 0)), .Names = c("10", "20", "52.5", "81",
"110", "140.5", "189", "222.5", "278", "340", "397", "453.5",
"529", "580", "630.5", "683.5", "735.5", "784", "832", "882.5",
"926.5", "973", "1108", "1200"), row.names = c("at3g01510.1",
"at5g26570.1", "at1g10760.1"), class = "data.frame");
library(ggplot2)
library(reshape2)
library(data.table)
X.dt<-as.data.table(t(X))
X.dt[,X:=1:dim(X.dt)[1]]
X.dt<-melt(X.dt, id='X')
ggplot(X.dt,aes(X, value,group=variable,color=variable))+
geom_line()+
facet_wrap(~variable, nrow=3)+
guides(color=FALSE)+labs(x="X",y="Intensity")

Since you have a discrete number of x values, I suggest using a barplot instead. This will make the categories easier to distinguish and highlight the aspect you are most interested in.
First put the data in long format
dat <- structure(list(`10` = c(0, 0, 0), `20` = c(0, 0, 0), `52.5` = c(0, 0, 0),
`81` = c(0, 0, 0), `110` = c(0, 0, 0), `140.5` = c(0, 0, 0),
`189` = c(0, 0, 0), `222.5` = c(0, 0, 0), `278` = c(0, 0, 0),
`340` = c(0, 0, 0), `397` = c(0, 0, 0), `453.5` = c(0, 0, 0),
`529` = c(0, 0, 0), `580` = c(0, 0, 0), `630.5` = c(0, 0, 0),
`683.5` = c(0, 0, 0.57073483), `735.5` = c(0, 1, 0.85691826),
`784` = c(0, 0, 0.90706982), `832` = c(1, 1, 1),
`882.5` = c(0, 0, 0), `926.5` = c(0, 0, 0), `973` = c(0, 0, 0),
`1108` = c(0, 0, 0), `1200` = c(0, 0, 0)),
.Names = c("10", "20", "52.5", "81", "110", "140.5", "189",
"222.5", "278", "340", "397", "453.5", "529", "580",
"630.5", "683.5", "735.5", "784", "832", "882.5",
"926.5", "973", "1108", "1200"),
row.names = c("at3g01510.1", "at5g26570.1", "at1g10760.1"),
class = "data.frame")
library(tidyr)
dat$rowname <- rownames(dat)
ggdat <- gather(dat, key = "colname", value = "Intensity", -rowname)
Then create the barplot using ggplot2
library(RColorBrewer)
library(ggplot2)
colors <- brewer.pal(nrow(dat), "Dark2")
ggplot(data = ggdat, aes(x = colname, y = Intensity, fill = rowname)) +
geom_bar(aes(color = rowname), stat = "identity",
position = position_dodge(), width = 0.75) +
scale_fill_manual(values = colors) +
scale_color_manual(values = colors) +
theme(axis.title.x = element_blank(),
axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5),
legend.position = "bottom")
The code could be used for more than 3 rows, although the bars will get harder to distinguish with more categories. If this is a problem, you could consider dropping/binning x values, or perhaps splitting the plot into two:
ggdat$group <- factor(ggdat$colname %in% colnames(dat)[1:12],
levels = c(TRUE, FALSE), labels = c("Low x", "High x"))
ggplot(data = ggdat, aes(x = colname, y = Intensity, fill = rowname)) +
geom_bar(aes(color = rowname), stat = "identity",
position = position_dodge(), width = 0.75) +
scale_fill_manual(values = colors) +
scale_color_manual(values = colors) +
theme(axis.title.x = element_blank(),
axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5),
legend.position = "bottom") +
facet_wrap(~ group, ncol = 1, scales = "free_x")

How many records does the dataset have? It seems you are dealing with an overplotting issue. Follow #Nikos method to tidy the data.
Use size and alpha to change the size and transparency of the line.
ggplot(data = X.dt, aes(x = X, y = value, group = variable, color = variable)) +
geom_line(data = X.dt, aes(x = X, y = value, group = variable, color = variable),
size = 3, alpha = .25)
The color of the line changes as they overlap. However this will only work for smaller datasets. My only other suggestion is to overlay geom_line() with geom_point() that will plot points over the lines. You can use position = position_jitter() to slightly augment the position of the points, that way if they overlap you can see where they overlap.
ggplot(data = X.dt, aes(x = X, y = value, group = variable, color = variable)) +
geom_point(position = position_jitter(w = 0.001, h = 0.02), size = 3, alpha = .5) +
geom_line(data = X.dt, aes(x = X, y = value, group = variable, color = variable), size = 1, alpha = .25)

You can try to play with the line types but this can become really difficult if you have too much lines to see : is 3 the maximum you'll have ? Else, you may consider another way to draw your data.
Here is an example with your data, when I plot it, I can see the 3 lines :
matplot(as.matrix(t(tbl_alles[rowsToPlot,])),type="l",lwd=2,lty=c("solid","48","36"), col=rainbow(length(rowsToPlot)),xlab = 'Fraction Size', ylab = 'Intensity')
legend('topright',c('LSF1', 'PWD', 'GWD'),lty=c("solid","48","36"),lwd=2, bty='n', cex=.75, col = rainbow(length(rowsToPlot)))
the 3 line types :
solid: this is the default type, as you already know...
48: first 4 units of line then a blank of 8 units
36: first 3 units of line then a blank of 6 units.
I also changed the width of the line with lwd=2.
There is another parameter to play with : transparency.
If (keeping the different lty) you change the colors to c("#FF000030","#0000FF50","#00FF0080") for example, it will be easier to see every lines (the two last characters of each hexadecimal code specify the transparency).
If you use transparency, then you can even specify a unique color and ovelapping lines will appear darker : for example, col=#00000044".

Related

How to create and export multiple plots to jpeg format in r?

I have been creating a bar plot for the result of a sentiment analysis model in R. The data is very confidential feedbacks from the customers. So, the feedbacks are then fed into a sentiment analysis model to generate outputs. My work is to generate a chart for each combination for example zone = delhi and delhi has sub zones like eastdelhi, westdelhi,northdelhi,southdelhi. I want to generate charts with combination like
zone = delhi and sub-zone = eastdelhi. And I want to save it to a jpeg file.I have written a for loop to do so. But for some reason it isn't working. This is the code
#Set locales
rm(list = ls())
Sys.setlocale(category = "LC_ALL",locale = "English")
#Load libraries
LoadLibraries <- c("openxlsx",
"dplyr",
"tidyr",
"plotly",
"RColorBrewer",
"shiny",
"officer",
"parallel",
"dplyr",
"tidyr",
"magrittr",
"knitr")
lapply(LoadLibraries, require, character.only = TRUE)
path = "C:/Users/R_Visual/Data/visual_data.xlsx"
input_data <- read.xlsx(path)
name <- names(input_data[,1:10])
#Filtering the zones and circles
for (i in 1:length(unique(Zone.Final))){
for (j in 1:length(unique(Circle.Final))){
fileName = 'C:/Users/R_Visual/'+ str(i) + str(j) + '.jpeg'
jpeg(fileName, width = 900, height = 450)
df <- input_data %>%
filter(input_data$Zone.Final[i])
df <- df %>%
filter(df$Circle.Final[j])
color <- c("#ca2f27","#f56d43","#f8c38a","#fde08b","#d9ef8b","#a7d86f","#67bd64","#1a984f","#D3D3D3","#A9A9A9")
plot <- barplot(sort(colSums(input_data[, 1:10])),
main = paste("Sentiment Analysis for Zone",df$Zone.Final[i]," and Circle",df$Circle.Final[j], sep = ""),
xlab = "Sentiments",
ylab = "Count",
horiz = FALSE,
names = name,
col = color,
border = FALSE,
legend = TRUE,
beside = TRUE,
legend.text = name,
args.legend = list(bty = "n", x = "topleft",ncol = 1, cex = 0.8, y.intersp = 0.8, x.intersp = 0.25, horiz = F, xpd = TRUE, inset = c(0,0)))
dev.off()
}
}
EDIT:
This is the sample of input_data
> dput(input_data)
structure(list(anger = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), anticipation = c(1,
0, 0, 0, 0, 0, 1, 0, 0, 0), disgust = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0), fear = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), joy = c(0,
0, 0, 0, 0, 0, 1, 0, 0, 0), sadness = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0), surprise = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), trust = c(0,
0, 1, 1, 1, 0, 2, 0, 0, 0), negative = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0), positive = c(1, 0, 0, 0, 1, 1, 2, 1, 0, 1), Zone.Final = c("Delhi",
"Lucknow", "Durgapur", "Lucknow", "Mumbai", "Bhopal", "Chandigarh",
"Chandigarh", "Gurugram", "Chandigarh"), Circle.Final = c("Noida",
"Gorakhpur", "Murshidabad", "Gorakhpur", "Mumbai City", "Bhopal",
"Chandigarh", "Panchkula", "Hisar", "Karnal")), row.names = c(NA,
10L), class = "data.frame")
If anyone could help me with the code, it would be of great help.
You can try creating a list combining the zone and subzone:
#Data
input_data <- structure(list(anger = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), anticipation = c(1,
0, 0, 0, 0, 0, 1, 0, 0, 0), disgust = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0), fear = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), joy = c(0,
0, 0, 0, 0, 0, 1, 0, 0, 0), sadness = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0), surprise = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), trust = c(0,
0, 1, 1, 1, 0, 2, 0, 0, 0), negative = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0), positive = c(1, 0, 0, 0, 1, 1, 2, 1, 0, 1), Zone.Final = c("Delhi",
"Lucknow", "Durgapur", "Lucknow", "Mumbai", "Bhopal", "Chandigarh",
"Chandigarh", "Gurugram", "Chandigarh"), Circle.Final = c("Noida",
"Gorakhpur", "Murshidabad", "Gorakhpur", "Mumbai City", "Bhopal",
"Chandigarh", "Panchkula", "Hisar", "Karnal")), row.names = c(NA,
10L), class = "data.frame")
#Code
#First create and global id to combine zone and subzone
df <- input_data
df$id <- paste(df$Zone.Final,df$Circle.Final,sep = '-')
#Split
List <- split(df,df$id)
#Plot
color <- c("#ca2f27","#f56d43","#f8c38a","#fde08b","#d9ef8b","#a7d86f","#67bd64","#1a984f","#D3D3D3","#A9A9A9")
#Plot names
vnames <- paste0(names(List),'.jpeg')
#Loop
for(i in 1:length(List))
{
name <- names(List[[i]][, 1:10])
#Plot
jpeg(filename = vnames[i], width = 900, height = 450)
barplot(sort(colSums(List[[i]][, 1:10])),
main = paste("Sentiment Analysis for Zone ",
unique(List[[i]]$Zone.Final),
" and Circle ",unique(List[[i]]$Circle.Final), sep = ""),
xlab = "Sentiments",
ylab = "Count",
horiz = FALSE,
names = name,
col = color,
border = FALSE,
legend = TRUE,
beside = TRUE,
legend.text = name,
args.legend = list(bty = "n", x = "topleft",ncol = 1,
cex = 0.8, y.intersp = 0.8, x.intersp = 0.25,
horiz = F, xpd = TRUE, inset = c(0,0)))
dev.off()
}
That will create the plots. Of course you can add a path to vnames like the dir you have to save the plots in that folder.

Sum columns of dataframe

I have dataframe with the following structure:
structure(list(`Monday_04:00` = c(0, 0, 0), `Monday_04:15` = c(0,
0, 0), `Monday_04:30` = c(0, 0, 0), `Monday_04:45` = c(0, 0,
0), `Monday_05:00` = c(0, 0, 0), `Monday_05:15` = c(0, 0, 0),
`Monday_05:30` = c(0, 0, 0), `Monday_05:45` = c(0, 0, 0),
`Monday_06:00` = c(0, 0, 0), `Monday_06:15` = c(0, 0, 0),
`Monday_06:30` = c(0, 0, 0), `Monday_06:45` = c(0, 0, 0),
`Monday_07:00` = c(0, 0, 0), `Monday_07:15` = c(0, 0, 0),
`Monday_07:30` = c(0, 0, 0), `Monday_07:45` = c(0, 0, 0),
`Monday_08:00` = c(2, 0, 0), `Monday_08:15` = c(2, 0, 0),
`Monday_08:30` = c(2, 0, 0), `Monday_08:45` = c(2, 0, 0),
`Monday_09:00` = c(2, 0, 0), `Monday_09:15` = c(2, 0, 0),
`Monday_09:30` = c(2, 0, 0), `Monday_09:45` = c(2, 0, 0),
`Monday_10:00` = c(2, 0, 0), `Monday_10:15` = c(2, 0, 0),
`Monday_10:30` = c(2, 0, 0), `Monday_10:45` = c(2, 0, 0),
`Monday_11:00` = c(2, 0, 0), `Monday_11:15` = c(2, 0, 0),
`Monday_11:30` = c(2, 0, 0), `Monday_11:45` = c(2, 0, 0),
`Monday_12:00` = c(0, 0, 0), `Monday_12:15` = c(0, 0, 0),
`Monday_12:30` = c(0, 0, 0), `Monday_12:45` = c(0, 0, 0),
`Monday_13:00` = c(2, 0, 0), `Monday_13:15` = c(2, 0, 0),
`Monday_13:30` = c(2, 0, 0), `Monday_13:45` = c(2, 0, 0),
`Monday_14:00` = c(2, 0, 0), `Monday_14:15` = c(2, 0, 0),
`Monday_14:30` = c(2, 0, 0), `Monday_14:45` = c(2, 0, 0),
`Monday_15:00` = c(2, 0, 0), `Monday_15:15` = c(2, 0, 0),
`Monday_15:30` = c(2, 0, 0), `Monday_15:45` = c(2, 0, 0),
`Monday_16:00` = c(2, 0, 0), `Monday_16:15` = c(2, 0, 0),
`Monday_16:30` = c(2, 0, 0), `Monday_16:45` = c(2, 0, 0),
`Monday_17:00` = c(2, 0, 0), `Monday_17:15` = c(2, 0, 0),
`Monday_17:30` = c(2, 0, 0), `Monday_17:45` = c(2, 0, 0),
`Monday_18:00` = c(2, 0, 0), `Monday_18:15` = c(2, 0, 0),
`Monday_18:30` = c(0, 0, 0), `Monday_18:45` = c(0, 0, 0),
`Monday_19:00` = c(0, 0, 0), `Monday_19:15` = c(0, 0, 0),
`Monday_19:30` = c(0, 0, 0), `Monday_19:45` = c(0, 0, 0),
`Monday_20:00` = c(0, 0, 0), `Monday_20:15` = c(0, 0, 0),
`Monday_20:30` = c(0, 0, 0), `Monday_20:45` = c(0, 0, 0),
`Monday_21:00` = c(0, 0, 0), `Monday_21:15` = c(0, 0, 0),
`Monday_21:30` = c(0, 0, 0), `Monday_21:45` = c(0, 0, 0),
`Monday_22:00` = c(0, 0, 0), `Monday_22:15` = c(0, 0, 0),
`Monday_22:30` = c(0, 0, 0), `Monday_22:45` = c(0, 0, 0),
`Monday_23:00` = c(0, 0, 0), `Monday_23:15` = c(0, 0, 0),
`Monday_23:30` = c(0, 0, 0), `Monday_23:45` = c(0, 0, 0),
`Monday_00:00` = c(0, 0, 0), `Monday_00:15` = c(0, 0, 0),
`Monday_00:30` = c(0, 0, 0), `Monday_00:45` = c(0, 0, 0),
`Monday_01:00` = c(0, 0, 0), `Monday_01:15` = c(0, 0, 0),
`Monday_01:30` = c(0, 0, 0), `Monday_01:45` = c(0, 0, 0),
`Monday_02:00` = c(0, 0, 0), `Monday_02:15` = c(0, 0, 0),
`Monday_02:30` = c(0, 0, 0), `Monday_02:45` = c(0, 0, 0),
`Monday_03:00` = c(0, 0, 0), `Monday_03:15` = c(0, 0, 0),
`Monday_03:30` = c(0, 0, 0), `Monday_03:45` = c(0, 0, 0),
`Tuesday_04:00` = c(0, 0, 0), `Tuesday_04:15` = c(0, 0, 0
), `Tuesday_04:30` = c(0, 0, 0), `Tuesday_04:45` = c(0, 0,
0), `Tuesday_05:00` = c(0, 0, 0), `Tuesday_05:15` = c(0,
0, 0), `Tuesday_05:30` = c(0, 0, 0), `Tuesday_05:45` = c(0,
0, 0), `Tuesday_06:00` = c(0, 0, 0), `Tuesday_06:15` = c(0,
0, 0), `Tuesday_06:30` = c(0, 0, 0), `Tuesday_06:45` = c(0,
0, 0), `Tuesday_07:00` = c(0, 0, 0), `Tuesday_07:15` = c(0,
0, 0), `Tuesday_07:30` = c(0, 0, 0), `Tuesday_07:45` = c(0,
0, 0), `Tuesday_08:00` = c(2, 0, 0), `Tuesday_08:15` = c(2,
0, 0), `Tuesday_08:30` = c(2, 0, 2), `Tuesday_08:45` = c(2,
0, 2), `Tuesday_09:00` = c(2, 0, 2), `Tuesday_09:15` = c(2,
0, 2), `Tuesday_09:30` = c(2, 0, 2), `Tuesday_09:45` = c(2,
0, 2), `Tuesday_10:00` = c(2, 0, 2), `Tuesday_10:15` = c(2,
0, 2), `Tuesday_10:30` = c(2, 0, 2), `Tuesday_10:45` = c(2,
0, 2), `Tuesday_11:00` = c(2, 0, 2), `Tuesday_11:15` = c(2,
0, 2), `Tuesday_11:30` = c(2, 0, 2), `Tuesday_11:45` = c(2,
0, 2), `Tuesday_12:00` = c(0, 0, 2), `Tuesday_12:15` = c(0,
0, 2), `Tuesday_12:30` = c(0, 0, 2), `Tuesday_12:45` = c(0,
0, 2), `Tuesday_13:00` = c(2, 0, 2), `Tuesday_13:15` = c(2,
0, 2), `Tuesday_13:30` = c(2, 0, 2), `Tuesday_13:45` = c(2,
0, 2), `Tuesday_14:00` = c(2, 0, 2), `Tuesday_14:15` = c(2,
0, 2), `Tuesday_14:30` = c(2, 0, 2), `Tuesday_14:45` = c(2,
0, 2), `Tuesday_15:00` = c(2, 0, 2), `Tuesday_15:15` = c(2,
0, 2), `Tuesday_15:30` = c(2, 0, 2), `Tuesday_15:45` = c(2,
0, 0), `Tuesday_16:00` = c(2, 0, 0), `Tuesday_16:15` = c(2,
0, 0), `Tuesday_16:30` = c(2, 0, 0), `Tuesday_16:45` = c(2,
0, 0), `Tuesday_17:00` = c(2, 0, 0), `Tuesday_17:15` = c(2,
0, 0), `Tuesday_17:30` = c(2, 0, 0), `Tuesday_17:45` = c(2,
0, 0), `Tuesday_18:00` = c(2, 0, 0), `Tuesday_18:15` = c(2,
0, 0), `Tuesday_18:30` = c(0, 0, 0), `Tuesday_18:45` = c(0,
0, 0), `Tuesday_19:00` = c(0, 0, 0), `Tuesday_19:15` = c(0,
0, 0), `Tuesday_19:30` = c(0, 0, 0), `Tuesday_19:45` = c(0,
0, 0), `Tuesday_20:00` = c(0, 0, 0), `Tuesday_20:15` = c(0,
0, 0), `Tuesday_20:30` = c(0, 0, 0), `Tuesday_20:45` = c(0,
0, 0), `Tuesday_21:00` = c(0, 0, 0), `Tuesday_21:15` = c(0,
0, 0), `Tuesday_21:30` = c(0, 0, 0), `Tuesday_21:45` = c(0,
0, 0), `Tuesday_22:00` = c(0, 0, 0), `Tuesday_22:15` = c(0,
0, 0), `Tuesday_22:30` = c(0, 0, 0), `Tuesday_22:45` = c(0,
0, 0), `Tuesday_23:00` = c(0, 0, 0), `Tuesday_23:15` = c(0,
0, 0), `Tuesday_23:30` = c(0, 0, 0), `Tuesday_23:45` = c(0,
0, 0), `Tuesday_00:00` = c(0, 0, 0), `Tuesday_00:15` = c(0,
0, 0), `Tuesday_00:30` = c(0, 0, 0), `Tuesday_00:45` = c(0,
0, 0), `Tuesday_01:00` = c(0, 0, 0), `Tuesday_01:15` = c(0,
0, 0), `Tuesday_01:30` = c(0, 0, 0), `Tuesday_01:45` = c(0,
0, 0), `Tuesday_02:00` = c(0, 0, 0), `Tuesday_02:15` = c(0,
0, 0), `Tuesday_02:30` = c(0, 0, 0), `Tuesday_02:45` = c(0,
0, 0), `Tuesday_03:00` = c(0, 0, 0), `Tuesday_03:15` = c(0,
0, 0), `Tuesday_03:30` = c(0, 0, 0), `Tuesday_03:45` = c(0,
0, 0), `Wednesday_04:00` = c(0, 0, 0), `Wednesday_04:15` = c(0,
0, 0), `Wednesday_04:30` = c(0, 0, 0), `Wednesday_04:45` = c(0,
0, 0), `Wednesday_05:00` = c(0, 0, 0), `Wednesday_05:15` = c(0,
0, 0), `Wednesday_05:30` = c(0, 0, 0), `Wednesday_05:45` = c(0,
0, 0), `Wednesday_06:00` = c(0, 0, 0), `Wednesday_06:15` = c(0,
0, 0), `Wednesday_06:30` = c(0, 0, 0), `Wednesday_06:45` = c(0,
0, 0), `Wednesday_07:00` = c(0, 0, 0), `Wednesday_07:15` = c(0,
0, 0), `Wednesday_07:30` = c(0, 0, 0), `Wednesday_07:45` = c(0,
0, 0), `Wednesday_08:00` = c(0, 0, 2), `Wednesday_08:15` = c(0,
0, 2), `Wednesday_08:30` = c(0, 0, 2), `Wednesday_08:45` = c(0,
0, 2), `Wednesday_09:00` = c(0, 0, 2), `Wednesday_09:15` = c(0,
0, 2), `Wednesday_09:30` = c(0, 0, 2), `Wednesday_09:45` = c(0,
0, 2), `Wednesday_10:00` = c(0, 0, 2), `Wednesday_10:15` = c(0,
0, 2), `Wednesday_10:30` = c(0, 0, 2), `Wednesday_10:45` = c(0,
0, 2), `Wednesday_11:00` = c(0, 0, 2), `Wednesday_11:15` = c(0,
0, 2), `Wednesday_11:30` = c(0, 0, 2), `Wednesday_11:45` = c(0,
0, 2), `Wednesday_12:00` = c(0, 0, 2), `Wednesday_12:15` = c(0,
0, 2), `Wednesday_12:30` = c(0, 0, 2), `Wednesday_12:45` = c(0,
0, 2), `Wednesday_13:00` = c(0, 0, 2), `Wednesday_13:15` = c(0,
0, 2), `Wednesday_13:30` = c(0, 0, 2), `Wednesday_13:45` = c(0,
0, 2), `Wednesday_14:00` = c(0, 0, 2), `Wednesday_14:15` = c(0,
0, 2), `Wednesday_14:30` = c(0, 0, 2), `Wednesday_14:45` = c(0,
0, 2), `Wednesday_15:00` = c(0, 0, 2), `Wednesday_15:15` = c(0,
0, 2), `Wednesday_15:30` = c(0, 0, 2), `Wednesday_15:45` = c(0,
0, 2), `Wednesday_16:00` = c(0, 0, 2), `Wednesday_16:15` = c(0,
0, 2), `Wednesday_16:30` = c(0, 0, 2), `Wednesday_16:45` = c(0,
0, 2), `Wednesday_17:00` = c(0, 0, 2), `Wednesday_17:15` = c(0,
0, 2), `Wednesday_17:30` = c(0, 0, 0), `Wednesday_17:45` = c(0,
0, 0), `Wednesday_18:00` = c(0, 0, 0), `Wednesday_18:15` = c(0,
0, 0), `Wednesday_18:30` = c(0, 0, 0), `Wednesday_18:45` = c(0,
0, 0), `Wednesday_19:00` = c(0, 0, 0), `Wednesday_19:15` = c(0,
0, 0), `Wednesday_19:30` = c(0, 0, 0), `Wednesday_19:45` = c(0,
0, 0), `Wednesday_20:00` = c(0, 0, 0), `Wednesday_20:15` = c(0,
0, 0), `Wednesday_20:30` = c(0, 0, 0), `Wednesday_20:45` = c(0,
0, 0), `Wednesday_21:00` = c(0, 0, 0), `Wednesday_21:15` = c(0,
0, 0), `Wednesday_21:30` = c(0, 0, 0), `Wednesday_21:45` = c(0,
0, 0), `Wednesday_22:00` = c(0, 0, 0), `Wednesday_22:15` = c(0,
0, 0), `Wednesday_22:30` = c(0, 0, 0), `Wednesday_22:45` = c(0,
0, 0), `Wednesday_23:00` = c(0, 0, 0), `Wednesday_23:15` = c(0,
0, 0), `Wednesday_23:30` = c(0, 0, 0), `Wednesday_23:45` = c(0,
0, 0), `Wednesday_00:00` = c(0, 0, 0), `Wednesday_00:15` = c(0,
0, 0), `Wednesday_00:30` = c(0, 0, 0), `Wednesday_00:45` = c(0,
0, 0), `Wednesday_01:00` = c(0, 0, 0), `Wednesday_01:15` = c(0,
0, 0), `Wednesday_01:30` = c(0, 0, 0), `Wednesday_01:45` = c(0,
0, 0), `Wednesday_02:00` = c(0, 0, 0), `Wednesday_02:15` = c(0,
0, 0), `Wednesday_02:30` = c(0, 0, 0), `Wednesday_02:45` = c(0,
0, 0), `Wednesday_03:00` = c(0, 0, 0), `Wednesday_03:15` = c(0,
0, 0), `Wednesday_03:30` = c(0, 0, 0), `Wednesday_03:45` = c(0,
0, 0), `Thursday_04:00` = c(0, 0, 0), `Thursday_04:15` = c(0,
0, 0), `Thursday_04:30` = c(0, 0, 0), `Thursday_04:45` = c(0,
0, 0), `Thursday_05:00` = c(0, 0, 0), `Thursday_05:15` = c(0,
0, 0), `Thursday_05:30` = c(0, 0, 0), `Thursday_05:45` = c(0,
0, 0), `Thursday_06:00` = c(0, 0, 0), `Thursday_06:15` = c(0,
0, 0), `Thursday_06:30` = c(0, 0, 0), `Thursday_06:45` = c(0,
0, 0), `Thursday_07:00` = c(0, 0, 0), `Thursday_07:15` = c(0,
0, 0), `Thursday_07:30` = c(0, 0, 0), `Thursday_07:45` = c(0,
0, 0), `Thursday_08:00` = c(0, 0, 0), `Thursday_08:15` = c(0,
0, 0), `Thursday_08:30` = c(0, 0, 2), `Thursday_08:45` = c(0,
0, 2), `Thursday_09:00` = c(0, 0, 2), `Thursday_09:15` = c(0,
0, 2), `Thursday_09:30` = c(0, 0, 2), `Thursday_09:45` = c(0,
0, 2), `Thursday_10:00` = c(0, 0, 2), `Thursday_10:15` = c(0,
0, 2), `Thursday_10:30` = c(0, 0, 2), `Thursday_10:45` = c(0,
0, 2), `Thursday_11:00` = c(0, 0, 2), `Thursday_11:15` = c(0,
0, 2), `Thursday_11:30` = c(0, 0, 2), `Thursday_11:45` = c(0,
0, 2), `Thursday_12:00` = c(0, 0, 2), `Thursday_12:15` = c(0,
0, 2), `Thursday_12:30` = c(0, 0, 2), `Thursday_12:45` = c(0,
0, 2), `Thursday_13:00` = c(0, 0, 2), `Thursday_13:15` = c(0,
0, 2), `Thursday_13:30` = c(0, 0, 2), `Thursday_13:45` = c(0,
0, 2), `Thursday_14:00` = c(0, 0, 2), `Thursday_14:15` = c(0,
0, 2), `Thursday_14:30` = c(0, 0, 2), `Thursday_14:45` = c(0,
0, 2), `Thursday_15:00` = c(0, 0, 2), `Thursday_15:15` = c(0,
0, 2), `Thursday_15:30` = c(0, 0, 2), `Thursday_15:45` = c(0,
0, 2), `Thursday_16:00` = c(0, 0, 2), `Thursday_16:15` = c(0,
0, 2), `Thursday_16:30` = c(0, 0, 2), `Thursday_16:45` = c(0,
0, 2), `Thursday_17:00` = c(0, 0, 0), `Thursday_17:15` = c(0,
0, 0), `Thursday_17:30` = c(0, 0, 0), `Thursday_17:45` = c(0,
0, 0), `Thursday_18:00` = c(0, 0, 0), `Thursday_18:15` = c(0,
0, 0), `Thursday_18:30` = c(0, 0, 0), `Thursday_18:45` = c(0,
0, 0), `Thursday_19:00` = c(0, 0, 0), `Thursday_19:15` = c(0,
0, 0), `Thursday_19:30` = c(0, 0, 0), `Thursday_19:45` = c(0,
0, 0), `Thursday_20:00` = c(0, 0, 0), `Thursday_20:15` = c(0,
0, 0), `Thursday_20:30` = c(0, 0, 0), `Thursday_20:45` = c(0,
0, 0), `Thursday_21:00` = c(0, 0, 0), `Thursday_21:15` = c(0,
0, 0), `Thursday_21:30` = c(0, 0, 0), `Thursday_21:45` = c(0,
0, 0), `Thursday_22:00` = c(0, 0, 0), `Thursday_22:15` = c(0,
0, 0), `Thursday_22:30` = c(0, 0, 0), `Thursday_22:45` = c(0,
0, 0), `Thursday_23:00` = c(0, 0, 0), `Thursday_23:15` = c(0,
0, 0), `Thursday_23:30` = c(0, 0, 0), `Thursday_23:45` = c(0,
0, 0), `Thursday_00:00` = c(0, 0, 0), `Thursday_00:15` = c(0,
0, 0), `Thursday_00:30` = c(0, 0, 0), `Thursday_00:45` = c(0,
0, 0), `Thursday_01:00` = c(0, 0, 0), `Thursday_01:15` = c(0,
0, 0), `Thursday_01:30` = c(0, 0, 0), `Thursday_01:45` = c(0,
0, 0), `Thursday_02:00` = c(0, 0, 0), `Thursday_02:15` = c(0,
0, 0), `Thursday_02:30` = c(0, 0, 0), `Thursday_02:45` = c(0,
0, 0), `Thursday_03:00` = c(0, 0, 0), `Thursday_03:15` = c(0,
0, 0), `Thursday_03:30` = c(0, 0, 0), `Thursday_03:45` = c(0,
0, 0), `Friday_04:00` = c(0, 0, 0), `Friday_04:15` = c(0,
0, 0), `Friday_04:30` = c(0, 0, 0), `Friday_04:45` = c(0,
0, 0), `Friday_05:00` = c(0, 0, 0), `Friday_05:15` = c(0,
0, 0), `Friday_05:30` = c(0, 0, 0), `Friday_05:45` = c(0,
0, 0), `Friday_06:00` = c(0, 0, 0), `Friday_06:15` = c(0,
0, 0), `Friday_06:30` = c(0, 0, 0), `Friday_06:45` = c(0,
0, 0), `Friday_07:00` = c(0, 0, 0), `Friday_07:15` = c(0,
0, 0), `Friday_07:30` = c(0, 0, 0), `Friday_07:45` = c(0,
0, 0), `Friday_08:00` = c(0, 0, 0), `Friday_08:15` = c(2,
0, 0), `Friday_08:30` = c(2, 0, 2), `Friday_08:45` = c(2,
0, 2), `Friday_09:00` = c(2, 0, 2), `Friday_09:15` = c(2,
0, 2), `Friday_09:30` = c(2, 0, 2), `Friday_09:45` = c(2,
0, 2), `Friday_10:00` = c(2, 0, 2), `Friday_10:15` = c(2,
0, 2), `Friday_10:30` = c(2, 0, 2), `Friday_10:45` = c(2,
0, 2), `Friday_11:00` = c(2, 0, 2), `Friday_11:15` = c(2,
0, 2), `Friday_11:30` = c(2, 0, 0), `Friday_11:45` = c(2,
0, 0), `Friday_12:00` = c(0, 0, 0), `Friday_12:15` = c(0,
0, 0), `Friday_12:30` = c(0, 0, 0), `Friday_12:45` = c(0,
0, 0), `Friday_13:00` = c(0, 0, 0), `Friday_13:15` = c(2,
0, 0), `Friday_13:30` = c(2, 0, 0), `Friday_13:45` = c(2,
0, 0), `Friday_14:00` = c(2, 0, 0), `Friday_14:15` = c(2,
0, 0), `Friday_14:30` = c(2, 2, 0), `Friday_14:45` = c(2,
2, 0), `Friday_15:00` = c(2, 2, 0), `Friday_15:15` = c(2,
2, 0), `Friday_15:30` = c(2, 2, 0), `Friday_15:45` = c(2,
2, 0), `Friday_16:00` = c(2, 2, 0), `Friday_16:15` = c(2,
2, 0), `Friday_16:30` = c(2, 2, 0), `Friday_16:45` = c(2,
2, 0), `Friday_17:00` = c(2, 2, 0), `Friday_17:15` = c(2,
2, 0), `Friday_17:30` = c(2, 2, 0), `Friday_17:45` = c(2,
2, 0), `Friday_18:00` = c(2, 2, 0), `Friday_18:15` = c(0,
2, 0), `Friday_18:30` = c(0, 2, 0), `Friday_18:45` = c(0,
2, 0), `Friday_19:00` = c(0, 2, 0), `Friday_19:15` = c(0,
2, 0), `Friday_19:30` = c(0, 0, 0), `Friday_19:45` = c(0,
0, 0), `Friday_20:00` = c(0, 0, 0), `Friday_20:15` = c(0,
0, 0), `Friday_20:30` = c(0, 0, 0), `Friday_20:45` = c(0,
0, 0), `Friday_21:00` = c(0, 0, 0), `Friday_21:15` = c(0,
0, 0), `Friday_21:30` = c(0, 0, 0), `Friday_21:45` = c(0,
0, 0), `Friday_22:00` = c(0, 0, 0), `Friday_22:15` = c(0,
0, 0), `Friday_22:30` = c(0, 0, 0), `Friday_22:45` = c(0,
0, 0), `Friday_23:00` = c(0, 0, 0), `Friday_23:15` = c(0,
0, 0), `Friday_23:30` = c(0, 0, 0), `Friday_23:45` = c(0,
0, 0), `Friday_00:00` = c(0, 0, 0), `Friday_00:15` = c(0,
0, 0), `Friday_00:30` = c(0, 0, 0), `Friday_00:45` = c(0,
0, 0), `Friday_01:00` = c(0, 0, 0), `Friday_01:15` = c(0,
0, 0), `Friday_01:30` = c(0, 0, 0), `Friday_01:45` = c(0,
0, 0), `Friday_02:00` = c(0, 0, 0), `Friday_02:15` = c(0,
0, 0), `Friday_02:30` = c(0, 0, 0), `Friday_02:45` = c(0,
0, 0), `Friday_03:00` = c(0, 0, 0), `Friday_03:15` = c(0,
0, 0), `Friday_03:30` = c(0, 0, 0), `Friday_03:45` = c(0,
0, 0), `Saturday_04:00` = c(0, 0, 0), `Saturday_04:15` = c(0,
0, 0), `Saturday_04:30` = c(0, 0, 0), `Saturday_04:45` = c(0,
0, 0), `Saturday_05:00` = c(0, 0, 0), `Saturday_05:15` = c(0,
0, 0), `Saturday_05:30` = c(0, 0, 0), `Saturday_05:45` = c(0,
0, 0), `Saturday_06:00` = c(0, 0, 0), `Saturday_06:15` = c(0,
0, 0), `Saturday_06:30` = c(0, 0, 0), `Saturday_06:45` = c(0,
0, 0), `Saturday_07:00` = c(0, 0, 0), `Saturday_07:15` = c(0,
0, 0), `Saturday_07:30` = c(0, 0, 0), `Saturday_07:45` = c(0,
0, 0), `Saturday_08:00` = c(0, 0, 0), `Saturday_08:15` = c(2,
0, 0), `Saturday_08:30` = c(2, 0, 0), `Saturday_08:45` = c(2,
0, 0), `Saturday_09:00` = c(2, 0, 0), `Saturday_09:15` = c(2,
0, 0), `Saturday_09:30` = c(2, 0, 0), `Saturday_09:45` = c(2,
0, 0), `Saturday_10:00` = c(2, 0, 0), `Saturday_10:15` = c(2,
0, 0), `Saturday_10:30` = c(2, 0, 0), `Saturday_10:45` = c(2,
0, 0), `Saturday_11:00` = c(2, 0, 0), `Saturday_11:15` = c(2,
0, 0), `Saturday_11:30` = c(2, 0, 0), `Saturday_11:45` = c(2,
0, 0), `Saturday_12:00` = c(0, 0, 0), `Saturday_12:15` = c(0,
0, 0), `Saturday_12:30` = c(0, 0, 0), `Saturday_12:45` = c(0,
0, 0), `Saturday_13:00` = c(2, 0, 0), `Saturday_13:15` = c(2,
0, 0), `Saturday_13:30` = c(2, 0, 0), `Saturday_13:45` = c(2,
0, 0), `Saturday_14:00` = c(2, 0, 0), `Saturday_14:15` = c(2,
0, 0), `Saturday_14:30` = c(2, 0, 0), `Saturday_14:45` = c(2,
0, 0), `Saturday_15:00` = c(2, 0, 0), `Saturday_15:15` = c(2,
0, 0), `Saturday_15:30` = c(2, 0, 0), `Saturday_15:45` = c(2,
0, 0), `Saturday_16:00` = c(2, 0, 0), `Saturday_16:15` = c(2,
0, 0), `Saturday_16:30` = c(2, 0, 0), `Saturday_16:45` = c(2,
0, 0), `Saturday_17:00` = c(2, 0, 0), `Saturday_17:15` = c(2,
0, 0), `Saturday_17:30` = c(2, 0, 0), `Saturday_17:45` = c(2,
0, 0), `Saturday_18:00` = c(2, 0, 0), `Saturday_18:15` = c(0,
0, 0), `Saturday_18:30` = c(0, 0, 0), `Saturday_18:45` = c(0,
0, 0), `Saturday_19:00` = c(0, 0, 0), `Saturday_19:15` = c(0,
0, 0), `Saturday_19:30` = c(0, 0, 0), `Saturday_19:45` = c(0,
0, 0), `Saturday_20:00` = c(0, 0, 0), `Saturday_20:15` = c(0,
0, 0), `Saturday_20:30` = c(0, 0, 0), `Saturday_20:45` = c(0,
0, 0), `Saturday_21:00` = c(0, 0, 0), `Saturday_21:15` = c(0,
0, 0), `Saturday_21:30` = c(0, 0, 0), `Saturday_21:45` = c(0,
0, 0), `Saturday_22:00` = c(0, 0, 0), `Saturday_22:15` = c(0,
0, 0), `Saturday_22:30` = c(0, 0, 0), `Saturday_22:45` = c(0,
0, 0), `Saturday_23:00` = c(0, 0, 0), `Saturday_23:15` = c(0,
0, 0), `Saturday_23:30` = c(0, 0, 0), `Saturday_23:45` = c(0,
0, 0), `Saturday_00:00` = c(0, 0, 0), `Saturday_00:15` = c(0,
0, 0), `Saturday_00:30` = c(0, 0, 0), `Saturday_00:45` = c(0,
0, 0), `Saturday_01:00` = c(0, 0, 0), `Saturday_01:15` = c(0,
0, 0), `Saturday_01:30` = c(0, 0, 0), `Saturday_01:45` = c(0,
0, 0), `Saturday_02:00` = c(0, 0, 0), `Saturday_02:15` = c(0,
0, 0), `Saturday_02:30` = c(0, 0, 0), `Saturday_02:45` = c(0,
0, 0), `Saturday_03:00` = c(0, 0, 0), `Saturday_03:15` = c(0,
0, 0), `Saturday_03:30` = c(0, 0, 0), `Saturday_03:45` = c(0,
0, 0), `Sunday_04:00` = c(0, 0, 0), `Sunday_04:15` = c(0,
0, 0), `Sunday_04:30` = c(0, 0, 0), `Sunday_04:45` = c(0,
0, 0), `Sunday_05:00` = c(0, 0, 0), `Sunday_05:15` = c(0,
0, 0), `Sunday_05:30` = c(0, 0, 0), `Sunday_05:45` = c(0,
0, 0), `Sunday_06:00` = c(0, 0, 0), `Sunday_06:15` = c(0,
0, 0), `Sunday_06:30` = c(0, 0, 0), `Sunday_06:45` = c(0,
0, 0), `Sunday_07:00` = c(0, 0, 0), `Sunday_07:15` = c(0,
0, 0), `Sunday_07:30` = c(0, 0, 0), `Sunday_07:45` = c(0,
0, 0), `Sunday_08:00` = c(2, 0, 0), `Sunday_08:15` = c(2,
0, 0), `Sunday_08:30` = c(2, 0, 0), `Sunday_08:45` = c(2,
0, 0), `Sunday_09:00` = c(2, 0, 0), `Sunday_09:15` = c(2,
0, 0), `Sunday_09:30` = c(2, 0, 0), `Sunday_09:45` = c(2,
0, 0), `Sunday_10:00` = c(2, 0, 0), `Sunday_10:15` = c(2,
0, 0), `Sunday_10:30` = c(2, 0, 0), `Sunday_10:45` = c(2,
0, 0), `Sunday_11:00` = c(2, 0, 0), `Sunday_11:15` = c(2,
0, 0), `Sunday_11:30` = c(2, 0, 0), `Sunday_11:45` = c(2,
0, 0), `Sunday_12:00` = c(2, 0, 0), `Sunday_12:15` = c(2,
0, 0), `Sunday_12:30` = c(2, 0, 0), `Sunday_12:45` = c(2,
0, 0), `Sunday_13:00` = c(0, 0, 0), `Sunday_13:15` = c(0,
0, 0), `Sunday_13:30` = c(0, 0, 0), `Sunday_13:45` = c(0,
0, 0), `Sunday_14:00` = c(2, 0, 0), `Sunday_14:15` = c(2,
0, 0), `Sunday_14:30` = c(2, 0, 0), `Sunday_14:45` = c(2,
0, 0), `Sunday_15:00` = c(2, 0, 0), `Sunday_15:15` = c(2,
0, 0), `Sunday_15:30` = c(2, 0, 0), `Sunday_15:45` = c(2,
0, 0), `Sunday_16:00` = c(2, 0, 0), `Sunday_16:15` = c(2,
0, 0), `Sunday_16:30` = c(2, 0, 0), `Sunday_16:45` = c(2,
0, 0), `Sunday_17:00` = c(2, 0, 0), `Sunday_17:15` = c(2,
0, 0), `Sunday_17:30` = c(0, 0, 0), `Sunday_17:45` = c(0,
0, 0), `Sunday_18:00` = c(0, 0, 0), `Sunday_18:15` = c(0,
0, 0), `Sunday_18:30` = c(0, 0, 0), `Sunday_18:45` = c(0,
0, 0), `Sunday_19:00` = c(0, 0, 0), `Sunday_19:15` = c(0,
0, 0), `Sunday_19:30` = c(0, 0, 0), `Sunday_19:45` = c(0,
0, 0), `Sunday_20:00` = c(0, 0, 0), `Sunday_20:15` = c(0,
0, 0), `Sunday_20:30` = c(0, 0, 0), `Sunday_20:45` = c(0,
0, 0), `Sunday_21:00` = c(0, 0, 0), `Sunday_21:15` = c(0,
0, 0), `Sunday_21:30` = c(0, 0, 0), `Sunday_21:45` = c(0,
0, 0), `Sunday_22:00` = c(0, 0, 0), `Sunday_22:15` = c(0,
0, 0), `Sunday_22:30` = c(0, 0, 0), `Sunday_22:45` = c(0,
0, 0), `Sunday_23:00` = c(0, 0, 0), `Sunday_23:15` = c(0,
0, 0), `Sunday_23:30` = c(0, 0, 0), `Sunday_23:45` = c(0,
0, 0), `Sunday_00:00` = c(0, 0, 0), `Sunday_00:15` = c(0,
0, 0), `Sunday_00:30` = c(0, 0, 0), `Sunday_00:45` = c(0,
0, 0), `Sunday_01:00` = c(0, 0, 0), `Sunday_01:15` = c(0,
0, 0), `Sunday_01:30` = c(0, 0, 0), `Sunday_01:45` = c(0,
0, 0), `Sunday_02:00` = c(0, 0, 0), `Sunday_02:15` = c(0,
0, 0), `Sunday_02:30` = c(0, 0, 0), `Sunday_02:45` = c(0,
0, 0), `Sunday_03:00` = c(0, 0, 0), `Sunday_03:15` = c(0,
0, 0), `Sunday_03:30` = c(0, 0, 0), `Sunday_03:45` = c(0,
0, 0), index = 1:3), row.names = c(NA, 3L), class = "data.frame")
>
The first row (rn)contains numbers from 2 to 3523. The other 672 columns are time steps that record people works chedules: 0 - not worked and 2 - worked
I am trying to calculate: a.) daily workhours per id (sum of 95 columns/day).
b.) weekly workhours per id
Any suggestion is welcomed
I would use tidyr and dplyr to go from wide to long format and then group_by and summarize.
Try this:
library(tidyverse)
df %>%
gather(day_time, worked, -index) %>%
separate(day_time, c("day", "time"), "_") %>%
group_by(index, day) %>%
summarize(tot_work = sum(worked)/2*15/60)
#> # A tibble: 21 x 3
#> # Groups: index [?]
#> index day tot_work
#> <int> <chr> <dbl>
#> 1 1 Friday 8.75
#> 2 1 Monday 9.5
#> 3 1 Saturday 9
#> 4 1 Sunday 8.5
#> 5 1 Thursday 0
#> 6 1 Tuesday 9.5
#> 7 1 Wednesday 0
#> 8 2 Friday 5
#> 9 2 Monday 0
#> 10 2 Saturday 0
#> # … with 11 more rows
df %>%
gather(day_time, worked, -index) %>%
separate(day_time, c("day", "time"), "_") %>%
group_by(index) %>%
summarize(tot_work = sum(worked)/2 * 15 / 60) # divided by 2 * 15 min and divided by 60 min to have everything in hours
#> # A tibble: 3 x 2
#> index tot_work
#> <int> <dbl>
#> 1 1 45.2
#> 2 2 5
#> 3 3 28.2
If you want everything in minutes do not divide by 60.
Consider reshaping your data from the current wide format to long format. This is the ideal structure for most data analytics including aggregation and a storage efficient format as columns necessitate more meta data (i.e., tracking types) than rows.
# RESHAPE WIDE TO LONG
rdf <- reshape(df, idvar="index", varying=list(head(names(df),-1)), v.names="hours",
times = head(names(df),-1), timevar = "day_time",
new.row.names = 1:1E5, direction = "long")
# SPLIT DAY AND TIME
rdf[c("day", "time")] <- t(sapply(strsplit(rdf$day_time, split="_"), "["))
rdf$day_time <- NULL
head(rdf, 10)
# index hours day time
# 1 1 0 Monday 04:00
# 2 2 0 Monday 04:00
# 3 3 0 Monday 04:00
# 4 1 0 Monday 04:15
# 5 2 0 Monday 04:15
# 6 3 0 Monday 04:15
# 7 1 0 Monday 04:30
# 8 2 0 Monday 04:30
# 9 3 0 Monday 04:30
# 10 1 0 Monday 04:45
# SUMMARIZE HOURS BY INDEX
agg_df <- aggregate(hours ~ index, rdf, sum)
agg_df
# index hours
# 1 1 362
# 2 2 40
# 3 3 226
By weekday:
rdf$day <- factor(rdf$day, levels=c("Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday"))
aggregate(hours ~ index + day, rdf, sum)
# day index hours
# 1 Monday 1 76
# 2 Tuesday 1 76
# 3 Wednesday 1 0
# 4 Thursday 1 0
# 5 Friday 1 70
# 6 Saturday 1 72
# 7 Sunday 1 68
# 8 Monday 2 0
# 9 Tuesday 2 0
# 10 Wednesday 2 0
# 11 Thursday 2 0
# 12 Friday 2 40
# 13 Saturday 2 0
# 14 Sunday 2 0
# 15 Monday 3 0
# 16 Tuesday 3 58
# 17 Wednesday 3 76
# 18 Thursday 3 68
# 19 Friday 3 24
# 20 Saturday 3 0
# 21 Sunday 3 0

Calculate euclidean distance between profiles stored in data frame. Using one row as a reference

I have a data frame like that one below:
> dput(data)
structure(list(`28` = c(0, 0, 0, 0, 0, 0), `38` = c(0, 0, 0,
0, 0, 0), `45` = c(0, 0, 0, 0, 0, 0), `53` = c(0, 0, 0, 0, 0,
0), `60` = c(0, 0, 0, 0, 0, 0), `78` = c(0, 0, 0, 0, 0, 0), `116` = c(0,
0, 0, 0, 0, 0.983309489747258), `145` = c(0, 0, 0, 0, 0, 1),
`189` = c(0, 1, 0.560384508734634, 0, 0, 0.875695437927198
), `223` = c(0, 0.988158197286733, 1, 0, 0, 0.492500108379937
), `281` = c(1, 0.677856978615774, 0.448525741750624, 0,
0.362088745790311, 0.180474270603026), `362` = c(0.79151704397606,
0.763278914693033, 0.35864682503004, 1, 1, 0.114178985852806
), `440` = c(0.662841530054645, 0.818636468153598, 0.448488769756909,
0, 0.448447503793346, 0), `524` = c(0, 0.638192687974247,
0, 0, 0, 0), `634` = c(0, 0, 0, 0, 0, 0), `759` = c(0, 0,
0, 0, 0, 0), `848` = c(0, 0, 0, 0, 0, 0), `979` = c(0, 0,
0, 0, 0, 0), `1120` = c(0, 0, 0, 0, 0, 0), `1248` = c(0,
0, 0, 0, 0, 0)), .Names = c("28", "38", "45", "53", "60",
"78", "116", "145", "189", "223", "281", "362", "440", "524",
"634", "759", "848", "979", "1120", "1248"), row.names = c("Mark",
"Gregg", "Tim", "Oscar", "Tom", "Matthew"
), class = "data.frame")
I would like to calculate euclidean distance between all the profiles from this data and Tim should be used as a reference. The results can be stored in additional column.
Mark to Tim
Gregg to Tim
Oscar to Tim
and etc
You can use dist function (which actually computes all the distances between all the profiles) :
m <- as.matrix(DF)
distances <- as.matrix(dist(m, method = "euclidean", upper = TRUE,diag = TRUE))
> distances['Mark','Tim']
[1] 1.36069
> distances['Gregg','Tim']
[1] 0.9767401
> distances['Oscar','Tim']
[1] 1.458658

Using aggregate on list in R

I have a list (lst3, subset below) and would like to do some calculations on it, e.g.:
lst4 <-lapply(lst3, function(x) aggregate(x[,5:ncol(x)], x[c(4)], FUN = mean)) #column means
lst5<-lapply(lst4,function(x) apply(x[,-c(1)],1,mean)) # get row mean
However, I am unable to get row mean without ignoring "Site".
I would like my final list to look like this:
lst5<-
[[1]]
Site x
G116 1.864233
[[2]]
Site x
GG16 2.064567
The essence is that the final list should have the above structure so that I can write my data to working directory using:
lapply(lst5,function(x)write.table(x,file=paste(getwd(),"summer",paste0(unique(x$Site),".csv"),
sep="/"),row.names=FALSE,quote=FALSE)) ### create a folder called "summer" and write files to directory###
Thanks,
AZ.
list(structure(list(Year = c(2005L, 2005L, 2005L), Month = c(8L,
8L, 8L), Day = 29:31, Site = structure(c(1L, 1L, 1L), .Label = "G116", class = "factor"),
Sim001 = c(8.4, 17.72, 6.03), Sim002 = c(0.27, 0, 0), Sim003 = c(2.83,
0.14, 0.1), Sim004 = c(0, 0, 0), Sim005 = c(0, 0.77, 0.28
), Sim006 = c(0, 0, 0), Sim007 = c(0, 0, 0), Sim008 = c(10.94,
4.77, 0), Sim009 = c(0, 0, 0), Sim010 = c(3.43, 2.74, 0.65
), Sim011 = c(0.36, 0, 2.75), Sim012 = c(26.91, 0, 2.16),
Sim013 = c(0.88, 1.33, 0.87), Sim014 = c(0, 0.86, 9.42),
Sim015 = c(0, 0.17, 1.15), Sim016 = c(0, 0, 0), Sim017 = c(0.13,
0, 0), Sim018 = c(0, 0, 6.72), Sim019 = c(8.45, 12.99, 23.72
), Sim020 = c(1.76, 0, 0), Sim021 = c(0, 0, 2.34), Sim022 = c(0,
0, 0), Sim023 = c(1.2, 0, 0.26), Sim024 = c(0.85, 0, 0),
Sim025 = c(0, 0, 0), Sim026 = c(2.05, 0.76, 5.03), Sim027 = c(0.78,
0, 0), Sim028 = c(1.2, 0, 0), Sim029 = c(22, 0.19, 0), Sim030 = c(0.12,
0, 0), Sim031 = c(3.1, 13.67, 0), Sim032 = c(0, 0, 17.88),
Sim033 = c(0, 0, 0), Sim034 = c(1.11, 0, 0), Sim035 = c(1.17,
1.41, 23.35), Sim036 = c(0, 0.48, 1.71), Sim037 = c(1.51,
11.1, 7.98), Sim038 = c(0, 0, 0), Sim039 = c(0, 0, 5.46),
Sim040 = c(5.21, 0, 0), Sim041 = c(0.1, 0.11, 0), Sim042 = c(0,
0.15, 5.23), Sim043 = c(0, 0, 0), Sim044 = c(0, 0.1, 0),
Sim045 = c(0, 0, 0), Sim046 = c(0, 0, 0), Sim047 = c(0, 0,
0.11), Sim048 = c(0, 0, 0), Sim049 = c(0, 0, 4.05), Sim050 = c(0,
0, 0), Sim051 = c(0, 0.12, 0), Sim052 = c(0.24, 2.58, 0),
Sim053 = c(3.63, 0, 0.17), Sim054 = c(10.94, 2.69, 0), Sim055 = c(0,
0, 0), Sim056 = c(0.24, 0.44, 8.27), Sim057 = c(0, 0, 0),
Sim058 = c(0, 0, 3.75), Sim059 = c(0.19, 11.06, 0), Sim060 = c(0,
0, 1.65), Sim061 = c(0, 4.95, 0), Sim062 = c(0.15, 0, 4.73
), Sim063 = c(2.99, 0.12, 1.28), Sim064 = c(0, 0, 0), Sim065 = c(0,
0, 0), Sim066 = c(0, 0, 0), Sim067 = c(0.11, 0.62, 0.56),
Sim068 = c(2.84, 0, 0), Sim069 = c(0, 0, 0), Sim070 = c(17.91,
0.11, 4.78), Sim071 = c(0, 0, 1.68), Sim072 = c(0, 0, 1.38
), Sim073 = c(1.68, 0, 0), Sim074 = c(0.53, 0, 2.87), Sim075 = c(0,
0, 0), Sim076 = c(2.58, 0.27, 0.11), Sim077 = c(0, 0, 0),
Sim078 = c(9.07, 3.13, 8.62), Sim079 = c(0.98, 0, 2.38),
Sim080 = c(3.4, 0, 0), Sim081 = c(0, 0, 4.57), Sim082 = c(1.87,
2.86, 0), Sim083 = c(21.76, 2.24, 0), Sim084 = c(0.45, 4.03,
0.39), Sim085 = c(0, 0, 0), Sim086 = c(0, 0, 0), Sim087 = c(0,
0, 17.12), Sim088 = c(5.05, 0, 0), Sim089 = c(0, 0, 1.4),
Sim090 = c(0.1, 0, 0), Sim091 = c(1.96, 0, 1.38), Sim092 = c(0,
0, 0), Sim093 = c(0, 0, 0), Sim094 = c(0, 0, 1.81), Sim095 = c(2.72,
7.16, 1.7), Sim096 = c(6.37, 0, 0), Sim097 = c(0, 1.12, 25.7
), Sim098 = c(0, 0, 0), Sim099 = c(0, 0, 0), Sim100 = c(6.77,
10.87, 2.6)), .Names = c("Year", "Month", "Day", "Site",
"Sim001", "Sim002", "Sim003", "Sim004", "Sim005", "Sim006", "Sim007",
"Sim008", "Sim009", "Sim010", "Sim011", "Sim012", "Sim013", "Sim014",
"Sim015", "Sim016", "Sim017", "Sim018", "Sim019", "Sim020", "Sim021",
"Sim022", "Sim023", "Sim024", "Sim025", "Sim026", "Sim027", "Sim028",
"Sim029", "Sim030", "Sim031", "Sim032", "Sim033", "Sim034", "Sim035",
"Sim036", "Sim037", "Sim038", "Sim039", "Sim040", "Sim041", "Sim042",
"Sim043", "Sim044", "Sim045", "Sim046", "Sim047", "Sim048", "Sim049",
"Sim050", "Sim051", "Sim052", "Sim053", "Sim054", "Sim055", "Sim056",
"Sim057", "Sim058", "Sim059", "Sim060", "Sim061", "Sim062", "Sim063",
"Sim064", "Sim065", "Sim066", "Sim067", "Sim068", "Sim069", "Sim070",
"Sim071", "Sim072", "Sim073", "Sim074", "Sim075", "Sim076", "Sim077",
"Sim078", "Sim079", "Sim080", "Sim081", "Sim082", "Sim083", "Sim084",
"Sim085", "Sim086", "Sim087", "Sim088", "Sim089", "Sim090", "Sim091",
"Sim092", "Sim093", "Sim094", "Sim095", "Sim096", "Sim097", "Sim098",
"Sim099", "Sim100"), row.names = 15947:15949, class = "data.frame"),
structure(list(Year = c(2005L, 2005L, 2005L), Month = c(8L,
8L, 8L), Day = 29:31, Site = structure(c(1L, 1L, 1L), .Label = "GG16", class = "factor"),
Sim001 = c(18.36, 0.33, 0.14), Sim002 = c(0, 10.92, 0
), Sim003 = c(0, 0, 0), Sim004 = c(0, 0, 1.7), Sim005 = c(0,
0, 0), Sim006 = c(0.91, 4.24, 0), Sim007 = c(0, 0, 0.22
), Sim008 = c(0.63, 2.9, 2.24), Sim009 = c(0, 0, 0),
Sim010 = c(0, 0, 6.91), Sim011 = c(0, 3.28, 10.18), Sim012 = c(8.39,
14.58, 45.62), Sim013 = c(2.87, 0.53, 0.11), Sim014 = c(9.15,
21.1, 0.66), Sim015 = c(0, 1.75, 2.2), Sim016 = c(0,
7.86, 0), Sim017 = c(0, 0, 0), Sim018 = c(0, 0, 0), Sim019 = c(0,
0, 0), Sim020 = c(0.39, 0, 0), Sim021 = c(0.13, 0, 1.05
), Sim022 = c(0, 0, 10.91), Sim023 = c(0.23, 0, 0), Sim024 = c(0.12,
0.83, 5.35), Sim025 = c(0, 0, 0), Sim026 = c(7.75, 0,
4.82), Sim027 = c(20.04, 0, 0), Sim028 = c(12.41, 0,
5.3), Sim029 = c(0, 0, 0), Sim030 = c(0, 0, 0), Sim031 = c(0,
8.06, 0), Sim032 = c(0, 0, 0), Sim033 = c(0, 0, 0), Sim034 = c(0.1,
0, 3.34), Sim035 = c(0, 4.34, 3.53), Sim036 = c(2.89,
0.27, 0), Sim037 = c(0, 0, 0), Sim038 = c(0, 0, 0), Sim039 = c(0,
0.11, 0), Sim040 = c(9.83, 1.55, 9.09), Sim041 = c(3.6,
0, 0), Sim042 = c(0, 0, 1.37), Sim043 = c(0, 0, 0), Sim044 = c(0,
0, 0), Sim045 = c(0, 0, 0), Sim046 = c(0, 0, 0), Sim047 = c(0,
20.52, 0.65), Sim048 = c(1.77, 0.67, 0), Sim049 = c(0,
0, 0), Sim050 = c(0, 0, 0), Sim051 = c(0, 4.9, 0), Sim052 = c(0.71,
11.34, 0), Sim053 = c(3.46, 2.59, 1.5), Sim054 = c(0,
23.63, 0), Sim055 = c(0, 16.48, 4.99), Sim056 = c(0,
0, 0), Sim057 = c(0, 0, 0), Sim058 = c(0, 0, 0), Sim059 = c(0,
0, 0), Sim060 = c(16.87, 0, 0), Sim061 = c(0, 3.43, 0
), Sim062 = c(0.45, 0, 0), Sim063 = c(0, 11.14, 7.22),
Sim064 = c(0, 0, 0), Sim065 = c(0, 0, 0), Sim066 = c(0,
16.08, 1.87), Sim067 = c(0, 0, 0), Sim068 = c(5.16, 0.88,
0.1), Sim069 = c(0, 0, 3.91), Sim070 = c(0, 0, 0), Sim071 = c(0.17,
0, 5.22), Sim072 = c(0, 0, 6.95), Sim073 = c(0, 0, 0),
Sim074 = c(0.14, 0, 0), Sim075 = c(0, 0, 0), Sim076 = c(0,
9.62, 0), Sim077 = c(0, 0, 0), Sim078 = c(1.65, 0, 0),
Sim079 = c(0.23, 8.41, 0.28), Sim080 = c(0.78, 0, 0),
Sim081 = c(0, 0, 0), Sim082 = c(0.11, 2.75, 0), Sim083 = c(0.26,
7.34, 5.92), Sim084 = c(0, 0, 4.27), Sim085 = c(0, 0,
0), Sim086 = c(0, 0, 0.1), Sim087 = c(27.18, 0.72, 28.29
), Sim088 = c(0, 0, 4.2), Sim089 = c(0, 9.37, 6.59),
Sim090 = c(0.21, 2.57, 0), Sim091 = c(0.45, 0, 0), Sim092 = c(0,
4.97, 0), Sim093 = c(1.43, 0, 0), Sim094 = c(0, 0, 2.15
), Sim095 = c(6, 0, 1.63), Sim096 = c(7.21, 0, 0), Sim097 = c(0,
0.39, 1.92), Sim098 = c(0, 0, 0), Sim099 = c(4.38, 0,
0), Sim100 = c(0, 0, 0)), .Names = c("Year", "Month",
"Day", "Site", "Sim001", "Sim002", "Sim003", "Sim004", "Sim005",
"Sim006", "Sim007", "Sim008", "Sim009", "Sim010", "Sim011",
"Sim012", "Sim013", "Sim014", "Sim015", "Sim016", "Sim017",
"Sim018", "Sim019", "Sim020", "Sim021", "Sim022", "Sim023",
"Sim024", "Sim025", "Sim026", "Sim027", "Sim028", "Sim029",
"Sim030", "Sim031", "Sim032", "Sim033", "Sim034", "Sim035",
"Sim036", "Sim037", "Sim038", "Sim039", "Sim040", "Sim041",
"Sim042", "Sim043", "Sim044", "Sim045", "Sim046", "Sim047",
"Sim048", "Sim049", "Sim050", "Sim051", "Sim052", "Sim053",
"Sim054", "Sim055", "Sim056", "Sim057", "Sim058", "Sim059",
"Sim060", "Sim061", "Sim062", "Sim063", "Sim064", "Sim065",
"Sim066", "Sim067", "Sim068", "Sim069", "Sim070", "Sim071",
"Sim072", "Sim073", "Sim074", "Sim075", "Sim076", "Sim077",
"Sim078", "Sim079", "Sim080", "Sim081", "Sim082", "Sim083",
"Sim084", "Sim085", "Sim086", "Sim087", "Sim088", "Sim089",
"Sim090", "Sim091", "Sim092", "Sim093", "Sim094", "Sim095",
"Sim096", "Sim097", "Sim098", "Sim099", "Sim100"), row.names = 15947:15949, class = "data.frame"))
You can go from lst3 directly to lst5 without the intermediate aggregate step:
lapply(lst3, function(df){
data.frame(Site = df$Site[1], x = mean(unlist(df[-c(1:4)])))
})
#[[1]]
# Site x
#1 G116 1.864233
#
#[[2]]
# Site x
#1 GG16 2.064567
Since you're calculating the mean of all columns except the first 4 columns and over all the rows of the other columns, it's quite easy to unlist the data, creating a single vector, and then using standard mean on it. Also, by skipping the lst4 step, this most likely be noticeably faster.
Or, as commented by Richard, a variation could be:
lapply(lst3, function(df){
data.frame(Site = df$Site[1], x = mean(colMeans(df[-c(1:4)])))
})
Benchmark:
library(microbenchmark)
microbenchmark(
f1 = {lapply(lst3, function(df){
data.frame(Site = df$Site[1], x = mean(unlist(df[-c(1:4)])))
})},
f2 = {lapply(lst3, function(df){
data.frame(Site = df$Site[1], x = mean(colMeans(df[-c(1:4)])))
})},
unit = "relative"
)
Unit: relative
expr min lq median uq max neval
f1 1.00000 1.000000 1.000000 1.000000 1.000000 100
f2 2.91545 2.937272 2.927799 2.894704 3.486007 100
Here's another option for your consideration:
library(reshape2)
x <- melt(lst3)
aggregate(value ~ Site, x[grepl("^Sim.*", x$variable),], FUN = mean)
# Site value
#1 G116 1.864233
#2 GG16 2.064567
Or the same concept but using dplyr:
library(dplyr)
filter(x, grepl("^Sim.*", variable)) %>% group_by(Site) %>% summarise(x = mean(value))
#Source: local data frame [2 x 2]
#
# Site x
#1 G116 1.864233
#2 GG16 2.064567
Of course, this could also be done using data.table, for example like this (there are probably several even slightly more efficient ways to do this in data.table):
library(data.table)
setDT(x)[grepl("^Sim.*", variable), list(x = mean(value)), by = Site]
# Site x
#1: G116 1.864233
#2: GG16 2.064567

Plotting all of the rows in different graph - data frame

Propably the code is very simple but I have never tried plotting in R yet.
I would like to have a linear plot for every row and all the plots on different graph.
The number in my data goes from 0 to 1. Value one is the maximum of the plot, in some cases there might be few maximums in a single row. I would like to have a pdf file as an output.
Data:
> dput(head(tbl_end))
structure(list(`NA` = structure(1:6, .Label = c("AT1G01050",
"AT1G01080", "AT1G01090", "AT1G01220", "AT1G01320", "AT1G01420",
"ATCG00800", "ATCG00810", "ATCG00820", "ATCG01090", "ATCG01110",
"ATCG01120", "ATCG01240", "ATCG01300", "ATCG01310", "ATMG01190"
), class = "factor"), `10` = c(0, 0, 0, 0, 0, 0), `20` = c(0,
0, 0, 0, 0, 0), `52.5` = c(0, 1, 0, 0, 0, 0), `81` = c(0, 0.660693687777888,
0, 0, 0, 0), `110` = c(0, 0.521435654491704, 0, 0, 0, 1), `140.5` = c(0,
0.437291194705566, 0, 0, 0, 1), `189` = c(0, 0.52204783488213,
0, 0, 0, 0), `222.5` = c(0, 0.524298383907171, 0, 0, 0, 0), `278` = c(1,
0.376865096972469, 0, 1, 0, 0), `340` = c(0, 0, 0, 0, 0, 0),
`397` = c(0, 0, 0, 0, 0, 0), `453.5` = c(0, 0, 0, 0, 0, 0
), `529` = c(0, 0, 0, 0, 0, 0), `580` = c(0, 0, 0, 0, 0,
0), `630.5` = c(0, 0, 0, 0, 0, 0), `683.5` = c(0, 0, 0, 0,
0, 0), `735.5` = c(0, 0, 0, 0, 0, 0), `784` = c(0, 0, 0.476101907006443,
0, 0, 0), `832` = c(0, 0, 1, 0, 0, 0), `882.5` = c(0, 0,
0, 0, 0, 0), `926.5` = c(0, 0, 0, 0, 1, 0), `973` = c(0,
0, 0, 0, 0, 0), `1108` = c(0, 0, 0, 0, 0, 0), `1200` = c(0,
0, 0, 0, 0, 0)), .Names = c(NA, "10", "20", "52.5", "81",
"110", "140.5", "189", "222.5", "278", "340", "397", "453.5",
"529", "580", "630.5", "683.5", "735.5", "784", "832", "882.5",
"926.5", "973", "1108", "1200"), row.names = c(NA, 6L), class = "data.frame").
Would be great to have a name of the row on the top of each page in pdf.
Here's an example using your dputed data:
# open the pdf file
pdf(file='myfile.pdf')
# since I don't know what values should be on the X axis,
# I'm just using values from 1 to number of y-values
x <- 1:(ncol(tbl_end)-1)
for(i in 1:nrow(tbl_end)){
# plot onto a new pdf page
plot(x=x,y=tbl_end[i,-1],type='b',main=tbl_end[i,1],xlab='X',ylab='Y')
}
# close the pdf file
dev.off()
where the first page is something like this:
If you want to change the style (e.g. lines without the little circles etc.) of the plot, have a look at the documentation.

Resources