Wiederholen Sie den Vektor, um die Spalte im Datenrahmen nach unten zu füllen.

Scheint, als ob dieses sehr einfache Manöver bei mir funktioniert hätte, und jetzt funktioniert es einfach nicht mehr. Eine Dummy-Version des Problems:

df <- data.frame(x = 1:5) # create simple dataframe
df
  x
1 1
2 2
3 3
4 4
5 5

df$y <- c(1:5) # adding a new column with a vector of the exact same length. Works out like it should
df
 x y
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5

df$z <- c(1:4) # trying to add a new colum, this time with a vector with less elements than there are rows in the dataframe.

Error in `
df <- data.frame(x = 1:5) # create simple dataframe
df
  x
1 1
2 2
3 3
4 4
5 5

df$y <- c(1:5) # adding a new column with a vector of the exact same length. Works out like it should
df
 x y
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5

df$z <- c(1:4) # trying to add a new colum, this time with a vector with less elements than there are rows in the dataframe.

Error in `$<-.data.frame`(`*tmp*`, "z", value = 1:4) : 
  replacement has 4 rows, data has 5
lt;-.data.frame`(`*tmp*`, "z", value = 1:4) : replacement has 4 rows, data has 5

ch hatte erwartet, dass dies mit dem folgenden Ergebnis funktioniert:

 x y z
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 1

D. Der kürzere Vektor sollte sich automatisch wiederholen. Ich bin mir ziemlich sicher, dass dies bei mir funktioniert hat (es ist in einem Skript enthalten, das ich zuvor hundert Mal ohne Probleme ausgeführt habe). Jetzt kann ich nicht einmal das obige Dummy-Beispiel so einsetzen, wie ich es möchte. Was vermisse ich

Antworten auf die Frage(4)

Ihre Antwort auf die Frage