Quantcast

Convert large matrix directly to data.table

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Convert large matrix directly to data.table

HrQlX
Is there a way to pass a matrix-object directly into the data.table-format?
I couldn't find this functionality being mentioned in the package documentation, but maybe there exists a solution already?

Many thanks in advance!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Convert large matrix directly to data.table

Matthew Dowle

Welcome.

Like this?

> M = matrix(1:12,nrow=3)
> M
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12
> as.data.table(M)
   V1 V2 V3 V4
1:  1  4  7 10
2:  2  5  8 11
3:  3  6  9 12
>

Things like as.* methods are considered standard R, so aren't documented
explicitly.  You can see how it does it by typing
"data.table:::as.data.table.matrix".

If anyone needs that to be faster, it would be easy to port into C, so
please raise a feature request if needed.


> Is there a way to pass a matrix-object directly into the
> data.table-format?
> I couldn't find this functionality being mentioned in the package
> documentation, but maybe there exists a solution already?
>
> Many thanks in advance!
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Convert-large-matrix-directly-to-data-table-tp4642897.html
> Sent from the datatable-help mailing list archive at Nabble.com.
> _______________________________________________
> datatable-help mailing list
> [hidden email]
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
>


_______________________________________________
datatable-help mailing list
[hidden email]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Convert large matrix directly to data.table

HrQlX
Hi Matthew,

First of all thank you for the quick response.  Is there a way to include a statement like stringsAsFactors into data.table:::as.data.table.matrix?

My 10mil by 20 matrices are of mode character, but the columns shouldn't be coerced into factors as is the default behaviour....
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Convert large matrix directly to data.table

Matthew Dowle

Please add a FR to add stringsAsFactors to as.data.table.matrix method,
and may as well capture feature request to port that to C, at the same
time.

In meatime, given M your character matrix :

as.data.table(as.data.frame(M,stringsAsFactors=FALSE))


> Hi Matthew,
>
> First of all thank you for the quick response.  Is there a way to include
> a
> statement like stringsAsFactors into data.table:::as.data.table.matrix?
>
> My 10mil by 20 matrices are of mode character, but the columns shouldn't
> be
> coerced into factors as is the default behaviour....
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Convert-large-matrix-directly-to-data-table-tp4642897p4643008.html
> Sent from the datatable-help mailing list archive at Nabble.com.
> _______________________________________________
> datatable-help mailing list
> [hidden email]
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
>


_______________________________________________
datatable-help mailing list
[hidden email]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Convert large matrix directly to data.table

Matthew Dowle

or, to avoid the copy of as.data.table(), then a faster but more raw way is :

setattr(as.data.frame(M,stringsAsFactors=FALSE), "class", "data.table")

which, if you need data.frame inheritence to work, you'll have to remember
to include :

setattr(as.data.frame(M,stringsAsFactors=FALSE), "class",
c("data.frame","data.table"))



>
> Please add a FR to add stringsAsFactors to as.data.table.matrix method,
> and may as well capture feature request to port that to C, at the same
> time.
>
> In meatime, given M your character matrix :
>
> as.data.table(as.data.frame(M,stringsAsFactors=FALSE))
>
>
>> Hi Matthew,
>>
>> First of all thank you for the quick response.  Is there a way to
>> include
>> a
>> statement like stringsAsFactors into data.table:::as.data.table.matrix?
>>
>> My 10mil by 20 matrices are of mode character, but the columns shouldn't
>> be
>> coerced into factors as is the default behaviour....
>>
>>
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/Convert-large-matrix-directly-to-data-table-tp4642897p4643008.html
>> Sent from the datatable-help mailing list archive at Nabble.com.
>> _______________________________________________
>> datatable-help mailing list
>> [hidden email]
>> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
>>
>
>
> _______________________________________________
> datatable-help mailing list
> [hidden email]
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
>


_______________________________________________
datatable-help mailing list
[hidden email]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
Loading...