Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.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/1/angularjs/23.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_Count - Fatal编程技术网

使用r跨多个列进行频率计数

使用r跨多个列进行频率计数,r,count,R,Count,我有一个数据框,其形式如下: x <- Chrom sample1 sample2 sample3 ... Contig12 0/0 0/0 0/1 Contig12 ./. ./. 0/0 Contig28 0/0 0/0 0/0 Contig28 1/1 1/1 1/1 Contig55 0/0 0/0 0/1 Contig55 0/1 0/1

我有一个数据框,其形式如下:

x <-
Chrom    sample1    sample2    sample3  ...
Contig12    0/0     0/0     0/1
Contig12    ./.     ./.     0/0
Contig28    0/0     0/0     0/0
Contig28    1/1     1/1     1/1
Contig55    0/0     0/0     0/1
Contig55    0/1     0/1     0/1
Contig61    ./.     0/1     1/1
.
.
.
有没有关于我如何做到这一点的建议?我曾尝试使用plyr包中的count(),但我不知道如何在每一列中使用它

非常感谢您的帮助

library(dplyr)
df %>% gather(key, value, -Chrom) %>% # gather turn dataset from wide to long format by collapse (collect) values in all columns 
                                      #except Chrom into two columns key and value. See ?gather for more info
       dplyr::select(-Chrom) %>%      #select all columns except Chrom i.e. key and value 
       table()                        # count the number of each unique pear

         value
 key       ./. 0/0 0/1 1/1
  sample1   2   3   1   1
  sample2   1   3   2   1
  sample3   0   2   3   2
资料
df@A.Suliman,++ve代表nice命令,请您也在您的帖子中发布解释,我们将非常感谢您。@RavinderSingh13感谢您的好话。请检查我的更新。
library(dplyr)
df %>% gather(key, value, -Chrom) %>% # gather turn dataset from wide to long format by collapse (collect) values in all columns 
                                      #except Chrom into two columns key and value. See ?gather for more info
       dplyr::select(-Chrom) %>%      #select all columns except Chrom i.e. key and value 
       table()                        # count the number of each unique pear

         value
 key       ./. 0/0 0/1 1/1
  sample1   2   3   1   1
  sample2   1   3   2   1
  sample3   0   2   3   2
df <- read.table(text="
      Chrom    sample1    sample2    sample3
             Contig12    0/0     0/0     0/1
             Contig12    ./.     ./.     0/0
             Contig28    0/0     0/0     0/0
             Contig28    1/1     1/1     1/1
             Contig55    0/0     0/0     0/1
             Contig55    0/1     0/1     0/1
             Contig61    ./.     0/1     1/1
              ",header=T, stringsAsFactors = F)