web-dev-qa-db-ja.com

3列のデータフレームをマトリックスに変更します(「長い」形式から「広い」形式)

このようなdata.frameがあります。

x a 1 
x b 2 
x c 3 
y a 3 
y b 3 
y c 2 

マトリックス形式でこれが欲しいので、ヒートマップにフィードしてプロットを作成できます。結果は次のようになります。

    a    b    c
x   1    2    3
y   3    3    2

Reshapeパッケージからcastを試しました。これを行うための手動関数を記述しようとしましたが、正しくできないようです。

119
MalteseUnderdog

これを行うには多くの方法があります。この回答は私のお気に入りの方法から始まりますが、このサイトに散在する同様の質問への回答からさまざまな方法も集めています。

tmp <- data.frame(x=gl(2,3, labels=letters[24:25]),
                  y=gl(3,1,6, labels=letters[1:3]), 
                  z=c(1,2,3,3,3,2))

tidyverseの使用:

これを行うクールな新しい方法は、tidyrのspreadを使用することです。データフレームを返します。これはおそらく、この回答のほとんどの読者が望むものです。ただし、ヒートマップの場合は、これを真のマトリックスに変換する必要があります。

library(tidyr)
spread(tmp, y, z)
##   x a b c
## 1 x 1 2 3
## 2 y 3 3 2

reshape2の使用:

整頓に向けた最初のステップの1つは、reshape2パッケージでした。私はまだ多くの再形成タスクについて、meltおよび*cast関数は整然とした方法よりもクリーンでシンプルだと思います。

マトリックスを取得するには、acastを使用します。

library(reshape2)
acast(tmp, x~y, value.var="z")
##   a b c
## x 1 2 3
## y 3 3 2

または、データフレームを取得するには、次のようにdcastを使用します。 1列の値のデータを変更

dcast(tmp, x~y, value.var="z")
##   x a b c
## 1 x 1 2 3
## 2 y 3 3 2

plyrの使用:

Reshape2とtidyverseの間にplyrが追加され、次に示すようにdaply関数が追加されました。 https://stackoverflow.com/a/7020101/21067

library(plyr)
daply(tmp, .(x, y), function(x) x$z)
##    y
## x   a b c
##   x 1 2 3
##   y 3 3 2

マトリックスインデックスの使用:

これはちょっと古い学校ですが、マトリックスインデックス作成の素晴らしいデモンストレーションであり、特定の状況で本当に役立ちます。

with(tmp, {
  out <- matrix(nrow=nlevels(x), ncol=nlevels(y),
                dimnames=list(levels(x), levels(y)))
  out[cbind(x, y)] <- z
  out
})

xtabsの使用

xtabs(z~x+y, data=tmp)

スパース行列の使用:

ここに見られるように、sparseMatrixパッケージ内にはMatrixもあります。 R-列名でBIGテーブルをマトリックスに変換

with(tmp, sparseMatrix(i = as.numeric(x), j=as.numeric(y), x=z,
                       dimnames=list(levels(x), levels(y))))
## 2 x 3 sparse Matrix of class "dgCMatrix"
##   a b c
## x 1 2 3
## y 3 3 2

reshapeの使用

ここで提案されているように、ベースR関数reshapeを使用することもできます。 列名でテーブルをマトリックスに変換 。ただし、後で余分な列を削除して取得するには少し操作する必要があります正しい名前(表示されていません)。

reshape(tmp, idvar="x", timevar="y", direction="wide")
##   x z.a z.b z.c
## 1 x   1   2   3
## 4 y   3   3   2
171
Aaron

質問は数年前ですが、多分一部の人々はまだ別の答えに興味があります。

パッケージをロードしたくない場合は、次の関数を使用できます。

