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

David Winsemius dwinsemius at comcast.net
Wed May 17 21:34:51 CEST 2017


> On May 17, 2017, at 12:18 PM, Davide Piffer <pifferdavide at gmail.com> wrote:
> 
> 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))

Maybe it shold be:

matrix_y_list = lapply(y, function(x) f_matrix (x) )


> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list