How to quickly apply over Document Term Matrix in R










0















I am working on a project that requires me to iterate over a Document Term Matrix, converting all non-zero values to 1 and keeping zero values at zero. The function I'm using now takes forever to run, and I would like help optimizing the code.



My code as it is right now is



convert_counts <- function(x) 
x <- ifelse(x > 0, 1, 0)
x <- factor(x, levels = c(0, 1),
labels = c("No", "Yes"))

data_exp <- apply(data_dtm, 2, convert_counts)


Where data_dtm is a large Document Term Matrix.










share|improve this question
























  • I hope this helps you : stackoverflow.com/questions/12835942/…

    – Carles Sans Fuentes
    Nov 14 '18 at 16:28















0















I am working on a project that requires me to iterate over a Document Term Matrix, converting all non-zero values to 1 and keeping zero values at zero. The function I'm using now takes forever to run, and I would like help optimizing the code.



My code as it is right now is



convert_counts <- function(x) 
x <- ifelse(x > 0, 1, 0)
x <- factor(x, levels = c(0, 1),
labels = c("No", "Yes"))

data_exp <- apply(data_dtm, 2, convert_counts)


Where data_dtm is a large Document Term Matrix.










share|improve this question
























  • I hope this helps you : stackoverflow.com/questions/12835942/…

    – Carles Sans Fuentes
    Nov 14 '18 at 16:28













0












0








0








I am working on a project that requires me to iterate over a Document Term Matrix, converting all non-zero values to 1 and keeping zero values at zero. The function I'm using now takes forever to run, and I would like help optimizing the code.



My code as it is right now is



convert_counts <- function(x) 
x <- ifelse(x > 0, 1, 0)
x <- factor(x, levels = c(0, 1),
labels = c("No", "Yes"))

data_exp <- apply(data_dtm, 2, convert_counts)


Where data_dtm is a large Document Term Matrix.










share|improve this question
















I am working on a project that requires me to iterate over a Document Term Matrix, converting all non-zero values to 1 and keeping zero values at zero. The function I'm using now takes forever to run, and I would like help optimizing the code.



My code as it is right now is



convert_counts <- function(x) 
x <- ifelse(x > 0, 1, 0)
x <- factor(x, levels = c(0, 1),
labels = c("No", "Yes"))

data_exp <- apply(data_dtm, 2, convert_counts)


Where data_dtm is a large Document Term Matrix.







r text-mining






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 16:16







Griffin Barich

















asked Nov 14 '18 at 15:53









Griffin BarichGriffin Barich

32




32












  • I hope this helps you : stackoverflow.com/questions/12835942/…

    – Carles Sans Fuentes
    Nov 14 '18 at 16:28

















  • I hope this helps you : stackoverflow.com/questions/12835942/…

    – Carles Sans Fuentes
    Nov 14 '18 at 16:28
















I hope this helps you : stackoverflow.com/questions/12835942/…

– Carles Sans Fuentes
Nov 14 '18 at 16:28





I hope this helps you : stackoverflow.com/questions/12835942/…

– Carles Sans Fuentes
Nov 14 '18 at 16:28












1 Answer
1






active

oldest

votes


















0














The function you have transforms a sparse matrix to a full character matrix. If you have a large document term matrix this will result in long running times and a good chance of getting a memory error. Replacing values in a sparse matrix can be done quickly if you make use of how the matrix is built. A sparse matrix values are stored in the v (values) part of the matrix. See ?slam::simple_triplet_matrix.



Using any of the apply family on a sparse matrix, without using functions that are designed to work with a sparse matrix will turn it into a normal (dense) matrix. With accordingly long run times and memory issues.



To change all values different from 0 in your case, just use the following:



data_dtm$v[data_dtm$v > 0] <- 1
inspect(data_dtm) # show first 10 columns and rows



This replaces all the values to 1 and keeps the data as a document term matrix (aka nice and sparse).



Depending on your follow up data analysis you really should make use of sparse matrix functions. If you want to transform a large document term matrix into a data.frame or data.table you have a good chance of running out of memory.



