从坐标列表中分配矩阵中的1

时间:2016-10-12 01:15:08

标签: r vectorization

我想要一个坐标列表。对于坐标中的每个位置,我想为矩阵指定1。最终矩阵应如下所示。我想使用快速矢量化方法而不是for循环。

<div id="icon-holder">
    <div id="line-01">
        <div class="back-line-container-outer">
            <div class="back-line-container-inner">
                <svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 132.73 13.92">
                    <title>001</title>
                    <path d="M207.73,134.79v13.92l-11.19-1.04q-9.9-.915-19.84-.92H75v-10H176.7q9.93,0,19.84-.92Z" transform="translate(-75 -134.79)" fill="#231f20"/>
                </svg>
            </div>
        </div>
        <div class="front-line-container-outer">
            <div class="front-line-container-inner">
                <svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 132.65 17.84">
                    <title>002</title>
                    <path d="M75.73,150.67V132.83h101.7q9.93,0,19.84.92l11.11,1.04v13.92l-11.11,1.04q-9.9.915-19.84.92Z" transform="translate(-75.73 -132.83)" fill="#231f20"/>
                </svg>
            </div>
        </div>
    </div>

    <div id="line-02">
        <div class="back-line-container-outer">
            <div class="back-line-container-inner">
                <svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 132.73 13.92">
                    <title>001</title>
                    <path d="M207.73,134.79v13.92l-11.19-1.04q-9.9-.915-19.84-.92H75v-10H176.7q9.93,0,19.84-.92Z" transform="translate(-75 -134.79)" fill="#231f20"/>
                </svg>
            </div>
        </div>
        <div class="front-line-container-outer">
            <div class="front-line-container-inner">
                <svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 132.65 17.84">
                    <title>002</title>
                    <path d="M75.73,150.67V132.83h101.7q9.93,0,19.84.92l11.11,1.04v13.92l-11.11,1.04q-9.9.915-19.84.92Z" transform="translate(-75.73 -132.83)" fill="#231f20"/>
                </svg>
            </div>
        </div>
    </div>

    <div id="line-03">
        <div class="back-line-container-outer">
            <div class="back-line-container-inner">
                <svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 132.73 13.92">
                    <title>001</title>
                    <path d="M207.73,134.79v13.92l-11.19-1.04q-9.9-.915-19.84-.92H75v-10H176.7q9.93,0,19.84-.92Z" transform="translate(-75 -134.79)" fill="#231f20"/>
                </svg>
            </div>
        </div>
        <div class="front-line-container-outer">
            <div class="front-line-container-inner">
                <svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 132.65 17.84">
                    <title>002</title>
                    <path d="M75.73,150.67V132.83h101.7q9.93,0,19.84.92l11.11,1.04v13.92l-11.11,1.04q-9.9.915-19.84.92Z" transform="translate(-75.73 -132.83)" fill="#231f20"/>
                </svg>
            </div>
        </div>
    </div>

    <div id="line-04">
        <div class="back-line-container-outer">
            <div class="back-line-container-inner">
                <svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 132.73 13.92">
                    <title>001</title>
                    <path d="M207.73,134.79v13.92l-11.19-1.04q-9.9-.915-19.84-.92H75v-10H176.7q9.93,0,19.84-.92Z" transform="translate(-75 -134.79)" fill="#231f20"/>
                </svg>
            </div>
        </div>
        <div class="front-line-container-outer">
            <div class="front-line-container-inner">
                <svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 132.65 17.84">
                    <title>002</title>
                    <path d="M75.73,150.67V132.83h101.7q9.93,0,19.84.92l11.11,1.04v13.92l-11.11,1.04q-9.9.915-19.84.92Z" transform="translate(-75.73 -132.83)" fill="#231f20"/>
                </svg>
            </div>
        </div>
    </div>
</div>

示例数据:

    > sample.matrix
      A B C D E F G H I J K L M N O P Q R S T
    A 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    B 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
    C 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    D 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
    E 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    F 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
    G 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    H 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
    I 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    J 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
    K 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    L 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    M 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
    N 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
    O 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
    P 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
    Q 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
    R 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
    S 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    T 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

我尝试过的是什么。

    set.seed(15)
    dat <- data.frame(x=sample(LETTERS[1:20], 15,set.seed(15)),y=sample(LETTERS[1:20], 15,set.seed(15)), stringsAsFactors = FALSE)
    sample.matrix <- matrix(data=0, nrow = 20, ncol = 20, dimnames = list(LETTERS[1:20],LETTERS[1:20]))

我尝试的不起作用。

    for ( n in 1:15){
      sample.matrix[dat[n,]$x,dat[n,]$y] =1
    }

3 个答案:

答案 0 :(得分:8)

这是该语言的一个很酷的编程方面。在R中,我们可以使用两列矩阵进行子集化:

sample.matrix[as.matrix(dat)] <- 1

有关工作原理的更多信息

来自?Extract的帮助(粗体文字是我的重点):

  

第三种索引形式是通过带有一列的数字矩阵   对于每个维度:索引矩阵的每一行然后选择一个   数组的单个元素,结果是一个向量。负   索引矩阵中不允许使用索引。 NA和零值是   允许:忽略包含零的索引矩阵的行,   而包含NA的行在结果中产生NA。

     

通过字符矩阵进行索引,每个维度一列   如果数组具有维度名称,也会受到支持。和数字一样   矩阵索引,索引矩阵的每一行选择一个单独的元素   数组。指数与适当的维度相匹配   名。允许NA并在结果中产生NA。独一无二   索引以及空字符串(“”)是不允许的   导致错误。

在您的情况下,您有一个矩阵,其中的字母代表维度名称。

     x   y  
[1,] "B" "C"
[2,] "S" "S"
[3,] "L" "B"
[4,] "N" "L"
[5,] "E" "G"
[6,] "M" "A"

当我们将此矩阵插入到一组括号[as.matrix(dat)]中时,求值程序将检测矩阵并使用特殊类型的子集。第一列表示行,第二列表示列。

head(sample.matrix)
  A B C D E F G H I J K L M N O P Q R S T
A 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
B 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
C 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
D 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
E 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
F 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0

答案 1 :(得分:3)

您可以使用dcast中的data.table

dat$z <- 1
dcast(dat,x~y,fill=0, value="z")

   x B C D G H I J K L M N O P Q T
1  A 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
2  B 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
3  C 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
4  D 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
5  F 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
6  H 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
7  J 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
8  K 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
9  L 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
10 M 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
11 N 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
12 O 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
13 Q 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
14 R 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
15 T 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0

答案 2 :(得分:1)

这也有效:

sample.matrix[t(apply(dat, 1, function(x) 
             c(which(dimnames(sample.matrix)[[1]]==x[1]),
               which(dimnames(sample.matrix)[[2]]==x[2]))))] <- 1

sample.matrix
  A B C D E F G H I J K L M N O P Q R S T
A 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
B 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
C 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
D 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
E 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
F 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
G 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
H 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
I 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
J 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
K 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
L 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
M 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
N 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
O 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
P 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
Q 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
R 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
S 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
T 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0