Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/84.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.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在data.frame中的高效搜索_R_Performance - Fatal编程技术网

R在data.frame中的高效搜索

R在data.frame中的高效搜索,r,performance,R,Performance,我想使用R组织最有效的搜索值​​在格式为data.frame的表格中,如下所示 x01 x02 x03 x04 x05 x06 x07 1 NA 100 200 300 400 500 600 2 10 1 4 3 6 7 1 3 20 2 5 2 5 8 2 4 30 3 6 1 4 9 8 价值观​​按递增顺序排列在第一行和第一列中。例如,我需要找到第一行中包含300的列和第一列中包含20的行的

我想使用
R
组织最有效的搜索值​​在格式为
data.frame
的表格中,如下所示

    x01 x02 x03 x04 x05 x06 x07
 1  NA 100 200 300 400 500 600
 2  10   1   4   3   6   7   1
 3  20   2   5   2   5   8   2
 4  30   3   6   1   4   9   8
价值观​​按递增顺序排列在第一行和第一列中。例如,我需要找到第一行中包含
300
的列和第一列中包含
20
的行的十字光标的值。值
2
。代码:

coefficient_table_1 <- data.frame(
 x01=c(NA,  10, 20, 30),
 x02=c(100, 1,  2,  3),
 x03=c(200, 4,  5,  6),
 x04=c(300, 3,  2,  1),
 x05=c(400, 6,  5,  4),
 x06=c(500, 7,  8,  9),
 x07=c(600, 1,  2,  8)
)


col_value <- 300
row_value <- 20

col <- 0

for(i in 2:ncol(coefficient_table_1)){
    if(coefficient_table_1[1,i]==col_value ){
        col <- i
    }
}

row <- which(coefficient_table_1$x01==row_value)


value <- coefficient_table_1[row, col] 

系数表1您的数据都是数字,因此最好的做法可能是使用数组,而不是数据帧

由于数组只包含单个类的数据(例如,数字),因此当数据为数组格式时,许多操作都要快得多

试试这个:

x <- as.matrix(coefficient_table_1)
x[which(x[, 1]==row_value), which(x[1, ]==col_value)]

x04 
  2 

x可能值得一提的是返回矩阵坐标的能力,如
which(my.mat==7,array.index=TRUE)