How to add multiple columns using lapply? [duplicate]










0
















This question already has an answer here:



  • Split text string in a data.table columns

    5 answers



I have a data table as below:



dt=data.table(
id=c(1,2,3,4,5,6),
date=c("28 Jul 2009","31 Jul 2009","31 Jul 2009","04 Aug 2009","10 Aug 2009","06 Aug 2009")
)


I want to add three more columns day,month,year to this column.So I tried



dt[,day:=unlist(strsplit(date,"\ "))[1]]
dt[,month:=unlist(strsplit(date,"\ "))[2]]
dt[,year:=unlist(strsplit(date,"\ "))[3]]


But this just appended all columns with same split for the first row.



SoI tried



dt[,day:=lapply(date,function (x) unlist(strsplit(x,"\ "))[1]),]
dt[,month:=lapply(date,function (x) unlist(strsplit(x,"\ "))[2]),]
dt[,year:=lapply(date,function (x) unlist(strsplit(x,"\ "))[3]),]


which worked. But how do I combine all in one line instead of 3 lines and any other efficeint methods.Any help is appreciated










share|improve this question













marked as duplicate by Henrik r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 15 '18 at 7:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






















    0
















    This question already has an answer here:



    • Split text string in a data.table columns

      5 answers



    I have a data table as below:



    dt=data.table(
    id=c(1,2,3,4,5,6),
    date=c("28 Jul 2009","31 Jul 2009","31 Jul 2009","04 Aug 2009","10 Aug 2009","06 Aug 2009")
    )


    I want to add three more columns day,month,year to this column.So I tried



    dt[,day:=unlist(strsplit(date,"\ "))[1]]
    dt[,month:=unlist(strsplit(date,"\ "))[2]]
    dt[,year:=unlist(strsplit(date,"\ "))[3]]


    But this just appended all columns with same split for the first row.



    SoI tried



    dt[,day:=lapply(date,function (x) unlist(strsplit(x,"\ "))[1]),]
    dt[,month:=lapply(date,function (x) unlist(strsplit(x,"\ "))[2]),]
    dt[,year:=lapply(date,function (x) unlist(strsplit(x,"\ "))[3]),]


    which worked. But how do I combine all in one line instead of 3 lines and any other efficeint methods.Any help is appreciated










    share|improve this question













    marked as duplicate by Henrik r
    Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

    StackExchange.ready(function()
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function()
    $hover.showInfoMessage('',
    messageElement: $msg.clone().show(),
    transient: false,
    position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
    dismissable: false,
    relativeToBody: true
    );
    ,
    function()
    StackExchange.helpers.removeMessages();

    );
    );
    );
    Nov 15 '18 at 7:21


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















      0












      0








      0









      This question already has an answer here:



      • Split text string in a data.table columns

        5 answers



      I have a data table as below:



      dt=data.table(
      id=c(1,2,3,4,5,6),
      date=c("28 Jul 2009","31 Jul 2009","31 Jul 2009","04 Aug 2009","10 Aug 2009","06 Aug 2009")
      )


      I want to add three more columns day,month,year to this column.So I tried



      dt[,day:=unlist(strsplit(date,"\ "))[1]]
      dt[,month:=unlist(strsplit(date,"\ "))[2]]
      dt[,year:=unlist(strsplit(date,"\ "))[3]]


      But this just appended all columns with same split for the first row.



      SoI tried



      dt[,day:=lapply(date,function (x) unlist(strsplit(x,"\ "))[1]),]
      dt[,month:=lapply(date,function (x) unlist(strsplit(x,"\ "))[2]),]
      dt[,year:=lapply(date,function (x) unlist(strsplit(x,"\ "))[3]),]


      which worked. But how do I combine all in one line instead of 3 lines and any other efficeint methods.Any help is appreciated










      share|improve this question















      This question already has an answer here:



      • Split text string in a data.table columns

        5 answers



      I have a data table as below:



      dt=data.table(
      id=c(1,2,3,4,5,6),
      date=c("28 Jul 2009","31 Jul 2009","31 Jul 2009","04 Aug 2009","10 Aug 2009","06 Aug 2009")
      )


      I want to add three more columns day,month,year to this column.So I tried



      dt[,day:=unlist(strsplit(date,"\ "))[1]]
      dt[,month:=unlist(strsplit(date,"\ "))[2]]
      dt[,year:=unlist(strsplit(date,"\ "))[3]]


      But this just appended all columns with same split for the first row.



      SoI tried



      dt[,day:=lapply(date,function (x) unlist(strsplit(x,"\ "))[1]),]
      dt[,month:=lapply(date,function (x) unlist(strsplit(x,"\ "))[2]),]
      dt[,year:=lapply(date,function (x) unlist(strsplit(x,"\ "))[3]),]


      which worked. But how do I combine all in one line instead of 3 lines and any other efficeint methods.Any help is appreciated





      This question already has an answer here:



      • Split text string in a data.table columns

        5 answers







      r dplyr data.table






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 4:50









      RickyRicky

      7371429




      7371429




      marked as duplicate by Henrik r
      Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Nov 15 '18 at 7:21


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by Henrik r
      Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Nov 15 '18 at 7:21


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























          3 Answers
          3






          active

          oldest

          votes


















          4














          You want tstrsplit(), also from the data table package. From help(tstrsplit), it is "a convenient wrapper function to split a column using strsplit and assign the transposed result to individual columns."



          dt[, c("day", "month", "year") := tstrsplit(date, " ")]

          dt
          # id date day month year
          # 1: 1 28 Jul 2009 28 Jul 2009
          # 2: 2 31 Jul 2009 31 Jul 2009
          # 3: 3 31 Jul 2009 31 Jul 2009
          # 4: 4 04 Aug 2009 04 Aug 2009
          # 5: 5 10 Aug 2009 10 Aug 2009
          # 6: 6 06 Aug 2009 06 Aug 2009





          share|improve this answer
































            2














            You can easily do it using library lubridate and dplyr



            library(data.table)
            dt=data.table(
            id=c(1,2,3,4,5,6),
            date=c("28 Jul 2009","31 Jul 2009","31 Jul 2009","04 Aug 2009","10 Aug 2009","06 Aug 2009")
            )
            str(dt)

            library(lubridate)
            dt$date<- dmy(dt$date)
            library(dplyr)
            mutate(dt, day= day(dt$date),month = month(dt$date),year = year(dt$date))





            share|improve this answer






























              2














              The issue is that your [1] indexing is taking the first of 18 elements, not the first of each of a list of 6. Try this:



              dt[,day := sapply(strsplit(date, "\s"), `[[`, 1),]
              dt
              # id date day
              # 1: 1 28 Jul 2009 28
              # 2: 2 31 Jul 2009 31
              # 3: 3 31 Jul 2009 31
              # 4: 4 04 Aug 2009 04
              # 5: 5 10 Aug 2009 10
              # 6: 6 06 Aug 2009 06





              share|improve this answer





























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                4














                You want tstrsplit(), also from the data table package. From help(tstrsplit), it is "a convenient wrapper function to split a column using strsplit and assign the transposed result to individual columns."



                dt[, c("day", "month", "year") := tstrsplit(date, " ")]

                dt
                # id date day month year
                # 1: 1 28 Jul 2009 28 Jul 2009
                # 2: 2 31 Jul 2009 31 Jul 2009
                # 3: 3 31 Jul 2009 31 Jul 2009
                # 4: 4 04 Aug 2009 04 Aug 2009
                # 5: 5 10 Aug 2009 10 Aug 2009
                # 6: 6 06 Aug 2009 06 Aug 2009





                share|improve this answer





























                  4














                  You want tstrsplit(), also from the data table package. From help(tstrsplit), it is "a convenient wrapper function to split a column using strsplit and assign the transposed result to individual columns."



                  dt[, c("day", "month", "year") := tstrsplit(date, " ")]

                  dt
                  # id date day month year
                  # 1: 1 28 Jul 2009 28 Jul 2009
                  # 2: 2 31 Jul 2009 31 Jul 2009
                  # 3: 3 31 Jul 2009 31 Jul 2009
                  # 4: 4 04 Aug 2009 04 Aug 2009
                  # 5: 5 10 Aug 2009 10 Aug 2009
                  # 6: 6 06 Aug 2009 06 Aug 2009





                  share|improve this answer



























                    4












                    4








                    4







                    You want tstrsplit(), also from the data table package. From help(tstrsplit), it is "a convenient wrapper function to split a column using strsplit and assign the transposed result to individual columns."



                    dt[, c("day", "month", "year") := tstrsplit(date, " ")]

                    dt
                    # id date day month year
                    # 1: 1 28 Jul 2009 28 Jul 2009
                    # 2: 2 31 Jul 2009 31 Jul 2009
                    # 3: 3 31 Jul 2009 31 Jul 2009
                    # 4: 4 04 Aug 2009 04 Aug 2009
                    # 5: 5 10 Aug 2009 10 Aug 2009
                    # 6: 6 06 Aug 2009 06 Aug 2009





                    share|improve this answer















                    You want tstrsplit(), also from the data table package. From help(tstrsplit), it is "a convenient wrapper function to split a column using strsplit and assign the transposed result to individual columns."



                    dt[, c("day", "month", "year") := tstrsplit(date, " ")]

                    dt
                    # id date day month year
                    # 1: 1 28 Jul 2009 28 Jul 2009
                    # 2: 2 31 Jul 2009 31 Jul 2009
                    # 3: 3 31 Jul 2009 31 Jul 2009
                    # 4: 4 04 Aug 2009 04 Aug 2009
                    # 5: 5 10 Aug 2009 10 Aug 2009
                    # 6: 6 06 Aug 2009 06 Aug 2009






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 15 '18 at 5:03

























                    answered Nov 15 '18 at 4:58









                    Rich ScrivenRich Scriven

                    77.5k8104173




                    77.5k8104173























                        2














                        You can easily do it using library lubridate and dplyr



                        library(data.table)
                        dt=data.table(
                        id=c(1,2,3,4,5,6),
                        date=c("28 Jul 2009","31 Jul 2009","31 Jul 2009","04 Aug 2009","10 Aug 2009","06 Aug 2009")
                        )
                        str(dt)

                        library(lubridate)
                        dt$date<- dmy(dt$date)
                        library(dplyr)
                        mutate(dt, day= day(dt$date),month = month(dt$date),year = year(dt$date))





                        share|improve this answer



























                          2














                          You can easily do it using library lubridate and dplyr



                          library(data.table)
                          dt=data.table(
                          id=c(1,2,3,4,5,6),
                          date=c("28 Jul 2009","31 Jul 2009","31 Jul 2009","04 Aug 2009","10 Aug 2009","06 Aug 2009")
                          )
                          str(dt)

                          library(lubridate)
                          dt$date<- dmy(dt$date)
                          library(dplyr)
                          mutate(dt, day= day(dt$date),month = month(dt$date),year = year(dt$date))





                          share|improve this answer

























                            2












                            2








                            2







                            You can easily do it using library lubridate and dplyr



                            library(data.table)
                            dt=data.table(
                            id=c(1,2,3,4,5,6),
                            date=c("28 Jul 2009","31 Jul 2009","31 Jul 2009","04 Aug 2009","10 Aug 2009","06 Aug 2009")
                            )
                            str(dt)

                            library(lubridate)
                            dt$date<- dmy(dt$date)
                            library(dplyr)
                            mutate(dt, day= day(dt$date),month = month(dt$date),year = year(dt$date))





                            share|improve this answer













                            You can easily do it using library lubridate and dplyr



                            library(data.table)
                            dt=data.table(
                            id=c(1,2,3,4,5,6),
                            date=c("28 Jul 2009","31 Jul 2009","31 Jul 2009","04 Aug 2009","10 Aug 2009","06 Aug 2009")
                            )
                            str(dt)

                            library(lubridate)
                            dt$date<- dmy(dt$date)
                            library(dplyr)
                            mutate(dt, day= day(dt$date),month = month(dt$date),year = year(dt$date))






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 15 '18 at 4:55









                            HunaidkhanHunaidkhan

                            1,014516




                            1,014516





















                                2














                                The issue is that your [1] indexing is taking the first of 18 elements, not the first of each of a list of 6. Try this:



                                dt[,day := sapply(strsplit(date, "\s"), `[[`, 1),]
                                dt
                                # id date day
                                # 1: 1 28 Jul 2009 28
                                # 2: 2 31 Jul 2009 31
                                # 3: 3 31 Jul 2009 31
                                # 4: 4 04 Aug 2009 04
                                # 5: 5 10 Aug 2009 10
                                # 6: 6 06 Aug 2009 06





                                share|improve this answer



























                                  2














                                  The issue is that your [1] indexing is taking the first of 18 elements, not the first of each of a list of 6. Try this:



                                  dt[,day := sapply(strsplit(date, "\s"), `[[`, 1),]
                                  dt
                                  # id date day
                                  # 1: 1 28 Jul 2009 28
                                  # 2: 2 31 Jul 2009 31
                                  # 3: 3 31 Jul 2009 31
                                  # 4: 4 04 Aug 2009 04
                                  # 5: 5 10 Aug 2009 10
                                  # 6: 6 06 Aug 2009 06





                                  share|improve this answer

























                                    2












                                    2








                                    2







                                    The issue is that your [1] indexing is taking the first of 18 elements, not the first of each of a list of 6. Try this:



                                    dt[,day := sapply(strsplit(date, "\s"), `[[`, 1),]
                                    dt
                                    # id date day
                                    # 1: 1 28 Jul 2009 28
                                    # 2: 2 31 Jul 2009 31
                                    # 3: 3 31 Jul 2009 31
                                    # 4: 4 04 Aug 2009 04
                                    # 5: 5 10 Aug 2009 10
                                    # 6: 6 06 Aug 2009 06





                                    share|improve this answer













                                    The issue is that your [1] indexing is taking the first of 18 elements, not the first of each of a list of 6. Try this:



                                    dt[,day := sapply(strsplit(date, "\s"), `[[`, 1),]
                                    dt
                                    # id date day
                                    # 1: 1 28 Jul 2009 28
                                    # 2: 2 31 Jul 2009 31
                                    # 3: 3 31 Jul 2009 31
                                    # 4: 4 04 Aug 2009 04
                                    # 5: 5 10 Aug 2009 10
                                    # 6: 6 06 Aug 2009 06






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 15 '18 at 4:59









                                    r2evansr2evans

                                    28k33159




                                    28k33159













                                        Popular posts from this blog

                                        Use pre created SQLite database for Android project in kotlin

                                        Darth Vader #20

                                        Ondo