remove columns with NA values from list of matrices












-1














I have a list of matrices, and need to remove columns containing NA values, within each matrix.



I tried to use lapply with !is.na to apply this to all the matrices in the entire list, but what it returns is a list of vectors with NAs excluded, whereas I still want a list of matrices (just without the columns containing NA).



> my_list
$mat1
V1 V2 V3
[1,] 1 5 NA
[1,] 2 6 NA
[1,] 3 7 NA
[1,] 4 8 NA

$mat2
V1 V2 V3
[1,] 1 NA 9
[1,] 2 NA 10
[1,] 3 NA 11
[1,] 4 NA 12

> lapply(my_list,function(x) x[!is.na(x)])
$mat1
[1] 1 2 3 4 5 6 7 8

$mat2
[1] 1 2 3 4 9 10 11 12


The output I'm trying to get is this:



$mat1
V1 V2
[1,] 1 5
[1,] 2 6
[1,] 3 7
[1,] 4 8

$mat2
V1 V3
[1,] 1 9
[1,] 2 10
[1,] 3 11
[1,] 4 12









share|improve this question




















  • 4




    Please give a Minimal, Complete, and Verifiable example in your question!
    – jogo
    Nov 21 '18 at 9:27






  • 1




    lapply(my_list,function(x) x[,colSums(is.na(x)) < 1, drop = FALSE])
    – Andre Elrico
    Nov 21 '18 at 9:28








  • 1




    @RLave complete.cases looks row-wise for NA. So not practical here
    – Andre Elrico
    Nov 21 '18 at 9:38






  • 3




    For one matrix apply(is.na(A), 2, any) gives you the logical index for the columns with NA. To remove the columns do: A[, !apply(is.na(A), 2, any)]. To do this with a list of matrices do: lapply(L, function(A) A[, !apply(is.na(A), 2, any)])
    – jogo
    Nov 21 '18 at 10:02








  • 1




    Thank you, jogo, this solved it. I added an verifiable example to the question now for clarity
    – Neuroguy
    Nov 21 '18 at 10:11
















-1














I have a list of matrices, and need to remove columns containing NA values, within each matrix.



I tried to use lapply with !is.na to apply this to all the matrices in the entire list, but what it returns is a list of vectors with NAs excluded, whereas I still want a list of matrices (just without the columns containing NA).



> my_list
$mat1
V1 V2 V3
[1,] 1 5 NA
[1,] 2 6 NA
[1,] 3 7 NA
[1,] 4 8 NA

$mat2
V1 V2 V3
[1,] 1 NA 9
[1,] 2 NA 10
[1,] 3 NA 11
[1,] 4 NA 12

> lapply(my_list,function(x) x[!is.na(x)])
$mat1
[1] 1 2 3 4 5 6 7 8

$mat2
[1] 1 2 3 4 9 10 11 12


The output I'm trying to get is this:



$mat1
V1 V2
[1,] 1 5
[1,] 2 6
[1,] 3 7
[1,] 4 8

$mat2
V1 V3
[1,] 1 9
[1,] 2 10
[1,] 3 11
[1,] 4 12









share|improve this question




















  • 4




    Please give a Minimal, Complete, and Verifiable example in your question!
    – jogo
    Nov 21 '18 at 9:27






  • 1




    lapply(my_list,function(x) x[,colSums(is.na(x)) < 1, drop = FALSE])
    – Andre Elrico
    Nov 21 '18 at 9:28








  • 1




    @RLave complete.cases looks row-wise for NA. So not practical here
    – Andre Elrico
    Nov 21 '18 at 9:38






  • 3




    For one matrix apply(is.na(A), 2, any) gives you the logical index for the columns with NA. To remove the columns do: A[, !apply(is.na(A), 2, any)]. To do this with a list of matrices do: lapply(L, function(A) A[, !apply(is.na(A), 2, any)])
    – jogo
    Nov 21 '18 at 10:02








  • 1




    Thank you, jogo, this solved it. I added an verifiable example to the question now for clarity
    – Neuroguy
    Nov 21 '18 at 10:11














-1












-1








-1







I have a list of matrices, and need to remove columns containing NA values, within each matrix.



