ich habe da mal wieder ein Problemchen.
Ich habe 2 dfs, df1 und df2
Code: Alles auswählen
df1= =structure(list(Station = c("Station1", "Station1", "Station1",
"Station1", "Station1", "Station1", "Station1", "Station1", "Station1",
"Station1", "Station1", "Station1", "Station1", "Station1", "Station1",
"Station1", "Station1", "Station1", "Station1", "Station1", "Station1",
"Station1", "Station1", "Station1", "Station1", "Station1", "Station1",
"Station1", "Station1", "Station1", "Station1", "Station1", "Station1",
"Station1", "Station1", "Station1", "Station1", "Station1", "Station1",
"Station1", "Station1", "Station1", "Station1", "Station1", "Station1",
"Station1", "Station1", "Station1", "Station1", "Station2", "Station2",
"Station2", "Station2", "Station2", "Station2", "Station2", "Station2",
"Station2", "Station2", "Station2", "Station2", "Station2", "Station2",
"Station2", "Station2", "Station2", "Station2", "Station2", "Station2",
"Station2", "Station2", "Station2", "Station2", "Station2", "Station2",
"Station2", "Station2", "Station2", "Station2", "Station2", "Station2",
"Station2", "Station2", "Station2", "Station2", "Station2", "Station2",
"Station2", "Station2", "Station2", "Station2", "Station2", "Station2",
"Station2", "Station2", "Station2", "Station2", "Station2"),
Timestamp = structure(c(1504994400, 1504998000, 1505001600,
1505005200, 1505008800, 1505012400, 1505016000, 1505019600,
1505023200, 1505026800, 1505030400, 1505034000, 1505037600,
1505041200, 1505044800, 1505048400, 1505052000, 1505055600,
1505059200, 1505062800, 1505066400, 1505070000, 1505073600,
1505077200, 1505080800, 1505084400, 1505088000, 1505091600,
1505095200, 1505098800, 1505102400, 1505106000, 1505109600,
1505113200, 1505116800, 1505120400, 1505124000, 1505127600,
1505131200, 1505134800, 1505138400, 1505142000, 1505145600,
1505149200, 1505152800, 1505156400, 1505160000, 1505163600,
1505167200, 1504994400, 1504998000, 1505001600, 1505005200,
1505008800, 1505012400, 1505016000, 1505019600, 1505023200,
1505026800, 1505030400, 1505034000, 1505037600, 1505041200,
1505044800, 1505048400, 1505052000, 1505055600, 1505059200,
1505062800, 1505066400, 1505070000, 1505073600, 1505077200,
1505080800, 1505084400, 1505088000, 1505091600, 1505095200,
1505098800, 1505102400, 1505106000, 1505109600, 1505113200,
1505116800, 1505120400, 1505124000, 1505127600, 1505131200,
1505134800, 1505138400, 1505142000, 1505145600, 1505149200,
1505152800, 1505156400, 1505160000, 1505163600, 1505167200
), tzone = "GMT", class = c("POSIXct", "POSIXt"))), row.names = c(NA,
-98L), class = "data.frame")
Code: Alles auswählen
df2= structure(list(Station = c("Station1", "Station1", "Station2",
"Station2", "Station2", "Station2", "Station1"), Beginn = structure(c(1505001600,
1505012400, 1505008800, 1505041200, 1505059200, 1505080800, 1505109600
), tzone = "UTC", class = c("POSIXct", "POSIXt")), Ende = structure(c(1505008800,
1505016000, 1505012400, 1505048400, 1505062800, 1505091600, 1505113200
), tzone = "UTC", class = c("POSIXct", "POSIXt")), Class = c("SIR",
"SIR", "SLR", "SIR", "SLR", "SLR", "SLR")), row.names = c(NA,
-7L), class = "data.frame")
In df2 habe ich eine Klasse ("Class") bei der zwischen zwei Zeitschritten ("Beginn" und "Ende") was aufgezeichnet wird.
Ich würde jetzt gerne in df1 eine neue Spalte "Class" einfügen, bei der Spalteninhalt von df2 während dieser Messperiode übertragen wird.
Versucht hatte ich das mit
Code: Alles auswählen
ifelse()
Code: Alles auswählen
df1$class <- ifelse(df1$Station==df2$Station & df1$Timestamp ==df2$Beginn | df1$Timestamp == df2$Ende, df2$Class,NA)
Wahrscheinlich ist das einfachste das mit einer for-Schleife zu lösen...
aber das hat irgendwie auch nicht so ganz funktioniert. Hier mal mein Ansatz dazu...
Code: Alles auswählen
for(i in 1:nrow(df1)){
df1$class[i] <-df2$class[which(df1$Station[i] == df2$Station &
df1$Timestamp[i]>= df2$Beginn&
df1$Timestamp[i]<= df2$Ende)]
}
Vielen Dank schonmal für eure Bemühungen.
Liebe Grüße