#' Converts three columns of a data.frame into a matrix -- e.g. to plot 
#' the data via image() later on. Two of the columns form the row and
#' col dimensions of the matrix. The third column provides values for
#' the matrix.
#' 
#' @param data data.frame: input data
#' @param rowtitle string: row-dimension; name of the column in data, which distinct values should be used as row names in the output matrix
#' @param coltitle string: col-dimension; name of the column in data, which distinct values should be used as column names in the output matrix
#' @param datatitle string: name of the column in data, which values should be filled into the output matrix
#' @param rowdecreasing logical: should the row names be in ascending (FALSE) or in descending (TRUE) order?
#' @param coldecreasing logical: should the col names be in ascending (FALSE) or in descending (TRUE) order?
#' @param default_value numeric: default value of matrix entries if no value exists in data.frame for the entries
#' @return matrix: matrix containing values of data[[datatitle]] with rownames data[[rowtitle]] and colnames data[coltitle]
#' @author Daniel Neumann
#' @date 2017-08-29
data.frame2matrix = function(data, rowtitle, coltitle, datatitle, 
                             rowdecreasing = FALSE, coldecreasing = FALSE,
                             default_value = NA) {

  # check, whether titles exist as columns names in the data.frame data
  if ( (!(rowtitle%in%names(data))) 
       || (!(coltitle%in%names(data))) 
       || (!(datatitle%in%names(data))) ) {
    stop('data.frame2matrix: bad row-, col-, or datatitle.')
  }

  # get number of rows in data
  ndata = dim(data)[1]

  # extract rownames and colnames for the matrix from the data.frame
  rownames = sort(unique(data[[rowtitle]]), decreasing = rowdecreasing)
  nrows = length(rownames)
  colnames = sort(unique(data[[coltitle]]), decreasing = coldecreasing)
  ncols = length(colnames)

  # initialize the matrix
  out_matrix = matrix(NA, 
                      nrow = nrows, ncol = ncols,
                      dimnames=list(rownames, colnames))

  # iterate rows of data
  for (i1 in 1:ndata) {
    # get matrix-row and matrix-column indices for the current data-row
    iR = which(rownames==data[[rowtitle]][i1])
    iC = which(colnames==data[[coltitle]][i1])

    # throw an error if the matrix entry (iR,iC) is already filled.
    if (!is.na(out_matrix[iR, iC])) stop('data.frame2matrix: double entry in data.frame')
    out_matrix[iR, iC] = data[[datatitle]][i1]
  }

  # set empty matrix entries to the default value
  out_matrix[is.na(out_matrix)] = default_value

  # return matrix
  return(out_matrix)

}

使い方:

myData = as.data.frame(list('dim1'=c('x', 'x', 'x', 'y','y','y'),
                            'dim2'=c('a','b','c','a','b','c'),
                            'values'=c(1,2,3,3,3,2))) 

myMatrix = data.frame2matrix(myData, 'dim1', 'dim2', 'values')

myMatrix
>   a b c
> x 1 2 3
> y 3 3 2
2
daniel.neumann

完全を期すために、tapply()ソリューションがあります。

with(d, tapply(z, list(x, y), sum))
#   a b c
# x 1 2 3
# y 3 3 2

データ

d <- structure(list(x = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("x", 
"y"), class = "factor"), y = structure(c(1L, 2L, 3L, 1L, 2L, 
3L), .Label = c("a", "b", "c"), class = "factor"), z = c(1, 2, 
3, 3, 3, 2)), class = "data.frame", row.names = c(NA, -6L))
2
jay.sf

ベースR、unstack

unstack(df, V3 ~ V2)
#   a b c
# 1 1 2 3
# 2 3 3 2

これは一般的な解決策ではないかもしれませんが、この場合はうまく機能します。

データ

df<-structure(list(V1 = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("x", 
"y"), class = "factor"), V2 = structure(c(1L, 2L, 3L, 1L, 2L, 
3L), .Label = c("a", "b", "c"), class = "factor"), V3 = c(1L, 
2L, 3L, 3L, 3L, 2L)), .Names = c("V1", "V2", "V3"), class = "data.frame", row.names = c(NA, 
-6L))
2
lebatsnok

tidyr 0.8.3.9000から、pivot_wider()という新しい関数が導入されました。これは基本的に、以前のspread()関数のアップグレードバージョンです (さらに、現在アクティブな開発中ではありません)ピボットビネット から:

このビネットでは、新しいpivot_longer()およびpivot_wider()関数の使用について説明します。彼らの目標は、gather()およびspread()の使いやすさを改善し、他のパッケージにある最新の機能を組み込むことです。

しばらくの間、spread()とgather()の設計に根本的な問題があることが明らかでした。多くの人は名前が直感的でなく、どの方向が広がりに対応し、どの方向に集まるのかを覚えにくいと感じています。また、これらの関数の引数を覚えるのは驚くほど難しいようです。つまり、多くの人(私を含む!)が毎回ドキュメントを参照する必要があります。

使用方法(@Aaronのデータを使用):

pivot_wider(data = tmp, names_from = y, values_from = z)

または、「フル」tidyverse形式で:

tmp %>% 
 pivot_wider(names_from = y, values_from = z)
1
tmfmnk

Tidyverseのtidyrパッケージには、これを行う優れた機能があります。

変数の名前がv1、v2、v3、左から右、データフレームの名前がdatであると仮定します。

dat %>% 
spread(key = v2,
       value = v3)

タダ!

1