Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R在apply函数中访问行索引_R_Syntax_Large Data - Fatal编程技术网

R在apply函数中访问行索引

R在apply函数中访问行索引,r,syntax,large-data,R,Syntax,Large Data,我在内存中有一个很大的数据集,大约有40万行。在处理这个数据帧的子集时,我想生成一个大图像,并根据数据帧中的条目将该图像中的元素设置为等于一个特定值。我使用for循环来完成这项工作非常简单,无疑是愚蠢的: library('Matrix') #saveMe is a subset of the dataframe containing the x-ranges I want #in columns 1,2; y-ranges in 3-4, and values in 5. saveMe&

我在内存中有一个很大的数据集,大约有40万行。在处理这个数据帧的子集时,我想生成一个大图像,并根据数据帧中的条目将该图像中的元素设置为等于一个特定值。我使用
for
循环来完成这项工作非常简单,无疑是愚蠢的:

library('Matrix')

#saveMe is a subset of the dataframe containing the x-ranges I want 
#in columns 1,2; y-ranges in 3-4, and values in 5. 
saveMe<-structure(list(XMin = c(1, 17, 19, 19, 21, 29, 29, 31, 31, 31, 31, 33, 33, 35, 37, 39, 39, 39, 41, 43), XMax = c(9, 15, 1, 3,1, 17, 37, 5, 13, 25, 35, 17, 43, 23, 47, 25, 25, 33, 21, 29), YMin = c(225, 305, 435, 481, 209, 1591, 157, 115, 1, 691, 79, 47, 893, 1805, 809, 949, 2179, 1733, 339, 739), YMax = c(277,315, 435, 499, 213, 1689, 217, 133, 1, 707, 111, 33, 903,1827, 849, 973, 2225, 1723, 341, 765), Value = c(3, 1, 0,1, 1, 4, 3, 1, 1, 0, 2, 1, 1, 0, 2, 1, 1, 2, 0, 0)), .Names = c("XMin", "XMax", "YMin", "YMax", "Value"),class = c("data.table", "data.frame"), row.names = c(NA, -20L))

#Create sparse matrix to store the result:
xMax <- max(saveMe$XMax) - min(saveMe$XMin)+1
yMax <- max(saveMe$YMax) - min(saveMe$YMin)+1
img<-Matrix(0, nrow = xMax, ncol = yMax, sparse = TRUE)

for (kx in 1:nrow(saveMe)) {
  img[as.numeric(saveMe[kx,1]):as.numeric(saveMe[kx,2]), as.numeric(saveMe[kx,3]):as.numeric(saveMe[kx,4])] <- as.numeric(saveMe[kx,5])
}
nnzero(img)
image(img)
库('Matrix')
#saveMe是包含我想要的x范围的数据帧的子集
#第1、2栏;y范围为3-4,数值为5。

saveMe你的怀疑是对的。 试着说服自己:

f <- function(x){
    x <- 5
}

x <- 4

f(x)
# Nothing is returned
x 
# [1] 4

y <- f(x)
x
# [1] 4
y
# [1] 5
这类似于

rm(x, y)
f <- function(x){
    x <- 5
    x
}
x <- 4
f(x)
# [1] 5
x
# [1] 4
这在哪里节省了你的时间?这里节省的大量时间是使用
[
从基于索引的数据表中提取单个值,而不是使用
[
。以下是数据:

使用行和列整数索引,在一个400000行的数据表中查找5个单值(因此循环中有2000000个查找)并基于这些值分配一个数组400000次。分配可能很难优化,但查找不是。让我们对数据表中的整数索引查找和单个值的分配分别进行100次尝试,比较
[
[
运算符

DT <- data.table(x = sample(5000))
single <- replicate(100, {
  system.time({
    for (i in seq_len(nrow(DT))){
      z <- DT[i,1]
    }
  })
})  
double <- replicate(100, {
  system.time({
    for (i in seq_len(nrow(DT))){
      z <- DT[[i,1]]
    }
  })
})

rowMeans(single)
# user.self   sys.self    elapsed user.child  sys.child 
#   1.69405    0.03519    1.89836    0.00000    0.00000 
rowMeans(double)
# user.self   sys.self    elapsed user.child  sys.child 
#   0.05047    0.00083    0.05668    0.00000    0.00000 

DT-Sathish——在SE上以可复制的方式显示数据帧的好方法是什么?请参见下面的编辑。我添加了我的总体建议和一些基准,为您提供了一些数据re:如何提高效率。这似乎对我来说并不实际。我已经用上面的示例数据运行了一遍,并且
img
仍然没有非零ele更新?你没有用apply更新img。你会得到一个新对象。这是一件好事。如果你想用
apply()更新对象
你必须打破一些关于范围界定的规则。请参见我答案末尾的示例。
x
仍然是
4
。如果你想使用函数在该函数的内部环境之外更新变量,你必须使用
我要去开会,但稍后我可以查看你的对象以及你如何使用apply。顺便说一句,apply通常不会加速循环。它仍然是一个循环,只是在一个很好的受保护的包装器中。这就是为什么您不能从用户定义的函数中获得返回值的原因。
rm(x, y)
f <- function(x){
    x <- 5
    x
}
x <- 4
f(x)
# [1] 5
x
# [1] 4
for (i in seq_len(nrow(saveMe))){
  img[saveMe[[i,1]]:saveMe[[i,2]], saveMe[[i,3]]:saveMe[[i,4]]] <- saveMe[[i,5]]
}
DT <- data.table(x = sample(5000))
single <- replicate(100, {
  system.time({
    for (i in seq_len(nrow(DT))){
      z <- DT[i,1]
    }
  })
})  
double <- replicate(100, {
  system.time({
    for (i in seq_len(nrow(DT))){
      z <- DT[[i,1]]
    }
  })
})

rowMeans(single)
# user.self   sys.self    elapsed user.child  sys.child 
#   1.69405    0.03519    1.89836    0.00000    0.00000 
rowMeans(double)
# user.self   sys.self    elapsed user.child  sys.child 
#   0.05047    0.00083    0.05668    0.00000    0.00000