【发布时间】:2021-08-02 03:03:35
【问题描述】:
我有一个尺寸为data[1:10,1:50,1:1000] 的大型数组。我想用维度为new_data[1,1:50,1:1000] 的新数据替换所有矩阵的第 5 行。
到目前为止,我已经尝试将数组分开并重新组合在一起:
data1<-data[1:4,1:50,1:1000]
data2<-data[6:10,1:50,1:1000]
combined_data<-rbind(data1,new_data,data2)
但是rbind 在这里似乎不合适,它返回一个大矩阵而不是带有dimensions[1:10,1:50,1:1000] 的大数组
这里有一个简单的例子:
vec1<-1:4
vec2<-c(1,2,2,4,1,2,2,4)
data_array<-array(c(vec1,vec2),dim=c(4,3,10))
data_array[,,1] # visualizing one of the 10 matrix - say they error is in row 3 where we would expect all 3s
new_data<-array(c(3,3,3),dim=c(1,3,10))
new_data[,,1] # correct data that we want to swap into row 3 of all the matrices
array2<-data_array[1:2,,] #correct data from original array
array3<-array(data_array[4,,],dim=c(1,3,10)) #correct data from original array
combined_data <- rbind(array2,new_data,array3) # attempting to combine and new_data into the correct row
但是,这会导致数据的维度为 [1:3,1:60],我的目标是与原始 data_array ([1:4,1:3,1:10]) 的维度完全相同但是在每个矩阵的第 3 行交换了 new_data
【问题讨论】:
-
如果您创建一个小的可重现示例以及预期的输出,这将更容易提供帮助。阅读how to give a reproducible example。
标签: r data-manipulation rbind