2016-11-15 65 views
1

我有一个数据框的列表,索引表示一个家庭结束,另一个开始。我想知道每个系列的statepath列中有多少个类别。数据框列表中的列中有多少个类别?

在我的下面的例子中,我有两个家族,然后我试图得到每个家族中每个statepath类别(233,434,323等)的频率表。

我输入:

List <- 
'$`1` 
Chr Start End Family Statepath 
1 187546286 187552094 father 233 
3 108028534 108032021 father 434 
1 4864403 4878685 mother 323 
1 18898657 18904908 mother 322 
2 460238 461771 offspring 322 
3 108028534 108032021 offspring 434 
$’2’ 
Chr Start End Family Statepath 
1 71481449 71532983 father 535 
2 74507242 74511395 father 233 
2 181864092 181864690 mother 322 
1 71481449 71532983 offspring 535 
2 181864092 181864690 offspring 322 
3 160057791 160113642 offspring 335' 

因此,我预期输出Freq_statepath会是什么样子:

Freq_statepath <- ‘Statepath Family_1 Family_2 
233 1 1 
434 2 0 
323 1 0 
322 2 2 
535 0 2 
335 0 1’ 
+0

请使用'dput'向我们提供你的'list',以便这个问题更具有可重现性(http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproduc IBLE-例子)。 – bouncyball

回答

0

我想你想是这样的:

test <- list(data.frame(Statepath = c(233,434,323,322,322)),data.frame(Statepath = c(434,323,322,322))) 
list_tables <- lapply(test, function(x) data.frame(table(x$Statepath))) 
final_result <- Reduce(function(...) merge(..., by.x = "Var1", by.y = "Var1", all.x = T, all.y = T), list_tables) 
final_result[is.na(final_result)] <- 0 

> test 
[[1]] 
    Statepath 
1  233 
2  434 
3  323 
4  322 
5  322 

[[2]] 
    Statepath 
1  434 
2  323 
3  322 
4  322 

> final_result 
    Var1 Freq.x Freq.y 
1 233  1  0 
2 322  2  2 
3 323  1  1 
4 434  1  1 
相关问题