In R data.table what is the difference between taking dot(.) and 'c' in j while using .SD?How it works?









up vote
2
down vote

favorite
1












Take DT as mtcars data table.



DT <- as.data.table(mtcars) 


While taking multiple arguments in 'j' with .SD, if we use dot(.) before j like below code



DT[ , .(lapply(.SD, sum), .N), by = (cyl) ]


the result comes in vertical order with without the column names.



O/P:




cyl V1 N



1: 6 138.2 7



2: 6 1283.2 7



3: 6 856 7



4: 6 25.1 7



5: 6 21.82 7



6: 6 125.84 7



7: 6 4 7



8: 6 3 7




But when I replace that with the dot(.) in 'j' with 'c' like below,



DT[ , c(lapply(.SD, sum), .N), by = (cyl) ]


the result comes in horizontal order.



O/P:




cyl mpg disp hp drat wt qsec vs am gear carb N



6 138.2 1283.2 856 25.10 21.820 125.84 4 3 27 24 7



4 293.3 1156.5 909 44.78 25.143 210.51 10 8 45 17 11



8 211.4 4943.4 2929 45.21 55.989 234.81 0 2 46 49 14




In another case, without lapply, exactly the opposite happens.



DT[ , c(sum(mpg), .N), by = (cyl) ]


gives the output vertically



O/P:




cyl V1



1: 6 138.2



2: 6 7.0



3: 4 293.3



4: 4 11.0



5: 8 211.4



6: 8 14.0




whereas a dot(.) in 'j' gives the output horizonatlly.



DT[ , .(sum(mpg), .N), by = (cyl) ]


O/P:




cyl V1 N



1: 6 138.2 7



2: 4 293.3 11



3: 8 211.4 14




Why does this happens? Why the result is ordered in such way?