I tried to use lapply with !is.na to apply this to all the matrices in the entire list, but what it returns is a list of vectors with NAs excluded, whereas I still want a list of matrices (just without the columns containing NA).



> my_list
$mat1
V1 V2 V3
[1,] 1 5 NA
[1,] 2 6 NA
[1,] 3 7 NA
[1,] 4 8 NA

$mat2
V1 V2 V3
[1,] 1 NA 9
[1,] 2 NA 10
[1,] 3 NA 11
[1,] 4 NA 12

> lapply(my_list,function(x) x[!is.na(x)])
$mat1
[1] 1 2 3 4 5 6 7 8

$mat2
[1] 1 2 3 4 9 10 11 12


The output I'm trying to get is this:



$mat1
V1 V2
[1,] 1 5
[1,] 2 6
[1,] 3 7
[1,] 4 8

$mat2
V1 V3
[1,] 1 9
[1,] 2 10
[1,] 3 11
[1,] 4 12









share|improve this question















I have a list of matrices, and need to remove columns containing NA values, within each matrix.



I tried to use lapply with !is.na to apply this to all the matrices in the entire list, but what it returns is a list of vectors with NAs excluded, whereas I still want a list of matrices (just without the columns containing NA).



> my_list
$mat1
V1 V2 V3
[1,] 1 5 NA
[1,] 2 6 NA
[1,] 3 7 NA
[1,] 4 8 NA

$mat2
V1 V2 V3
[1,] 1 NA 9
[1,] 2 NA 10
[1,] 3 NA 11
[1,] 4 NA 12

> lapply(my_list,function(x) x[!is.na(x)])
$mat1
[1] 1 2 3 4 5 6 7 8

$mat2
[1] 1 2 3 4 9 10 11 12


The output I'm trying to get is this:



$mat1
V1 V2
[1,] 1 5
[1,] 2 6
[1,] 3 7
[1,] 4 8

$mat2
V1 V3
[1,] 1 9
[1,] 2 10
[1,] 3 11
[1,] 4 12






r matrix na






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 10:07







Neuroguy

















asked Nov 21 '18 at 9:26









NeuroguyNeuroguy

447




447








  • 4




    Please give a Minimal, Complete, and Verifiable example in your question!
    – jogo
    Nov 21 '18 at 9:27






  • 1




    lapply(my_list,function(x) x[,colSums(is.na(x)) < 1, drop = FALSE])
    – Andre Elrico
    Nov 21 '18 at 9:28








  • 1




    @RLave complete.cases looks row-wise for NA. So not practical here
    – Andre Elrico
    Nov 21 '18 at 9:38






  • 3




    For one matrix apply(is.na(A), 2, any) gives you the logical index for the columns with NA. To remove the columns do: A[, !apply(is.na(A), 2, any)]. To do this with a list of matrices do: lapply(L, function(A) A[, !apply(is.na(A), 2, any)])
    – jogo
    Nov 21 '18 at 10:02








  • 1




    Thank you, jogo, this solved it. I added an verifiable example to the question now for clarity
    – Neuroguy
    Nov 21 '18 at 10:11














  • 4




    Please give a Minimal, Complete, and Verifiable example in your question!
    – jogo
    Nov 21 '18 at 9:27






  • 1




    lapply(my_list,function(x) x[,colSums(is.na(x)) < 1, drop = FALSE])
    – Andre Elrico
    Nov 21 '18 at 9:28








  • 1




    @RLave complete.cases looks row-wise for NA. So not practical here
    – Andre Elrico
    Nov 21 '18 at 9:38






  • 3




    For one matrix apply(is.na(A), 2, any) gives you the logical index for the columns with NA. To remove the columns do: A[, !apply(is.na(A), 2, any)]. To do this with a list of matrices do: lapply(L, function(A) A[, !apply(is.na(A), 2, any)])
    – jogo
    Nov 21 '18 at 10:02








  • 1




    Thank you, jogo, this solved it. I added an verifiable example to the question now for clarity
    – Neuroguy
    Nov 21 '18 at 10:11








4




4




Please give a Minimal, Complete, and Verifiable example in your question!
– jogo
Nov 21 '18 at 9:27




