1

3列のデータフレームがあるとしましょう:最初の列は機能の数(色など)を指定し、2番目はグループを指定し、3番目は機能がそのグループに存在する場合(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

それはあなたが望むもののように聞こえtableます。まず、is_there列が 1 になるように行をサブセット化し、3 番目の列を削除します。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
 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 に答える
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 に答える
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 に答える