使用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)