For any follow up questions, please include a reproducible example and an expected output.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304091%2fhow-to-quickly-apply-over-document-term-matrix-in-r%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The function you have transforms a sparse matrix to a full character matrix. If you have a large document term matrix this will result in long running times and a good chance of getting a memory error. Replacing values in a sparse matrix can be done quickly if you make use of how the matrix is built. A sparse matrix values are stored in the v (values) part of the matrix. See ?slam::simple_triplet_matrix.



    Using any of the apply family on a sparse matrix, without using functions that are designed to work with a sparse matrix will turn it into a normal (dense) matrix. With accordingly long run times and memory issues.



    To change all values different from 0 in your case, just use the following:



    data_dtm$v[data_dtm$v > 0] <- 1
    inspect(data_dtm) # show first 10 columns and rows



    This replaces all the values to 1 and keeps the data as a document term matrix (aka nice and sparse).



    Depending on your follow up data analysis you really should make use of sparse matrix functions. If you want to transform a large document term matrix into a data.frame or data.table you have a good chance of running out of memory.



    For any follow up questions, please include a reproducible example and an expected output.






    share|improve this answer



























      0














      The function you have transforms a sparse matrix to a full character matrix. If you have a large document term matrix this will result in long running times and a good chance of getting a memory error. Replacing values in a sparse matrix can be done quickly if you make use of how the matrix is built. A sparse matrix values are stored in the v (values) part of the matrix. See ?slam::simple_triplet_matrix.



      Using any of the apply family on a sparse matrix, without using functions that are designed to work with a sparse matrix will turn it into a normal (dense) matrix. With accordingly long run times and memory issues.



      To change all values different from 0 in your case, just use the following:



      data_dtm$v[data_dtm$v > 0] <- 1
      inspect(data_dtm) # show first 10 columns and rows



      This replaces all the values to 1 and keeps the data as a document term matrix (aka nice and sparse).



      Depending on your follow up data analysis you really should make use of sparse matrix functions. If you want to transform a large document term matrix into a data.frame or data.table you have a good chance of running out of memory.



      For any follow up questions, please include a reproducible example and an expected output.






      share|improve this answer

























        0












        0








        0







        The function you have transforms a sparse matrix to a full character matrix. If you have a large document term matrix this will result in long running times and a good chance of getting a memory error. Replacing values in a sparse matrix can be done quickly if you make use of how the matrix is built. A sparse matrix values are stored in the v (values) part of the matrix. See ?slam::simple_triplet_matrix.



        Using any of the apply family on a sparse matrix, without using functions that are designed to work with a sparse matrix will turn it into a normal (dense) matrix. With accordingly long run times and memory issues.



        To change all values different from 0 in your case, just use the following:



        data_dtm$v[data_dtm$v > 0] <- 1
        inspect(data_dtm) # show first 10 columns and rows



        This replaces all the values to 1 and keeps the data as a document term matrix (aka nice and sparse).



        Depending on your follow up data analysis you really should make use of sparse matrix functions. If you want to transform a large document term matrix into a data.frame or data.table you have a good chance of running out of memory.



        For any follow up questions, please include a reproducible example and an expected output.






        share|improve this answer













        The function you have transforms a sparse matrix to a full character matrix. If you have a large document term matrix this will result in long running times and a good chance of getting a memory error. Replacing values in a sparse matrix can be done quickly if you make use of how the matrix is built. A sparse matrix values are stored in the v (values) part of the matrix. See ?slam::simple_triplet_matrix.



        Using any of the apply family on a sparse matrix, without using functions that are designed to work with a sparse matrix will turn it into a normal (dense) matrix. With accordingly long run times and memory issues.



        To change all values different from 0 in your case, just use the following:



        data_dtm$v[data_dtm$v > 0] <- 1
        inspect(data_dtm) # show first 10 columns and rows



        This replaces all the values to 1 and keeps the data as a document term matrix (aka nice and sparse).



        Depending on your follow up data analysis you really should make use of sparse matrix functions. If you want to transform a large document term matrix into a data.frame or data.table you have a good chance of running out of memory.



        For any follow up questions, please include a reproducible example and an expected output.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 18:59









        phiverphiver

        13.5k92835




        13.5k92835





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304091%2fhow-to-quickly-apply-over-document-term-matrix-in-r%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            What does pagestruct do in Eviews?

            Dutch intervention in Lombok and Karangasem

            Channel Islands