Hallo Lilli,
nein, das ist kein reproduzierbares Beispiel, weil es so in R nicht lauffähig ist und weil eine Beschreibung fehlt, wie das richtige Ergebnis aussehen soll. Die Daten als reproduzierbares Beispiel angeben könnte z. B. so aussehen:
Code: Alles auswählen
bsp <- read.table(text = "738 -0.02 NA NA NA NA NA NA NA NA
739 -0.10 NA NA NA NA NA NA NA NA
740 -0.34 NA NA NA NA NA NA NA -0.06
741 0.14 0.12 NA NA NA NA NA -0.12 0.00
742 0.18 0.21 NA NA NA NA NA -0.08 -0.15
743 -0.03 0.00 -0.05 NA -0.06 0.16 NA 0.21 0.00
744 0.00 -0.17 0.11 -0.08 0.10 0.16 NA -0.10 NA
745 -0.11 0.00 0.00 0.20 0.02 0.22 -0.19 0.03 NA
746 0.06 0.10 0.03 -0.04 0.11 -0.03 -0.17 -0.16 NA
747 -0.06 -0.02 0.03 -0.01 -0.10 0.05 0.13 0.12 NA
748 -0.03 0.00 0.14 0.12 0.06 0.03 -0.11 0.00 NA
749 -0.30 -0.08 0.04 0.07 0.07 0.01 -0.38 -0.33 NA
750 -0.13 0.00 -0.06 0.01 -0.02 0.04 -0.17 0.58 NA
751 -0.40 -0.04 0.28 0.11 -0.12 0.14 0.05 -0.05 NA
752 NA -0.04 -0.05 0.03 -0.17 0.06 0.00 0.08 NA
753 NA -0.36 0.03 -0.12 -0.09 -0.08 -0.14 -0.13 NA
754 NA 0.43 0.05 -0.05 -0.03 -0.07 -0.61 0.06 NA
755 NA 0.00 0.05 -0.02 0.13 0.05 -0.29 -0.14 NA
756 NA -0.35 0.03 -0.13 -0.09 -0.12 0.00 -0.16 NA
757 NA 0.19 -0.19 0.06 -0.12 -0.03 NA 0.00 NA
758 NA 0.35 0.14 -0.07 0.17 -0.25 NA 0.04 NA
759 NA 0.14 0.00 -0.03 0.15 0.05 NA -0.07 NA
760 NA -0.21 0.03 -0.07 NA -0.02 NA -0.12 NA
761 NA -0.29 0.06 -0.07 NA -0.03 NA -0.09 NA
762 NA 0.19 -0.09 -0.05 NA 0.08 NA -0.45 NA
763 NA -0.09 -0.23 0.10 NA -0.14 NA -0.36 NA
764 NA -0.41 -0.34 -0.15 NA 0.16 NA 0.14 NA
765 NA -0.41 0.00 -0.20 NA 0.01 NA 0.12 NA
766 NA -0.05 0.09 -0.03 NA 0.11 NA 0.00 NA
767 NA 0.05 -0.14 0.53 NA 0.13 NA -0.22 NA
768 NA -0.50 -0.04 -0.02 NA -0.06 NA 0.43 NA
769 NA 1.80 0.46 0.13 NA 0.03 NA -0.20 NA
770 NA -0.07 -0.10 0.12 NA 0.15 NA 0.00 NA")[,-1]
Siehst Du, wie praktisch das ist, dass mit [code}-Tags markierter Code nicht über die volle Länge angezeigt wird?
Zur Nachmachung empfohlen!
Ich bin nicht sicher, ob ich verstehe, was Du mit returns meinst. Sicher für Kaufleute kein Problem, aber wir sind ja nicht alle Kaufleute.
Schau mal, ob ich das richtig verstanden habe und wenn ja, ob das so mit akzeptabler Geschwindigkeit läuft:
Code: Alles auswählen
threemonth <- numeric(nrow(bsp))
threemonth[1] <- NA
threemonth[2] <- NA
for(i in 3:nrow(bsp))
threemonth[i] <- sum(rowSums(bsp[(i-2):i,],na.rm = TRUE))
plot(threemonth, type = "l")
head(threemonth, 10)
bsp$threemonth <- threemonth
LG,
Bernhard