1

假设我有一个包含三列的数据框:第一列指定特征的数量(例如颜色),第二列指定组,如果该组中存在该特征(1)或该组中缺少该特征,则第三列( 0):

> d<-data.frame(feature=c("red","blue","green","yellow","red","blue","green","yellow"), group=c(rep("a",4),rep("b",4)),is_there=c(0,1,1,0,1,1,1,0))
> d
  feature group is_there
1     red     a        0
2    blue     a        1
3   green     a        1
4  yellow     a        0
5     red     b        1
6    blue     b        1
7   green     b        1
8  yellow     b        0

现在我想总结一下有多少特征:1.仅在a组中,仅在b组中以及两组中都有多少。此外,我需要提取两组中存在的特征的名称。我怎样才能做到这一点?我想像这样的功能crossprod可能会有所帮助,但我无法弄清楚。

输出将类似于:

feature 
red     1
blue    2
green   2
yellow  0

或者:

feature a b
red     0 1
blue    1 1
green   1 1
yellow  0 0

无论如何,我需要对一个相当大的数据文件有一个更好的概述(原始文件在大约 10 个组中有数百个特征)。

4

5 回答 5

2

听起来 atable就是你想要的。首先,我们对行进行子集化,使is_there列等于 1 并删除第三列。然后我们table在那个子集上调用 a 。

> ( tab <- table(d[d$is_there == 1, -3]) )
#         group
# feature  a b
#   blue   1 1
#   green  1 1
#   red    0 1
#   yellow 0 0

Atable是一个类似矩阵的对象。我们可以像在matrix.

看组a

> tab[,"a"]                           ## vector of group "a"
#  blue  green    red yellow 
#     1      1      0      0 
> tab[,"a"][ tab[,"a"] > 0 ]          ## present in group "a"
#  blue green 
#     1     1 
> names(tab[,"a"][ tab[,"a"] > 0 ])   ## "feature" present in group "a"
# [1] "blue"  "green"

对于 group 也是如此b

于 2014-08-28T08:56:19.750 回答
1

试试下面的代码:

with(d, tapply(is_there, list(feature, group), sum))
#       a b
#blue   1 1
#green  1 1
#red    0 1
#yellow 0 0
于 2014-08-28T09:06:13.680 回答
1
 tbl <- table(d$feature[!!d$is_there], d$group[!!d$is_there])
 rowSums(tbl)
 #blue  green    red yellow 
 #  2      2      1      0 

 tbl

 #       a b
 #blue   1 1
 #green  1 1
 #red    0 1
 #yellow 0 0

如果您想进行如下分组:

  d1 <- as.data.frame(matrix(rep(c("none", "only", "both")[rowSums(tbl)+1],
           each=2), ncol=2, byrow=TRUE, dimnames=dimnames(tbl)),
                                          stringsAsFactors=FALSE)

  d1[!tbl & rowSums(tbl)==1]  <- ""
  d1
 #        a    b
 #blue   both both
 #green  both both
 #red         only
 #yellow none none          
于 2014-08-28T08:55:50.203 回答
0

取以下数据框:

myd <- data.frame(
  feature=c("red","blue","green","yellow","red","blue","green","yellow"),
  group=c(rep("a",4),rep("b",4)),
  is_there=c(0,1,1,0,1,0,1,0))

要获得一个告诉您所有内容的因素,您可以尝试以下代码:

require(reshape2)

res <- acast(myd,feature ~ group, fun=sum, value.var="is_there")
where <- factor(
  colSums(res) - 2*diff(t(res)),
  levels=c(-1,0,2,3),
  labels=c("group2","nowhere","both","group1")
  )

给出:

> res
       a b
blue   1 0
green  1 1
red    0 1
yellow 0 0
> where
   blue   green     red  yellow 
 group1    both  group2 nowhere 
Levels: group2 nowhere both group1

从这里提取无处不在的那些是微不足道的。

请注意,为您提供矩阵的任何其他解决方案res都同样有效(tapply 解决方案会更快)

于 2014-08-28T09:12:38.800 回答
0

那会成功吗?

> tapply(d$feature[d$is_there==1],d$group[d$is_there==1], table)

$a
blue  green    red yellow 
   1      1      0      0 

$b
blue  green    red yellow 
   1      1      1      0 
于 2014-08-28T08:57:37.890 回答