Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.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 快速迭代行的方法_R - Fatal编程技术网

R 快速迭代行的方法

R 快速迭代行的方法,r,R,我试图将数据帧的每一行除以存储在第二个映射数据帧中的数字 for(g in rownames(data_table)){ print(g) data_table[g,] <- data_table[g,]/mapping[g,2] } for(行名中的g(数据表)){ 印刷品(g) 数据表[g,]试试这个: sweep(data_table, 1, mapping[[2]], "/") 就速度而言,这里是使用iris数据集并包括您的版本的可能性的基准: microbenchmark::

我试图将数据帧的每一行除以存储在第二个映射数据帧中的数字

for(g in rownames(data_table)){
print(g)
data_table[g,] <- data_table[g,]/mapping[g,2]
}
for(行名中的g(数据表)){
印刷品(g)
数据表[g,]试试这个:

sweep(data_table, 1, mapping[[2]], "/")
就速度而言,这里是使用
iris
数据集并包括您的版本的可能性的基准:

microbenchmark::microbenchmark(
 A = {
     for(g in rownames(test)){
         # print(g)
           test[g,] <- test[g,]/test[g,2]
         }
   },
 B = sweep(test, 1, test[[2]], "/"),

 C = test / test[[2]],

 times = 100 
)

#Unit: microseconds
#expr       min        lq        mean    median         uq        max neval
#A 82374.693 83722.023 101688.1254 84582.052 147280.057 157507.892   100
#B   453.652   484.393    514.4094   513.850    539.480    623.688   100
#C   404.506   423.794    456.0063   446.101    470.675    729.205   100
microbenchmark::microbenchmark(
A={
用于(行名称中的g(测试)){
#印刷品(g)

test[g,]如果两个变量的行数相同,则可以将此操作矢量化:

dt <- data.frame(a = rnorm(100), b = rnorm(100))
mapping <- data.frame(x = rnorm(100), y = rnorm(100))

dt / mapping[,2]

dt首先,去除打印。这会使打印速度变得更慢。您可以尝试
apply
lappy
。对于您的情况,您可能需要使用
mappy
,因为您有多个数据帧,它们的行数是否相等?如果顺序正确,您可以一次分割整个列。为什么
mappy
de>数据\u表
对象不是矩阵?iris
数据集对于有意义的性能基准来说太小了