Please give a Minimal, Complete, and Verifiable example in your question!
– jogo
Nov 21 '18 at 9:27




1




1




lapply(my_list,function(x) x[,colSums(is.na(x)) < 1, drop = FALSE])
– Andre Elrico
Nov 21 '18 at 9:28






lapply(my_list,function(x) x[,colSums(is.na(x)) < 1, drop = FALSE])
– Andre Elrico
Nov 21 '18 at 9:28






1




1




@RLave complete.cases looks row-wise for NA. So not practical here
– Andre Elrico
Nov 21 '18 at 9:38




@RLave complete.cases looks row-wise for NA. So not practical here
– Andre Elrico
Nov 21 '18 at 9:38




3




3




For one matrix apply(is.na(A), 2, any) gives you the logical index for the columns with NA. To remove the columns do: A[, !apply(is.na(A), 2, any)]. To do this with a list of matrices do: lapply(L, function(A) A[, !apply(is.na(A), 2, any)])
– jogo
Nov 21 '18 at 10:02






For one matrix apply(is.na(A), 2, any) gives you the logical index for the columns with NA. To remove the columns do: A[, !apply(is.na(A), 2, any)]. To do this with a list of matrices do: lapply(L, function(A) A[, !apply(is.na(A), 2, any)])
– jogo
Nov 21 '18 at 10:02






1




1




Thank you, jogo, this solved it. I added an verifiable example to the question now for clarity
– Neuroguy
Nov 21 '18 at 10:11




Thank you, jogo, this solved it. I added an verifiable example to the question now for clarity
– Neuroguy
Nov 21 '18 at 10:11












2 Answers
2






active

oldest

votes


















1














For one matrix apply(is.na(mat), 2, any) gives you the logical index for the columns with NA. To remove the columns do: 



mat[, !apply(is.na(mat), 2, any)]


To do this with a list of matrices do: 



lapply(my_list, function(mat) mat[, !apply(is.na(mat), 2, any)])


Data:



> dput(my_list)
list(structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, NA, NA, NA,
NA), .Dim = c(4L, 3L), .Dimnames = list(NULL, c("V1", "V2", "V3"
))), structure(c(1L, 2L, 3L, 4L, NA, NA, NA, NA, 9L, 10L, 11L,
12L), .Dim = c(4L, 3L), .Dimnames = list(NULL, c("V1", "V2",
"V3"))))





share|improve this answer































    0














    Or another option is tidyverse with select_if after converting to data.frame



    library(tidyverse)
    map(lst1, ~ .x %>%
    as.data.frame %>%
    select_if(~ all(!is.na(.))))
    #[[1]]
    # V1 V2
    #1 1 5
    #2 2 6
    #3 3 7
    #4 4 8

    #[[2]]
    # V1 V3
    #1 1 9
    #2 2 10
    #3 3 11
    #4 4 12





    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%2f53408869%2fremove-columns-with-na-values-from-list-of-matrices%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      For one matrix apply(is.na(mat), 2, any) gives you the logical index for the columns with NA. To remove the columns do: 



      mat[, !apply(is.na(mat), 2, any)]


      To do this with a list of matrices do: 



      lapply(my_list, function(mat) mat[, !apply(is.na(mat), 2, any)])


      Data:



      > dput(my_list)
      list(structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, NA, NA, NA,
      NA), .Dim = c(4L, 3L), .Dimnames = list(NULL, c("V1", "V2", "V3"
      ))), structure(c(1L, 2L, 3L, 4L, NA, NA, NA, NA, 9L, 10L, 11L,
      12L), .Dim = c(4L, 3L), .Dimnames = list(NULL, c("V1", "V2",
      "V3"))))





      share|improve this answer




























        1














        For one matrix apply(is.na(mat), 2, any) gives you the logical index for the columns with NA. To remove the columns do: 



        mat[, !apply(is.na(mat), 2, any)]


        To do this with a list of matrices do: 



        lapply(my_list, function(mat) mat[, !apply(is.na(mat), 2, any)])


        Data:



        > dput(my_list)
        list(structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, NA, NA, NA,
        NA), .Dim = c(4L, 3L), .Dimnames = list(NULL, c("V1", "V2", "V3"
        ))), structure(c(1L, 2L, 3L, 4L, NA, NA, NA, NA, 9L, 10L, 11L,
        12L), .Dim = c(4L, 3L), .Dimnames = list(NULL, c("V1", "V2",
        "V3"))))





        share|improve this answer


























          1












          1








          1






          For one matrix apply(is.na(mat), 2, any) gives you the logical index for the columns with NA. To remove the columns do: 



          mat[, !apply(is.na(mat), 2, any)]


          To do this with a list of matrices do: 



          lapply(my_list, function(mat) mat[, !apply(is.na(mat), 2, any)])


          Data:



          > dput(my_list)
          list(structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, NA, NA, NA,
          NA), .Dim = c(4L, 3L), .Dimnames = list(NULL, c("V1", "V2", "V3"
          ))), structure(c(1L, 2L, 3L, 4L, NA, NA, NA, NA, 9L, 10L, 11L,
          12L), .Dim = c(4L, 3L), .Dimnames = list(NULL, c("V1", "V2",
          "V3"))))





          share|improve this answer














          For one matrix apply(is.na(mat), 2, any) gives you the logical index for the columns with NA. To remove the columns do: 



          mat[, !apply(is.na(mat), 2, any)]


          To do this with a list of matrices do: 



          lapply(my_list, function(mat) mat[, !apply(is.na(mat), 2, any)])


          Data:



          > dput(my_list)
          list(structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, NA, NA, NA,
          NA), .Dim = c(4L, 3L), .Dimnames = list(NULL, c("V1", "V2", "V3"
          ))), structure(c(1L, 2L, 3L, 4L, NA, NA, NA, NA, 9L, 10L, 11L,
          12L), .Dim = c(4L, 3L), .Dimnames = list(NULL, c("V1", "V2",
          "V3"))))






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 10:39

























          answered Nov 21 '18 at 10:29









          jogojogo

          9,85692135




          9,85692135

























              0














              Or another option is tidyverse with select_if after converting to data.frame



              library(tidyverse)
              map(lst1, ~ .x %>%
              as.data.frame %>%
              select_if(~ all(!is.na(.))))
              #[[1]]
              # V1 V2
              #1 1 5
              #2 2 6
              #3 3 7
              #4 4 8

              #[[2]]
              # V1 V3
              #1 1 9
              #2 2 10
              #3 3 11
              #4 4 12





              share|improve this answer


























                0














                Or another option is tidyverse with select_if after converting to data.frame



                library(tidyverse)
                map(lst1, ~ .x %>%
                as.data.frame %>%
                select_if(~ all(!is.na(.))))
                #[[1]]
                # V1 V2
                #1 1 5
                #2 2 6
                #3 3 7
                #4 4 8

                #[[2]]
                # V1 V3
                #1 1 9
                #2 2 10
                #3 3 11
                #4 4 12





                share|improve this answer
























                  0












                  0








                  0






                  Or another option is tidyverse with select_if after converting to data.frame



                  library(tidyverse)
                  map(lst1, ~ .x %>%
                  as.data.frame %>%
                  select_if(~ all(!is.na(.))))
                  #[[1]]
                  # V1 V2
                  #1 1 5
                  #2 2 6
                  #3 3 7
                  #4 4 8

                  #[[2]]
                  # V1 V3
                  #1 1 9
                  #2 2 10
                  #3 3 11
                  #4 4 12





                  share|improve this answer












                  Or another option is tidyverse with select_if after converting to data.frame



                  library(tidyverse)
                  map(lst1, ~ .x %>%
                  as.data.frame %>%
                  select_if(~ all(!is.na(.))))
                  #[[1]]
                  # V1 V2
                  #1 1 5
                  #2 2 6
                  #3 3 7
                  #4 4 8

                  #[[2]]
                  # V1 V3
                  #1 1 9
                  #2 2 10
                  #3 3 11
                  #4 4 12






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 '18 at 18:03









                  akrunakrun

                  400k13189264




                  400k13189264






























                      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%2f53408869%2fremove-columns-with-na-values-from-list-of-matrices%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







                      Popular posts from this blog

                      Ottavio Pratesi

                      Tricia Helfer

                      15 giugno