Repita o vetor para preencher a coluna no quadro de dados
Parece que essa manobra muito simples costumava funcionar para mim, e agora simplesmente não funciona. Uma versão fictícia do problema:
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
Eu esperava que isso funcionasse com o seguinte resultado:
x y z
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 1
I.e. o vetor mais curto deve começar a se repetir automaticamente. Tenho certeza de que isso costumava funcionar para mim (é em um script que eu já executei centenas de vezes sem problemas). Agora nem consigo que o exemplo fictício acima funcione como eu quero. o que estou perdendo?