[英]how to cast a tibble to a sparse matrix
Consider this simple tibble 考虑一下这个简单的小技巧
> data_frame(col1 = c(1,2,3), col2 = c(3,2,NA))
# A tibble: 3 x 2
col1 col2
<dbl> <dbl>
1 1 3
2 2 2
3 3 NA
What is the most efficient way to cast it as a sparse matrix? 将其转换为稀疏矩阵的最有效方法是什么? I tried something like
我尝试了类似的东西
> data_frame(col1 = c(1,2,3), col2 = c(3,2,NA)) %>%
+ as(., 'sparseMatrix')
Error in as(from, "CsparseMatrix") :
no method or default for coercing “tbl_df” to “CsparseMatrix”
with no success. 没有成功。 Trying as suggested:
按照建议尝试:
y <- purrr::reduce(cbind2, map(df, 'Matrix', sparse = TRUE))
does not work either. 也不起作用。
Any good ideas using the tidyverse? 使用tidyverse有什么好主意吗? Thanks!
谢谢!
This is just a translation of the bounty-awarded answer to the post linked above, from base lapply
/ Reduce
to purrr
's map
/ reduce
. 这只是对上面链接的悬赏授予答案的翻译,从基础
lapply
/ Reduce
到purrr
的map
/ reduce
。 The previous answer used: 先前的答案使用了:
Reduce(cbind2, lapply(x[,-1], Matrix, sparse = TRUE))
Part of how this works is that data frames are technically lists, so you can use map
to iterate over the columns of the data frame. 这种工作方式的一部分是技术上讲数据框是列表,因此您可以使用
map
遍历数据框的列。 This yields two sparse matrices, one for each column: 这将产生两个稀疏矩阵,每列一个:
library(dplyr)
library(purrr)
df <- data_frame(col1 = c(1,2,3), col2 = c(3,2,NA))
map(df, Matrix::Matrix, sparse = T)
#> $col1
#> 3 x 1 sparse Matrix of class "dgCMatrix"
#>
#> [1,] 1
#> [2,] 2
#> [3,] 3
#>
#> $col2
#> 3 x 1 sparse Matrix of class "dgCMatrix"
#>
#> [1,] 3
#> [2,] 2
#> [3,] NA
If you then reduce it with cbind2
, that gets you a single sparse matrix. 如果然后使用
cbind2
对其进行cbind2
, cbind2
获得一个稀疏矩阵。
map(df, Matrix::Matrix, sparse = T) %>%
reduce(cbind2)
#> 3 x 2 sparse Matrix of class "dgCMatrix"
#>
#> [1,] 1 3
#> [2,] 2 2
#> [3,] 3 NA
Created on 2018-10-16 by the reprex package (v0.2.1) 由reprex软件包 (v0.2.1)创建于2018-10-16
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.