R:将数据帧的元素与相邻元素相乘

R:将数据帧的元素与相邻元素相乘,r,R,我有一个300x300元素的数据帧。它们中的每一个都是-1或+1: [,1] [,2] [,3] [1,] 1 -1 -1 [2,] 1 1 1 [3,] -1 -1 1 [4,] 1 1 -1 我想要的是迭代我的数据帧,并将每个值与每个相邻值相乘。 即: 对于原始数据帧中的元素[1,1],我需要[1,1]、[1,2]和[2,1]的乘积 对于原始数据帧中的元素[2,2],我

我有一个300x300元素的数据帧。它们中的每一个都是-1或+1:

     [,1]   [,2]   [,3]  
[1,]   1     -1     -1   
[2,]   1      1      1  
[3,]  -1     -1      1  
[4,]   1      1     -1
我想要的是迭代我的数据帧,并将每个值与每个相邻值相乘。
即:
对于原始数据帧中的元素[1,1],我需要[1,1]、[1,2]和[2,1]的乘积
对于原始数据帧中的元素[2,2],我需要[2,2]、[1,2]、[2,1]、[2,3]和[3,2]的乘积

我尝试创建4个新的数据帧,每个数据帧分别向右、向左、向上和向下移动1个元素:

x_up <- shift(x, 1, dir='up')
x_up <- as.array(x_up)
dim(x_up) <- dims
x_down <- shift(x, 1, dir='down')
x_down <- as.array(x_down)
dim(x_down) <- dims
x_left <- shift(x, 1, dir='left')
x_left <- as.array(x_left)
dim(x_left) <- dims
x_right <- shift(x, 1, dir='right')
x_right <- as.array(x_right)
dim(x_right) <- dims

x_up我认为可能有一种更聪明的方法可以做到这一点,但标准方法是迭代每个元素并将其周围环境相乘

首先是:

mat <- matrix(c(1, 1, -1, 1, -1, 1, -1, 1, -1, 1, 1, -1), ncol=3)
现在创建一个空矩阵来保存输出,然后迭代元素并乘以相邻元素

out <- matrix(nrow=nrow(mat), ncol=ncol(mat))
for (i in 1:nrow(mat)) {
  for (j in 1:ncol(mat)) {
    out[i,j] <- prod(mat[i,j], mat2[i-1, j], mat2[i, j-1], mat2[i+1, j], mat2[i, j+1])
  }
}

对于300x300矩阵,这只需要不到一秒钟的时间,因此对您来说可能已经足够了。

这应该可以做到:

ind <- which(x==x, arr.ind=TRUE) # index matrix

# find distances (need distances of 1 or 0) 
dist.mat <- as.matrix(dist(ind))
inds2mult <- apply(dist.mat, 1, function(ii) which(ii <= 1))

# get product of each list element in inds2mult
# and reform into appropriate matrix
matrix(
    sapply(inds2mult, function(ii) prod(unlist(x)[ii])),
    ncol=ncol(x))

#     [,1] [,2] [,3]
#[1,]   -1    1    1
#[2,]   -1    1   -1
#[3,]    1    1    1
#[4,]   -1    1   -1
fields.rdist.near
帮助页面中的增量参数:

阈值距离。由多个点分隔的所有点对 忽略的距离大于增量


@user20650我忘了乘以元素本身,修正了这个问题,谢谢。我得到一个错误:x2[I-1,j]中的错误:维数不正确。我的数据是在一个大的数组中-这个解决方案是否适用于那个数组,或者我应该转换它?你们所说的大数组是什么意思?如果它有两个维度,它就是一个矩阵。如果没有,代码将失败,因为它试图访问维度
i
j
(行/列)。这可能是我创建数据帧时引起的问题。我已经导入了一个二进制png文件,我可以看到它将其导入为一个300 3维的数组。我试图将其转换为300x300的矩阵,然后重新运行代码。抱歉,在询问之前没有这样做。@MadsObi哦,当您以二进制形式读取图像时,确实会发生这种情况。这是某种图像平滑吗?我喜欢这样,但是随着矩阵大小的增加,
dist
变得非常昂贵。。。300x300的旧笔记本内存不足matrix@user20650哦,在发布之前,应该在一个更大的矩阵上进行尝试。我将尝试改进它。我遇到一个错误:无法分配大小为271.6 Gb的向量,因此它可能不适用于我的数据帧。@MadsObi看到了
fields.rdist.near
方法以避免分配问题。@Frank:我读得很轻松,因为Molx answer对我很有用。我很感激你的回答!
> out
     [,1] [,2] [,3]
[1,]   -1    1    1
[2,]   -1    1   -1
[3,]    1    1    1
[4,]   -1    1   -1
ind <- which(x==x, arr.ind=TRUE) # index matrix

# find distances (need distances of 1 or 0) 
dist.mat <- as.matrix(dist(ind))
inds2mult <- apply(dist.mat, 1, function(ii) which(ii <= 1))

# get product of each list element in inds2mult
# and reform into appropriate matrix
matrix(
    sapply(inds2mult, function(ii) prod(unlist(x)[ii])),
    ncol=ncol(x))

#     [,1] [,2] [,3]
#[1,]   -1    1    1
#[2,]   -1    1   -1
#[3,]    1    1    1
#[4,]   -1    1   -1
x <- matrix(rep(-1, 300*300), ncol=300)

ind <- which(x==x, arr.ind=TRUE) # index matrix

library(fields)
ind.list <- fields.rdist.near(ind, delta=1) # took my computer ~ 15 - 20 seconds

inds2mult <- tapply(ind.list$ind[,2], ind.list$ind[,1], list)

matrix(
    sapply(inds2mult, function(ii) prod(unlist(x)[ii])),
    ncol=ncol(x))