将向量列堆叠成单列向量
Stacking columns of vectors into single column of vectors
我正在尝试使用 stack():
将列 Vector1
和 Vector2
堆叠到单列数据框 Test
中
df = data.frame("Nr"=c(1,2,3,4,5,6,7,8,9,10))
df$Vector1 <- list(as.numeric(c(123, 435 ,7)))
df$Vector2 <- list(as.numeric(c(234, 456, 4)))
Test <- stack(df[,2:3])[1]
当我使用 stack()
时,我得到一个包含 60 行的单列数据框。但是,我需要 Test
看起来像这样:
c(123, 435 ,7)
c(123, 435 ,7)
c(123, 435 ,7)
c(123, 435 ,7)
c(123, 435 ,7)
c(123, 435 ,7)
c(123, 435 ,7)
c(123, 435 ,7)
c(123, 435 ,7)
c(123, 435 ,7)
c(234, 456, 4)
c(234, 456, 4)
c(234, 456, 4)
c(234, 456, 4)
c(234, 456, 4)
c(234, 456, 4)
c(234, 456, 4)
c(234, 456, 4)
c(234, 456, 4)
c(234, 456, 4)
有没有另一种方法可以在不将向量分成 60 行的情况下完成此操作?
unlist
的一种方式:
result <- data.frame(id = seq(nrow(df) * 2))
result$stack_col <- unlist(df[2:3], recursive = FALSE, use.names = FALSE)
result
# id stack_col
#1 1 123, 435, 7
#2 2 123, 435, 7
#3 3 123, 435, 7
#4 4 123, 435, 7
#5 5 123, 435, 7
#6 6 123, 435, 7
#7 7 123, 435, 7
#8 8 123, 435, 7
#9 9 123, 435, 7
#10 10 123, 435, 7
#11 11 234, 456, 4
#12 12 234, 456, 4
#13 13 234, 456, 4
#14 14 234, 456, 4
#15 15 234, 456, 4
#16 16 234, 456, 4
#17 17 234, 456, 4
#18 18 234, 456, 4
#19 19 234, 456, 4
#20 20 234, 456, 4
您可能需要 stack()$values
的 matrix
列数对应于任意元素的长度,例如第一个 el
元素(假设所有长度都相等)。
matrix(stack(df[,2:3])$values, ncol=length(el(df$Vector1)), byrow=TRUE)
# [,1] [,2] [,3]
# [1,] 123 435 7
# [2,] 123 435 7
# [3,] 123 435 7
# [4,] 123 435 7
# [5,] 123 435 7
# [6,] 123 435 7
# [7,] 123 435 7
# [8,] 123 435 7
# [9,] 123 435 7
# [10,] 123 435 7
# [11,] 234 456 4
# [12,] 234 456 4
# [13,] 234 456 4
# [14,] 234 456 4
# [15,] 234 456 4
# [16,] 234 456 4
# [17,] 234 456 4
# [18,] 234 456 4
# [19,] 234 456 4
# [20,] 234 456 4
我正在尝试使用 stack():
将列Vector1
和 Vector2
堆叠到单列数据框 Test
中
df = data.frame("Nr"=c(1,2,3,4,5,6,7,8,9,10))
df$Vector1 <- list(as.numeric(c(123, 435 ,7)))
df$Vector2 <- list(as.numeric(c(234, 456, 4)))
Test <- stack(df[,2:3])[1]
当我使用 stack()
时,我得到一个包含 60 行的单列数据框。但是,我需要 Test
看起来像这样:
c(123, 435 ,7)
c(123, 435 ,7)
c(123, 435 ,7)
c(123, 435 ,7)
c(123, 435 ,7)
c(123, 435 ,7)
c(123, 435 ,7)
c(123, 435 ,7)
c(123, 435 ,7)
c(123, 435 ,7)
c(234, 456, 4)
c(234, 456, 4)
c(234, 456, 4)
c(234, 456, 4)
c(234, 456, 4)
c(234, 456, 4)
c(234, 456, 4)
c(234, 456, 4)
c(234, 456, 4)
c(234, 456, 4)
有没有另一种方法可以在不将向量分成 60 行的情况下完成此操作?
unlist
的一种方式:
result <- data.frame(id = seq(nrow(df) * 2))
result$stack_col <- unlist(df[2:3], recursive = FALSE, use.names = FALSE)
result
# id stack_col
#1 1 123, 435, 7
#2 2 123, 435, 7
#3 3 123, 435, 7
#4 4 123, 435, 7
#5 5 123, 435, 7
#6 6 123, 435, 7
#7 7 123, 435, 7
#8 8 123, 435, 7
#9 9 123, 435, 7
#10 10 123, 435, 7
#11 11 234, 456, 4
#12 12 234, 456, 4
#13 13 234, 456, 4
#14 14 234, 456, 4
#15 15 234, 456, 4
#16 16 234, 456, 4
#17 17 234, 456, 4
#18 18 234, 456, 4
#19 19 234, 456, 4
#20 20 234, 456, 4
您可能需要 stack()$values
的 matrix
列数对应于任意元素的长度,例如第一个 el
元素(假设所有长度都相等)。
matrix(stack(df[,2:3])$values, ncol=length(el(df$Vector1)), byrow=TRUE)
# [,1] [,2] [,3]
# [1,] 123 435 7
# [2,] 123 435 7
# [3,] 123 435 7
# [4,] 123 435 7
# [5,] 123 435 7
# [6,] 123 435 7
# [7,] 123 435 7
# [8,] 123 435 7
# [9,] 123 435 7
# [10,] 123 435 7
# [11,] 234 456 4
# [12,] 234 456 4
# [13,] 234 456 4
# [14,] 234 456 4
# [15,] 234 456 4
# [16,] 234 456 4
# [17,] 234 456 4
# [18,] 234 456 4
# [19,] 234 456 4
# [20,] 234 456 4