重复向量以填充数据框中的列
Repeat vector to fill down column in data frame
似乎这个非常简单的操作曾经对我有用,但现在根本行不通了。问题的虚拟版本:
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
我原以为这会产生以下结果:
x y z
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 1
即较短的矢量应该会自动开始重复。我很确定这曾经对我有用(它在我之前 运行 一百次都没有问题的脚本中)。现在我什至不能让上面的虚拟示例像我想要的那样工作。我错过了什么?
如果vector可以均匀回收,进入data.frame,就不会报错或警告:
df <- data.frame(x = 1:10)
df$z <- 1:5
这可能是您之前遇到的情况。
你可以让你的向量适应你提到的 rep_len
:
df$y <- rep_len(1:3, length.out=10)
这导致
df
x z y
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 1
5 5 5 2
6 6 1 3
7 7 2 1
8 8 3 2
9 9 4 3
10 10 5 1
请注意,您可以使用更常见的 rep
函数代替 rep_len
:
df$y <- rep(1:3,len=10)
来自 rep
的帮助文件:
rep.int
and rep_len
are faster simplified versions for two common cases. They are not generic.
如果总行数是新向量长度的倍数,则可以正常工作。如果不是,它就不会在任何地方都起作用。特别是,您可能已经将这种类型的回收与矩阵一起使用:
data.frame(1:6, 1:3, 1:4) # not a multiply
# Error in data.frame(1:6, 1:3, 1:4) :
# arguments imply differing number of rows: 6, 3, 4
data.frame(1:6, 1:3) # a multiple
# X1.6 X1.3
# 1 1 1
# 2 2 2
# 3 3 3
# 4 4 1
# 5 5 2
# 6 6 3
cbind(1:6, 1:3, 1:4) # works even with not a multiple
# [,1] [,2] [,3]
# [1,] 1 1 1
# [2,] 2 2 2
# [3,] 3 3 3
# [4,] 4 1 4
# [5,] 5 2 1
# [6,] 6 3 2
# Warning message:
# In cbind(1:6, 1:3, 1:4) :
# number of rows of result is not a multiple of vector length (arg 3)
似乎这个非常简单的操作曾经对我有用,但现在根本行不通了。问题的虚拟版本:
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
我原以为这会产生以下结果:
x y z
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 1
即较短的矢量应该会自动开始重复。我很确定这曾经对我有用(它在我之前 运行 一百次都没有问题的脚本中)。现在我什至不能让上面的虚拟示例像我想要的那样工作。我错过了什么?
如果vector可以均匀回收,进入data.frame,就不会报错或警告:
df <- data.frame(x = 1:10)
df$z <- 1:5
这可能是您之前遇到的情况。
你可以让你的向量适应你提到的 rep_len
:
df$y <- rep_len(1:3, length.out=10)
这导致
df
x z y
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 1
5 5 5 2
6 6 1 3
7 7 2 1
8 8 3 2
9 9 4 3
10 10 5 1
请注意,您可以使用更常见的 rep
函数代替 rep_len
:
df$y <- rep(1:3,len=10)
来自 rep
的帮助文件:
rep.int
andrep_len
are faster simplified versions for two common cases. They are not generic.
如果总行数是新向量长度的倍数,则可以正常工作。如果不是,它就不会在任何地方都起作用。特别是,您可能已经将这种类型的回收与矩阵一起使用:
data.frame(1:6, 1:3, 1:4) # not a multiply
# Error in data.frame(1:6, 1:3, 1:4) :
# arguments imply differing number of rows: 6, 3, 4
data.frame(1:6, 1:3) # a multiple
# X1.6 X1.3
# 1 1 1
# 2 2 2
# 3 3 3
# 4 4 1
# 5 5 2
# 6 6 3
cbind(1:6, 1:3, 1:4) # works even with not a multiple
# [,1] [,2] [,3]
# [1,] 1 1 1
# [2,] 2 2 2
# [3,] 3 3 3
# [4,] 4 1 4
# [5,] 5 2 1
# [6,] 6 3 2
# Warning message:
# In cbind(1:6, 1:3, 1:4) :
# number of rows of result is not a multiple of vector length (arg 3)