share|improve this question

























    up vote
    2
    down vote

    favorite
    1












    Take DT as mtcars data table.



    DT <- as.data.table(mtcars) 


    While taking multiple arguments in 'j' with .SD, if we use dot(.) before j like below code



    DT[ , .(lapply(.SD, sum), .N), by = (cyl) ]


    the result comes in vertical order with without the column names.



    O/P:




    cyl V1 N



    1: 6 138.2 7



    2: 6 1283.2 7



    3: 6 856 7



    4: 6 25.1 7



    5: 6 21.82 7



    6: 6 125.84 7



    7: 6 4 7



    8: 6 3 7




    But when I replace that with the dot(.) in 'j' with 'c' like below,



    DT[ , c(lapply(.SD, sum), .N), by = (cyl) ]


    the result comes in horizontal order.



    O/P:




    cyl mpg disp hp drat wt qsec vs am gear carb N



    6 138.2 1283.2 856 25.10 21.820 125.84 4 3 27 24 7



    4 293.3 1156.5 909 44.78 25.143 210.51 10 8 45 17 11



    8 211.4 4943.4 2929 45.21 55.989 234.81 0 2 46 49 14




    In another case, without lapply, exactly the opposite happens.



    DT[ , c(sum(mpg), .N), by = (cyl) ]


    gives the output vertically



    O/P:




    cyl V1



    1: 6 138.2



    2: 6 7.0



    3: 4 293.3



    4: 4 11.0



    5: 8 211.4



    6: 8 14.0




    whereas a dot(.) in 'j' gives the output horizonatlly.



    DT[ , .(sum(mpg), .N), by = (cyl) ]


    O/P:




    cyl V1 N



    1: 6 138.2 7



    2: 4 293.3 11



    3: 8 211.4 14




    Why does this happens? Why the result is ordered in such way?










    share|improve this question























      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      Take DT as mtcars data table.



      DT <- as.data.table(mtcars) 


      While taking multiple arguments in 'j' with .SD, if we use dot(.) before j like below code



      DT[ , .(lapply(.SD, sum), .N), by = (cyl) ]


      the result comes in vertical order with without the column names.



      O/P:




      cyl V1 N



      1: 6 138.2 7



      2: 6 1283.2 7



      3: 6 856 7



      4: 6 25.1 7



      5: 6 21.82 7



      6: 6 125.84 7



      7: 6 4 7



      8: 6 3 7




      But when I replace that with the dot(.) in 'j' with 'c' like below,



      DT[ , c(lapply(.SD, sum), .N), by = (cyl) ]


      the result comes in horizontal order.



      O/P:




      cyl mpg disp hp drat wt qsec vs am gear carb N



      6 138.2 1283.2 856 25.10 21.820 125.84 4 3 27 24 7



      4 293.3 1156.5 909 44.78 25.143 210.51 10 8 45 17 11



      8 211.4 4943.4 2929 45.21 55.989 234.81 0 2 46 49 14




      In another case, without lapply, exactly the opposite happens.



      DT[ , c(sum(mpg), .N), by = (cyl) ]


      gives the output vertically



      O/P:




      cyl V1



      1: 6 138.2



      2: 6 7.0



      3: 4 293.3



      4: 4 11.0



      5: 8 211.4



      6: 8 14.0




      whereas a dot(.) in 'j' gives the output horizonatlly.



      DT[ , .(sum(mpg), .N), by = (cyl) ]


      O/P:




      cyl V1 N



      1: 6 138.2 7



      2: 4 293.3 11



      3: 8 211.4 14




      Why does this happens? Why the result is ordered in such way?










      share|improve this question













      Take DT as mtcars data table.



      DT <- as.data.table(mtcars) 


      While taking multiple arguments in 'j' with .SD, if we use dot(.) before j like below code



      DT[ , .(lapply(.SD, sum), .N), by = (cyl) ]


      the result comes in vertical order with without the column names.



      O/P:




      cyl V1 N



      1: 6 138.2 7



      2: 6 1283.2 7



      3: 6 856 7



      4: 6 25.1 7



      5: 6 21.82 7



      6: 6 125.84 7



      7: 6 4 7



      8: 6 3 7




      But when I replace that with the dot(.) in 'j' with 'c' like below,



      DT[ , c(lapply(.SD, sum), .N), by = (cyl) ]


      the result comes in horizontal order.



      O/P:




      cyl mpg disp hp drat wt qsec vs am gear carb N



      6 138.2 1283.2 856 25.10 21.820 125.84 4 3 27 24 7



      4 293.3 1156.5 909 44.78 25.143 210.51 10 8 45 17 11



      8 211.4 4943.4 2929 45.21 55.989 234.81 0 2 46 49 14




      In another case, without lapply, exactly the opposite happens.



      DT[ , c(sum(mpg), .N), by = (cyl) ]


      gives the output vertically



      O/P:




      cyl V1



      1: 6 138.2



      2: 6 7.0



      3: 4 293.3



      4: 4 11.0



      5: 8 211.4



      6: 8 14.0




      whereas a dot(.) in 'j' gives the output horizonatlly.



      DT[ , .(sum(mpg), .N), by = (cyl) ]


      O/P:




      cyl V1 N



      1: 6 138.2 7



      2: 4 293.3 11



      3: 8 211.4 14




      Why does this happens? Why the result is ordered in such way?







      r data.table






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      Deb

      163




      163






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          DT[, .(sum(mpg), .N), by = (cyl) ] # equal, creates a list with 2 elements (2 columns)
          DT[, list(sum(mpg), .N), by = (cyl) ] # equal, to above

          DT[, c(sum(mpg), .N), by = (cyl) ] # creates a vector of length 2 (equal to 2 rows)



          another simplified example.



          DT[ , .(col1 = 1, col2 = 2), by = (cyl) ]
          DT[ , list(col1 = 1, col2 = 2), by = (cyl) ]

          DT[ , c(element1 = 1, element2 = 2), by = (cyl) ]



          To address your last point,



          DT[ , c(element1 = 1, element2 = 2, element3 = list(3)), by = (cyl) ]
          DT[ , c(element1 = 1, element2 = 2, element3 = 3 ), by = (cyl) ]


          You need to learn more about the c function.



          So as lapply (listapply) returns a list the c will add the .N as new LIST ELEMENT in c(lapply(.SD, sum), .N).



          So you end up with n list elements and therefore n columns.




          Just for fun:



          DT[ , c(lapply(.SD, sum), .N), by = (cyl) ]
          DT[ , c(sapply(.SD, sum), .N), by = (cyl) ] # sapply will simplify the result into a vector, therefore c() will combine into a vector and you end up with many rows.





          share|improve this answer






















          • @Deb see if this is enough explanation.
            – Andre Elrico
            yesterday










          • Thank you so much! This explains a lot. But, I still have one doubt. DT[ , (lapply(.SD, sum)), by = cyl ] returns a list with all columns as I understand but why does'DT[ , .(lapply(.SD, sum)), by = cyl ] returns the result in many rows when lapply should also return a list? What is the dot changing here?
            – Deb
            yesterday











          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',
          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%2f53224462%2fin-r-data-table-what-is-the-difference-between-taking-dot-and-c-in-j-while%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          DT[, .(sum(mpg), .N), by = (cyl) ] # equal, creates a list with 2 elements (2 columns)
          DT[, list(sum(mpg), .N), by = (cyl) ] # equal, to above

          DT[, c(sum(mpg), .N), by = (cyl) ] # creates a vector of length 2 (equal to 2 rows)



          another simplified example.



          DT[ , .(col1 = 1, col2 = 2), by = (cyl) ]
          DT[ , list(col1 = 1, col2 = 2), by = (cyl) ]

          DT[ , c(element1 = 1, element2 = 2), by = (cyl) ]



          To address your last point,



          DT[ , c(element1 = 1, element2 = 2, element3 = list(3)), by = (cyl) ]
          DT[ , c(element1 = 1, element2 = 2, element3 = 3 ), by = (cyl) ]


          You need to learn more about the c function.



          So as lapply (listapply) returns a list the c will add the .N as new LIST ELEMENT in c(lapply(.SD, sum), .N).



          So you end up with n list elements and therefore n columns.




          Just for fun:



          DT[ , c(lapply(.SD, sum), .N), by = (cyl) ]
          DT[ , c(sapply(.SD, sum), .N), by = (cyl) ] # sapply will simplify the result into a vector, therefore c() will combine into a vector and you end up with many rows.





          share|improve this answer






















          • @Deb see if this is enough explanation.
            – Andre Elrico
            yesterday










          • Thank you so much! This explains a lot. But, I still have one doubt. DT[ , (lapply(.SD, sum)), by = cyl ] returns a list with all columns as I understand but why does'DT[ , .(lapply(.SD, sum)), by = cyl ] returns the result in many rows when lapply should also return a list? What is the dot changing here?
            – Deb
            yesterday















          up vote
          1
          down vote



          accepted










          DT[, .(sum(mpg), .N), by = (cyl) ] # equal, creates a list with 2 elements (2 columns)
          DT[, list(sum(mpg), .N), by = (cyl) ] # equal, to above

          DT[, c(sum(mpg), .N), by = (cyl) ] # creates a vector of length 2 (equal to 2 rows)



          another simplified example.



          DT[ , .(col1 = 1, col2 = 2), by = (cyl) ]
          DT[ , list(col1 = 1, col2 = 2), by = (cyl) ]

          DT[ , c(element1 = 1, element2 = 2), by = (cyl) ]



          To address your last point,



          DT[ , c(element1 = 1, element2 = 2, element3 = list(3)), by = (cyl) ]
          DT[ , c(element1 = 1, element2 = 2, element3 = 3 ), by = (cyl) ]


          You need to learn more about the c function.



          So as lapply (listapply) returns a list the c will add the .N as new LIST ELEMENT in c(lapply(.SD, sum), .N).



          So you end up with n list elements and therefore n columns.




          Just for fun:



          DT[ , c(lapply(.SD, sum), .N), by = (cyl) ]
          DT[ , c(sapply(.SD, sum), .N), by = (cyl) ] # sapply will simplify the result into a vector, therefore c() will combine into a vector and you end up with many rows.





          share|improve this answer






















          • @Deb see if this is enough explanation.
            – Andre Elrico
            yesterday










          • Thank you so much! This explains a lot. But, I still have one doubt. DT[ , (lapply(.SD, sum)), by = cyl ] returns a list with all columns as I understand but why does'DT[ , .(lapply(.SD, sum)), by = cyl ] returns the result in many rows when lapply should also return a list? What is the dot changing here?
            – Deb
            yesterday













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          DT[, .(sum(mpg), .N), by = (cyl) ] # equal, creates a list with 2 elements (2 columns)
          DT[, list(sum(mpg), .N), by = (cyl) ] # equal, to above

          DT[, c(sum(mpg), .N), by = (cyl) ] # creates a vector of length 2 (equal to 2 rows)



          another simplified example.



          DT[ , .(col1 = 1, col2 = 2), by = (cyl) ]
          DT[ , list(col1 = 1, col2 = 2), by = (cyl) ]

          DT[ , c(element1 = 1, element2 = 2), by = (cyl) ]



          To address your last point,



          DT[ , c(element1 = 1, element2 = 2, element3 = list(3)), by = (cyl) ]
          DT[ , c(element1 = 1, element2 = 2, element3 = 3 ), by = (cyl) ]


          You need to learn more about the c function.



          So as lapply (listapply) returns a list the c will add the .N as new LIST ELEMENT in c(lapply(.SD, sum), .N).



          So you end up with n list elements and therefore n columns.




          Just for fun:



          DT[ , c(lapply(.SD, sum), .N), by = (cyl) ]
          DT[ , c(sapply(.SD, sum), .N), by = (cyl) ] # sapply will simplify the result into a vector, therefore c() will combine into a vector and you end up with many rows.





          share|improve this answer














          DT[, .(sum(mpg), .N), by = (cyl) ] # equal, creates a list with 2 elements (2 columns)
          DT[, list(sum(mpg), .N), by = (cyl) ] # equal, to above

          DT[, c(sum(mpg), .N), by = (cyl) ] # creates a vector of length 2 (equal to 2 rows)



          another simplified example.



          DT[ , .(col1 = 1, col2 = 2), by = (cyl) ]
          DT[ , list(col1 = 1, col2 = 2), by = (cyl) ]

          DT[ , c(element1 = 1, element2 = 2), by = (cyl) ]



          To address your last point,



          DT[ , c(element1 = 1, element2 = 2, element3 = list(3)), by = (cyl) ]
          DT[ , c(element1 = 1, element2 = 2, element3 = 3 ), by = (cyl) ]


          You need to learn more about the c function.



          So as lapply (listapply) returns a list the c will add the .N as new LIST ELEMENT in c(lapply(.SD, sum), .N).



          So you end up with n list elements and therefore n columns.




          Just for fun:



          DT[ , c(lapply(.SD, sum), .N), by = (cyl) ]
          DT[ , c(sapply(.SD, sum), .N), by = (cyl) ] # sapply will simplify the result into a vector, therefore c() will combine into a vector and you end up with many rows.






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited yesterday

























          answered yesterday









          Andre Elrico

          4,4971827




          4,4971827











          • @Deb see if this is enough explanation.
            – Andre Elrico
            yesterday










          • Thank you so much! This explains a lot. But, I still have one doubt. DT[ , (lapply(.SD, sum)), by = cyl ] returns a list with all columns as I understand but why does'DT[ , .(lapply(.SD, sum)), by = cyl ] returns the result in many rows when lapply should also return a list? What is the dot changing here?
            – Deb
            yesterday

















          • @Deb see if this is enough explanation.
            – Andre Elrico
            yesterday










          • Thank you so much! This explains a lot. But, I still have one doubt. DT[ , (lapply(.SD, sum)), by = cyl ] returns a list with all columns as I understand but why does'DT[ , .(lapply(.SD, sum)), by = cyl ] returns the result in many rows when lapply should also return a list? What is the dot changing here?
            – Deb
            yesterday
















          @Deb see if this is enough explanation.
          – Andre Elrico
          yesterday




          @Deb see if this is enough explanation.
          – Andre Elrico
          yesterday












          Thank you so much! This explains a lot. But, I still have one doubt. DT[ , (lapply(.SD, sum)), by = cyl ] returns a list with all columns as I understand but why does'DT[ , .(lapply(.SD, sum)), by = cyl ] returns the result in many rows when lapply should also return a list? What is the dot changing here?
          – Deb
          yesterday





          Thank you so much! This explains a lot. But, I still have one doubt. DT[ , (lapply(.SD, sum)), by = cyl ] returns a list with all columns as I understand but why does'DT[ , .(lapply(.SD, sum)), by = cyl ] returns the result in many rows when lapply should also return a list? What is the dot changing here?
          – Deb
          yesterday


















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224462%2fin-r-data-table-what-is-the-difference-between-taking-dot-and-c-in-j-while%23new-answer', 'question_page');

          );

          Post as a guest














































































          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo