3

これが問題です。N 行と C 列、および 2 つの因子を持つ行列があります:idsgroup、どちらも長さ N です。例:

m <- matrix( 1:25, nrow= 5, byrow= T )
id <- factor( c( "A", "A", "A", "B", "B" ) )
group <- factor( c( "a", "b", "c", "a", "c" ) )

すべての要因の組み合わせが存在するわけではありませんが、要因の各組み合わせは 1 回しか存在しません。タスクは、行と列mを持つように行列を変換することです。つまり、各変数が元の列と factor のすべての可能なレベルの組み合わせに対応する行列を作成します。欠損値 (id とグループの組み合わせが存在しない場合) は、NA に置き換えられます。上記の例の望ましい出力は次のとおりです。length( levels( id ) )length( levels( group ) ) * Cgroup

  a.1 a.2 a.3 a.4 a.5 b.1 b.2 b.3 b.4 b.5 c.1 c.2 c.3 c.4 c.5
A   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
B  16  17  18  19  20  NA  NA  NA  NA  NA  21  22  23  24  25

私は自分の関数を書きましたが、それはひどく効果がなく、非常に単純なものの機能を複製していると確信しています。

matrixReshuffle <- function( m, ids.row, factor.group ) {

  nr <- nrow( m )
  nc <- ncol( m )
  if( is.null( colnames( m ) ) ) colnames( m ) <- 1:nc

  ret <- NULL
  for( id in levels( ids.row ) ) {

    r <- c()

    for( fg in levels( factor.group ) ) {

      d <- m[ ids.row == id & factor.group == fg,, drop= F ]
      if( nrow( d ) > 1 )
        stop( sprintf( "Too many matches for ids.row= %s and factor.group= %s", id, fg ) )
      else if( nrow( d ) < 1 ) {
        r <- c( r, rep( NA, nc ) )
      } else {
        r <- c( r, d[1,] )
      }

    }

    ret <- rbind( ret, r )

  }

  colnames( ret ) <- paste( rep( levels( factor.group ), each= nc ), rep( colnames( m ), length( levels( factor.group ) ) ), sep= "." )
  rownames( ret ) <- levels( ids.row )

  return( ret )
}
4

3 に答える 3

2

@Aaronの提案に従う:

使用meltおよびacastからreshape2

require(reshape2)
df <- as.data.frame(m)
names(df) <- seq_len(ncol(df))
df.m <- melt(df)
df.m$id <- rep(id, nrow(df.m)/length(id))
df.m$group <- rep(group, nrow(df.m)/length(group))

o <- acast(df.m, id ~ group+variable, value.var="value")
colnames(o) <- sub("_", ".", colnames(o))

#   a.1 a.2 a.3 a.4 a.5 b.1 b.2 b.3 b.4 b.5 c.1 c.2 c.3 c.4 c.5
# A   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
# B  16  17  18  19  20  NA  NA  NA  NA  NA  21  22  23  24  25

これをマトリックスに戻すことができます。

于 2013-02-21T20:10:24.717 に答える
2

そこにいるすべてのマトリックスインデックスファンのために...

C <- ncol(m)
to.row <- matrix(rep(as.numeric(id), C), ncol=C)
to.col <- sweep(col(m),1,(as.numeric(group)-1)*C,`+`)
out <- array(dim=c(nlevels(id), nlevels(group)*C),
             dimnames=list(levels(id), as.vector(t(outer(levels(group), 1:C, paste, sep=".")))))
out[cbind(as.vector(to.row), as.vector(to.col))] <- m
out
#   a.1 a.2 a.3 a.4 a.5 b.1 b.2 b.3 b.4 b.5 c.1 c.2 c.3 c.4 c.5
# A   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
# B  16  17  18  19  20  NA  NA  NA  NA  NA  21  22  23  24  25
于 2013-02-21T21:17:36.550 に答える
1

これは、@Arun の応答のバージョンであり、(私にとって) 理解しやすいようにわずかに変更されています。また、群因子の複製には常に注意を払っています。実際には、これが系統誤差の潜在的な原因の 1 つであることを発見しました。id と group を直接引き継いで、melt() に要素を複製させる方がよいでしょう。しかし、それらはほんの些細なことです。

# add the aggregating variables to the matrix, converted to data frame
df <- data.frame( m )
df$id <- id
df$group <- group

# reshape the data frame
require( reshape2 )
df.m <- melt( df, c( "id", "group" ) )
df <- dcast( df.m, id ~ group + variable )

# df has the required shape, but convert it back to a matrix
rownames( df ) <- df$id
df$id <- NULL
m.reshaped <- as.matrix( df )
于 2013-02-27T08:57:05.557 に答える