计算与R中另一列的其他元素匹配的元素的频率

时间:2017-05-02 09:52:16

标签: r database data-manipulation

说我有

Name<- c("A", "A", "A", "A", "A", "B", "B", "B", "B", "C", "C", "C")
Cate<- c("a", "a", "b", "b", "c", "a", "a", "a", "c", "b", "b", "c")

我想重现以下内容:

Nam fra frb frc
A   2   2   1
B   3   0   1
C   0   2   1

其中frafrbfrc分别是Cate中每个类别(A,B,C)的a,b和c值的频率值Nam

我正在寻找比我正在使用的代码更快的代码(在每个类别中对Nam进行分组,然后计算频率)

3 个答案:

答案 0 :(得分:3)

我们可以从dcast开始data.table这是非常有效和快速的

library(data.table)
dcast(data.table(Name, Cate), Name ~paste0("fr", Cate))
#   Name fra frb frc
#1:    A   2   2   1
#2:    B   3   0   1
#3:    C   0   2   1

一个简单的base R选项是

table(Cate, Name)

数据

Name <- c("A", "A", "A", "A", "A", "B", "B", "B", "B", "C", "C", "C")
Cate <- c("a", "a", "b", "b", "c", "a", "a", "a", "c", "b", "b", "c")

答案 1 :(得分:1)

您还可以使用xtabs()功能:

    xtabs(~Name + Cate)

答案 2 :(得分:1)

为了完整性&#39;这是一个Hadleyverse解决方案:

server {
    listen 80;
    listen [::]:80;
    #server_name git.mydomain.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 default_server ssl;
    listen [::]:443 default_server ssl;
    server_name git.www.mydomain.com;
    # certs sent to the client in SERVER HELLO are concatenated in ssl_certific$
    ssl_certificate /etc/nginx/ssl/mydomain.com.crt;
    ssl_certificate_key /etc/nginx/ssl/mydomain.com.key;
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_session_tickets off;

    # Diffie-Hellman parameter for DHE ciphersuites, recommended 4096 bits
    ssl_dhparam /etc/nginx/ssl/dhparam.pem;

    # modern configuration. tweak to your needs.
    ssl_protocols TLSv1.2;
    ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDH$
    ssl_prefer_server_ciphers on;

    # HSTS (ngx_http_headers_module is required) (15768000 seconds = 6 months)
    add_header Strict-Transport-Security max-age=15768000;

    location / {
            try_files $uri $uri/ =404;
    }
}