[R] converting each column of a data frame into a matrix with n rows

Davide Piffer pifferdavide at gmail.com
Wed May 17 21:18:07 CEST 2017


I need to convert each vector of a dataframe into a matrix with 2 rows
and 2 columns (i.e. contingency table).
Note I don't want to convert the entire df into a matrix! I want to
apply a function that converts each 4 elements vector of a df into a 2
x 2 matrix.

I wrote something like this, but it will not work:

f_matrix=function(x){ matrix (x)
  nrow=2}
matrix_y=apply(y,2,function(x) f_matrix (x))



More information about the R-help mailing list