Lapply Giving List of List Results












0















I'm having some problems when I try to apply a function to a column. I thought I was doing everything correctly, but it is giving me some pretty insane results. Here is the code:



df <- data.frame(replicate(10,sample(0:10,10,rep=TRUE)))

dummy_fn <-function(col_name){
if (col_name>5){
return(1)
}
else{
return(0)
}
}


df$X11<-lapply(df$X1, dummy_fn)

summary(df$X11)


Here is the result I'm getting:
results



How can I make the results just normal 0 and 1 integers?










share|improve this question

























  • Maybe you'd prefer sapply (or for some more type safety, vapply). I assume your real code is more complicated, otherwise a simple ifelse would probably suffice in this instance.

    – joran
    Nov 26 '18 at 18:58











  • df$X11 is a list and it will summarize each part of the list and not the contents of the list

    – Mike
    Nov 26 '18 at 19:00
















0















I'm having some problems when I try to apply a function to a column. I thought I was doing everything correctly, but it is giving me some pretty insane results. Here is the code:



df <- data.frame(replicate(10,sample(0:10,10,rep=TRUE)))

dummy_fn <-function(col_name){
if (col_name>5){
return(1)
}
else{
return(0)
}
}


df$X11<-lapply(df$X1, dummy_fn)

summary(df$X11)


Here is the result I'm getting:
results



How can I make the results just normal 0 and 1 integers?










share|improve this question

























  • Maybe you'd prefer sapply (or for some more type safety, vapply). I assume your real code is more complicated, otherwise a simple ifelse would probably suffice in this instance.

    – joran
    Nov 26 '18 at 18:58











  • df$X11 is a list and it will summarize each part of the list and not the contents of the list

    – Mike
    Nov 26 '18 at 19:00














0












0








0








I'm having some problems when I try to apply a function to a column. I thought I was doing everything correctly, but it is giving me some pretty insane results. Here is the code:



df <- data.frame(replicate(10,sample(0:10,10,rep=TRUE)))

dummy_fn <-function(col_name){
if (col_name>5){
return(1)
}
else{
return(0)
}
}


df$X11<-lapply(df$X1, dummy_fn)

summary(df$X11)


Here is the result I'm getting:
results



How can I make the results just normal 0 and 1 integers?










share|improve this question
















I'm having some problems when I try to apply a function to a column. I thought I was doing everything correctly, but it is giving me some pretty insane results. Here is the code:



df <- data.frame(replicate(10,sample(0:10,10,rep=TRUE)))

dummy_fn <-function(col_name){
if (col_name>5){
return(1)
}
else{
return(0)
}
}


df$X11<-lapply(df$X1, dummy_fn)

summary(df$X11)


Here is the result I'm getting:
results



How can I make the results just normal 0 and 1 integers?







r lapply






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 19:51









hrbrmstr

61.1k690151




61.1k690151










asked Nov 26 '18 at 18:56









Kevin GregoryKevin Gregory

102




102













  • Maybe you'd prefer sapply (or for some more type safety, vapply). I assume your real code is more complicated, otherwise a simple ifelse would probably suffice in this instance.

    – joran
    Nov 26 '18 at 18:58











  • df$X11 is a list and it will summarize each part of the list and not the contents of the list

    – Mike
    Nov 26 '18 at 19:00



















  • Maybe you'd prefer sapply (or for some more type safety, vapply). I assume your real code is more complicated, otherwise a simple ifelse would probably suffice in this instance.

    – joran
    Nov 26 '18 at 18:58











  • df$X11 is a list and it will summarize each part of the list and not the contents of the list

    – Mike
    Nov 26 '18 at 19:00

















Maybe you'd prefer sapply (or for some more type safety, vapply). I assume your real code is more complicated, otherwise a simple ifelse would probably suffice in this instance.

– joran
Nov 26 '18 at 18:58





Maybe you'd prefer sapply (or for some more type safety, vapply). I assume your real code is more complicated, otherwise a simple ifelse would probably suffice in this instance.

– joran
Nov 26 '18 at 18:58













df$X11 is a list and it will summarize each part of the list and not the contents of the list

– Mike
Nov 26 '18 at 19:00





df$X11 is a list and it will summarize each part of the list and not the contents of the list

– Mike
Nov 26 '18 at 19:00












2 Answers
2






active

oldest

votes


















0














Try unlist. df$X11 <- unlist(lapply(df$X1, dummy_fn))



EDIT:



lapply returns a list. you can unlist the results after using lapply as the step above suggests, and that would work.



Alternatively, you may use sapply instead. sapply simplifies lapply and returns a vector or matrix. Basically, it will unlist the result if it can. Otherwise it will return a list (it will behave same as lapply). use it with caution, and double check the class of the returned results.



In this case, it is able to simplify the results



df$X11<-sapply(df$X1, dummy_fn) #Use sapply

summary(df$X11)
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.00 0.25 1.00 0.70 1.00 1.00





share|improve this answer

































    0














    We don't need an lapply for this



    dummy_fn <- function(data, columnName) {
    as.integer(data[[columnName]] > 5)
    }

    df$X11 <- dummy_fn(df, 'X1')





    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%2f53487373%2flapply-giving-list-of-list-results%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









      0














      Try unlist. df$X11 <- unlist(lapply(df$X1, dummy_fn))



      EDIT:



      lapply returns a list. you can unlist the results after using lapply as the step above suggests, and that would work.



      Alternatively, you may use sapply instead. sapply simplifies lapply and returns a vector or matrix. Basically, it will unlist the result if it can. Otherwise it will return a list (it will behave same as lapply). use it with caution, and double check the class of the returned results.



      In this case, it is able to simplify the results



      df$X11<-sapply(df$X1, dummy_fn) #Use sapply

      summary(df$X11)
      Min. 1st Qu. Median Mean 3rd Qu. Max.
      0.00 0.25 1.00 0.70 1.00 1.00





      share|improve this answer






























        0














        Try unlist. df$X11 <- unlist(lapply(df$X1, dummy_fn))



        EDIT:



        lapply returns a list. you can unlist the results after using lapply as the step above suggests, and that would work.



        Alternatively, you may use sapply instead. sapply simplifies lapply and returns a vector or matrix. Basically, it will unlist the result if it can. Otherwise it will return a list (it will behave same as lapply). use it with caution, and double check the class of the returned results.



        In this case, it is able to simplify the results



        df$X11<-sapply(df$X1, dummy_fn) #Use sapply

        summary(df$X11)
        Min. 1st Qu. Median Mean 3rd Qu. Max.
        0.00 0.25 1.00 0.70 1.00 1.00





        share|improve this answer




























          0












          0








          0







          Try unlist. df$X11 <- unlist(lapply(df$X1, dummy_fn))



          EDIT:



          lapply returns a list. you can unlist the results after using lapply as the step above suggests, and that would work.



          Alternatively, you may use sapply instead. sapply simplifies lapply and returns a vector or matrix. Basically, it will unlist the result if it can. Otherwise it will return a list (it will behave same as lapply). use it with caution, and double check the class of the returned results.



          In this case, it is able to simplify the results



          df$X11<-sapply(df$X1, dummy_fn) #Use sapply

          summary(df$X11)
          Min. 1st Qu. Median Mean 3rd Qu. Max.
          0.00 0.25 1.00 0.70 1.00 1.00





          share|improve this answer















          Try unlist. df$X11 <- unlist(lapply(df$X1, dummy_fn))



          EDIT:



          lapply returns a list. you can unlist the results after using lapply as the step above suggests, and that would work.



          Alternatively, you may use sapply instead. sapply simplifies lapply and returns a vector or matrix. Basically, it will unlist the result if it can. Otherwise it will return a list (it will behave same as lapply). use it with caution, and double check the class of the returned results.



          In this case, it is able to simplify the results



          df$X11<-sapply(df$X1, dummy_fn) #Use sapply

          summary(df$X11)
          Min. 1st Qu. Median Mean 3rd Qu. Max.
          0.00 0.25 1.00 0.70 1.00 1.00






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 26 '18 at 21:48









          Wally Ali

          2,152917




          2,152917










          answered Nov 26 '18 at 19:06









          pooja ppooja p

          1297




          1297

























              0














              We don't need an lapply for this



              dummy_fn <- function(data, columnName) {
              as.integer(data[[columnName]] > 5)
              }

              df$X11 <- dummy_fn(df, 'X1')





              share|improve this answer




























                0














                We don't need an lapply for this



                dummy_fn <- function(data, columnName) {
                as.integer(data[[columnName]] > 5)
                }

                df$X11 <- dummy_fn(df, 'X1')





                share|improve this answer


























                  0












                  0








                  0







                  We don't need an lapply for this



                  dummy_fn <- function(data, columnName) {
                  as.integer(data[[columnName]] > 5)
                  }

                  df$X11 <- dummy_fn(df, 'X1')





                  share|improve this answer













                  We don't need an lapply for this



                  dummy_fn <- function(data, columnName) {
                  as.integer(data[[columnName]] > 5)
                  }

                  df$X11 <- dummy_fn(df, 'X1')






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 26 '18 at 19:00









                  akrunakrun

                  408k13198273




                  408k13198273






























                      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%2f53487373%2flapply-giving-list-of-list-results%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

                      Contact image not getting when fetch all contact list from iPhone by CNContact

                      count number of partitions of a set with n elements into k subsets

                      A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks