Skip to content

Instantly share code, notes, and snippets.

@produnis
Created July 5, 2012 05:36
Show Gist options
  • Save produnis/3051561 to your computer and use it in GitHub Desktop.
Save produnis/3051561 to your computer and use it in GitHub Desktop.
MMA
mma <- read.table("http://www.produnis.de/R/MMA.TXT",sep=";",header=T)
head(mma)
attach(mma)
# Erzeuge eine neue Spalte "time", in welcher Datum und Uhrzeit
# im POSIXct-Format zusammengefasst werden.
mma$time <- as.POSIXct(paste(Datum,Uhrzeit),format="%Y-%m-%d %H:%M")
#Wandle falsch-numerisches in factor() um
mma$Action <- factor(mma$Action)
mma$Person <- factor(mma$Person)
# fertig
detach(mma)
head(mma)
get.data <- function(data,intervall) {
# kompatibilitaetsgruende
mma <- data
# Startzeit und Endzeit
start <- min(mma$time)
end <- max(mma$time)
# Ich erzeuge jetzt die erste Reihe des data.frames()
# Intervall-Anfangszeitpunkt und Endpunkt
x <- start
y <- start + intervall
new<-c(
table(subset(data,data$time>x&data$time<y)$Qualif),
table(subset(data,data$time>x&data$time<y)$Actiontxt),
table(subset(data,data$time>x&data$time<y)$Kategorie)#,
)
# umwandeln als Datenframe, und Zeit hinzufügen
new <- as.data.frame(t(c(new,as.character(x))))
# erhöhe Start, damit das nächste Intervall genommen wird
start=start+intervall
print("Looks good, calculating... this may take some time...")
# erst jetzt fang ich mit der eigentlichen Schleife an
# denn erst jetzt hab ich eine Reihe des neuen Frames
# so dass rbind() jetzt funktioniert.
# Die Schleife macht jetzt genau das selbe nochmal:
while(start<end){
x <- start
y <- start + intervall
new2<-c(
table(subset(data,data$time>x&data$time<y)$Qualif),
table(subset(data,data$time>x&data$time<y)$Actiontxt),
table(subset(data,data$time>x&data$time<y)$Kategorie)#,
)
# Hier verbinde ich die neue Reihe mit dem Datenframe
new2 <- as.data.frame(t(c(new2,as.character(x))))
new <- rbind(new,new2)
start=start+intervall
}
return(new)
}
new<-get.data(mma,3600)# Intervall = 3600sek = 1h
new<-get.data(mma,900)# (15 Minuten gleich 900 Sekunden)
#-------------------------------------------------------
x <- as.POSIXct("2005:10:04:04:00:00",format="%Y:%m:%d:%H:%M:%S")
y <- x + 86400 # 86400Sekunden= 1day
mma$day <- 0
mma[(mma$time>x&mma$time<y),10]<-1
Tag1 <- subset(mma,mma$time>x&mma$time<y)
x <- x + 86400 # 86400Sekunden= 1day
y <- x + 86400 # 86400Sekunden= 1day
mma[(mma$time>x&mma$time<y),10]<-2
Tag2 <- subset(mma,mma$time>x&mma$time<y)
x <- x + 86400 # 86400Sekunden= 1day
y <- x + 86400 # 86400Sekunden= 1day
mma[(mma$time>x&mma$time<y),10]<-3
Tag3 <- subset(mma,mma$time>x&mma$time<y)
x <- x + 86400 # 86400Sekunden= 1day
y <- x + 86400 # 86400Sekunden= 1day
mma[(mma$time>x&mma$time<y),10]<-4
Tag4 <- subset(mma,mma$time>x&mma$time<y)
x <- x + 86400 # 86400Sekunden= 1day
y <- x + 86400 # 86400Sekunden= 1dtay
mma[(mma$time>x&mma$time<y),10]<-5
Tag5 <- subset(mma,mma$time>x&mma$time<y)
x <- x + 86400 # 86400Sekunden= 1day
y <- x + 86400 # 86400Sekunden= 1day
mma[(mma$time>x&mma$time<y),10]<-6
Tag6 <- subset(mma,mma$time>x&mma$time<y)
x <- x + 86400 # 86400Sekunden= 1day
y <- x + 86400 # 86400Sekunden= 1day
mma[(mma$time>x&mma$time<y),10]<-7
Tag7 <- subset(mma,mma$time>x&mma$time<y)
rm(x,y)
head(Tag5)
get.per.day <- function(data,get.data,day){
new.chir <- get.data(subset(data,data$Station=="Chirurgie"),900)
new.chir$Station <- "Chirurgie"
new.derm <- get.data(subset(data,data$Station=="Dermatologie"),900)
new.derm$Station <- "Dermatologie"
new <- rbind(new.chir, new.derm)
new$day <- day
names(new)[names(new)=="V47"] <- "Time"
new$Time <- as.POSIXct(new$Time,format="%Y-%m-%d %H:%M")
return(new)
}
get.per.day(Tag1,get.data,1)
Tag1.freq <- get.per.day(Tag1,get.data,1)
Tag2.freq <- get.per.day(Tag2,get.data,2)
Tag3.freq <- get.per.day(Tag3,get.data,3)
Tag4.freq <- get.per.day(Tag4,get.data,4)
Tag5.freq <- get.per.day(Tag5,get.data,5)
Tag6.freq <- get.per.day(Tag6,get.data,6)
Tag7.freq <- get.per.day(Tag7,get.data,7)
Tag1.freq$Time <-Tag1.freq[["Time"]]
Tag2.freq$Time <-Tag2.freq[["Time"]]-60
Tag3.freq$Time <-Tag3.freq[["Time"]]+60
Tag4.freq$Time <-Tag4.freq[["Time"]]+60
Tag5.freq$Time <-Tag5.freq[["Time"]]+60
Tag6.freq$Time <-Tag6.freq[["Time"]]+60
Tag7.freq$Time <-Tag7.freq[["Time"]]+60
head(Tag1.freq)
mma.freq <- rbind(Tag1.freq, Tag2.freq, Tag3.freq, Tag4.freq, Tag5.freq, Tag6.freq, Tag7.freq)
head(mma.freq)
load(url("http://www.produnis.de/R/MMA.RData"))
library(ggplot2)
qplot(time,data=subset(mma,mma$Station=="Chirurgie"&mma$day==2),geom="bar",binwidth=500,fill=Kategorie)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment