How to logical compare two data frames in R











up vote
0
down vote

favorite












library(data.table)
library(QuantTools)

date_from <- '2018-11-01'
date_to <- '2018-11-30'
ticker <- 'SPFB.RTS'

# get days
dataDaily <- get_finam_data(ticker, date_from, date_to, 'day')
# get hours
dataHourly <- get_finam_data(ticker, date_from, date_to, 'hour')

# percent change of the day
dataDaily$pc <- ((dataDaily$close - dataDaily$open)/dataDaily$open)*100

# mark days with > 2 percent change
dataDaily$isBigCh <- dataDaily$pc[dataDaily$pc > 2]


So, I have a code above which downloads a daily/hourly OHLC data of the futures.



Questions:
1) How can I move the marks from dataDaily$isBigCh to dataHourly? It seems not easy because these data frames have different time formats and different lengths of rows.



dataHourly$time # has a format like this 2018-11-09 23:00:00
dataDaily$date # has a format like this 2018-11-09


2) How can I select the first bar of the day in dataHourly$time?










share|improve this question
























  • I guess something like dataHourly$date <- as.Date(dataHourly$time)
    – Frank
    Nov 10 at 10:20















up vote
0
down vote

favorite












library(data.table)
library(QuantTools)

date_from <- '2018-11-01'
date_to <- '2018-11-30'
ticker <- 'SPFB.RTS'

# get days
dataDaily <- get_finam_data(ticker, date_from, date_to, 'day')
# get hours
dataHourly <- get_finam_data(ticker, date_from, date_to, 'hour')

# percent change of the day
dataDaily$pc <- ((dataDaily$close - dataDaily$open)/dataDaily$open)*100

# mark days with > 2 percent change
dataDaily$isBigCh <- dataDaily$pc[dataDaily$pc > 2]


So, I have a code above which downloads a daily/hourly OHLC data of the futures.



Questions:
1) How can I move the marks from dataDaily$isBigCh to dataHourly? It seems not easy because these data frames have different time formats and different lengths of rows.



dataHourly$time # has a format like this 2018-11-09 23:00:00
dataDaily$date # has a format like this 2018-11-09


2) How can I select the first bar of the day in dataHourly$time?










share|improve this question
























  • I guess something like dataHourly$date <- as.Date(dataHourly$time)
    – Frank
    Nov 10 at 10:20













up vote
0
down vote

favorite









up vote
0
down vote

favorite











library(data.table)
library(QuantTools)

date_from <- '2018-11-01'
date_to <- '2018-11-30'
ticker <- 'SPFB.RTS'

# get days
dataDaily <- get_finam_data(ticker, date_from, date_to, 'day')
# get hours
dataHourly <- get_finam_data(ticker, date_from, date_to, 'hour')

# percent change of the day
dataDaily$pc <- ((dataDaily$close - dataDaily$open)/dataDaily$open)*100

# mark days with > 2 percent change
dataDaily$isBigCh <- dataDaily$pc[dataDaily$pc > 2]


So, I have a code above which downloads a daily/hourly OHLC data of the futures.



Questions:
1) How can I move the marks from dataDaily$isBigCh to dataHourly? It seems not easy because these data frames have different time formats and different lengths of rows.



dataHourly$time # has a format like this 2018-11-09 23:00:00
dataDaily$date # has a format like this 2018-11-09


2) How can I select the first bar of the day in dataHourly$time?










share|improve this question















library(data.table)
library(QuantTools)

date_from <- '2018-11-01'
date_to <- '2018-11-30'
ticker <- 'SPFB.RTS'

# get days
dataDaily <- get_finam_data(ticker, date_from, date_to, 'day')
# get hours
dataHourly <- get_finam_data(ticker, date_from, date_to, 'hour')

# percent change of the day
dataDaily$pc <- ((dataDaily$close - dataDaily$open)/dataDaily$open)*100

# mark days with > 2 percent change
dataDaily$isBigCh <- dataDaily$pc[dataDaily$pc > 2]


So, I have a code above which downloads a daily/hourly OHLC data of the futures.



Questions:
1) How can I move the marks from dataDaily$isBigCh to dataHourly? It seems not easy because these data frames have different time formats and different lengths of rows.



dataHourly$time # has a format like this 2018-11-09 23:00:00
dataDaily$date # has a format like this 2018-11-09


2) How can I select the first bar of the day in dataHourly$time?







r data.table






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 at 20:46









Alp Arıbal

324




324










asked Nov 9 at 19:50









Marsel

32




32












  • I guess something like dataHourly$date <- as.Date(dataHourly$time)
    – Frank
    Nov 10 at 10:20


















  • I guess something like dataHourly$date <- as.Date(dataHourly$time)
    – Frank
    Nov 10 at 10:20
















I guess something like dataHourly$date <- as.Date(dataHourly$time)
– Frank
Nov 10 at 10:20




I guess something like dataHourly$date <- as.Date(dataHourly$time)
– Frank
Nov 10 at 10:20












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Slightly modified code for readability



# percent change of the day
dataDaily[, price_change := ( close / open - 1 ) * 100 ]

# mark days with > 2 percent change
dataDaily[, isBigCh := price_change > 2 ]


Question 1



# add date column to hourly data
# note that 00:00 time corresponds to 23:00-00:00 candle
dataHourly[, date := as.Date( time - as.difftime( '01:00:00' ) ) ]

# copy dataDaily isBigCh to dataHourly isBigChDaily
dataHourly[ dataDaily, isBigChDaily := isBigCh, on = 'date' ]


Question 2



# select first bar of the day
dataHourly[, .SD[1], by = date ]


Optionally



# remove date column from hourly data
dataHourly[, date := NULL ]


Note




  • library(data.table) not necessary as QuantTools loads it automatically


  • please read data.table manual it will save you lots of time trying to figure out simple manipulations similar to what you asked







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',
    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%2f53232415%2fhow-to-logical-compare-two-data-frames-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








    up vote
    0
    down vote



    accepted










    Slightly modified code for readability



    # percent change of the day
    dataDaily[, price_change := ( close / open - 1 ) * 100 ]

    # mark days with > 2 percent change
    dataDaily[, isBigCh := price_change > 2 ]


    Question 1



    # add date column to hourly data
    # note that 00:00 time corresponds to 23:00-00:00 candle
    dataHourly[, date := as.Date( time - as.difftime( '01:00:00' ) ) ]

    # copy dataDaily isBigCh to dataHourly isBigChDaily
    dataHourly[ dataDaily, isBigChDaily := isBigCh, on = 'date' ]


    Question 2



    # select first bar of the day
    dataHourly[, .SD[1], by = date ]


    Optionally



    # remove date column from hourly data
    dataHourly[, date := NULL ]


    Note




    • library(data.table) not necessary as QuantTools loads it automatically


    • please read data.table manual it will save you lots of time trying to figure out simple manipulations similar to what you asked







    share|improve this answer

























      up vote
      0
      down vote



      accepted










      Slightly modified code for readability



      # percent change of the day
      dataDaily[, price_change := ( close / open - 1 ) * 100 ]

      # mark days with > 2 percent change
      dataDaily[, isBigCh := price_change > 2 ]


      Question 1



      # add date column to hourly data
      # note that 00:00 time corresponds to 23:00-00:00 candle
      dataHourly[, date := as.Date( time - as.difftime( '01:00:00' ) ) ]

      # copy dataDaily isBigCh to dataHourly isBigChDaily
      dataHourly[ dataDaily, isBigChDaily := isBigCh, on = 'date' ]


      Question 2



      # select first bar of the day
      dataHourly[, .SD[1], by = date ]


      Optionally



      # remove date column from hourly data
      dataHourly[, date := NULL ]


      Note




      • library(data.table) not necessary as QuantTools loads it automatically


      • please read data.table manual it will save you lots of time trying to figure out simple manipulations similar to what you asked







      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        Slightly modified code for readability



        # percent change of the day
        dataDaily[, price_change := ( close / open - 1 ) * 100 ]

        # mark days with > 2 percent change
        dataDaily[, isBigCh := price_change > 2 ]


        Question 1



        # add date column to hourly data
        # note that 00:00 time corresponds to 23:00-00:00 candle
        dataHourly[, date := as.Date( time - as.difftime( '01:00:00' ) ) ]

        # copy dataDaily isBigCh to dataHourly isBigChDaily
        dataHourly[ dataDaily, isBigChDaily := isBigCh, on = 'date' ]


        Question 2



        # select first bar of the day
        dataHourly[, .SD[1], by = date ]


        Optionally



        # remove date column from hourly data
        dataHourly[, date := NULL ]


        Note




        • library(data.table) not necessary as QuantTools loads it automatically


        • please read data.table manual it will save you lots of time trying to figure out simple manipulations similar to what you asked







        share|improve this answer












        Slightly modified code for readability



        # percent change of the day
        dataDaily[, price_change := ( close / open - 1 ) * 100 ]

        # mark days with > 2 percent change
        dataDaily[, isBigCh := price_change > 2 ]


        Question 1



        # add date column to hourly data
        # note that 00:00 time corresponds to 23:00-00:00 candle
        dataHourly[, date := as.Date( time - as.difftime( '01:00:00' ) ) ]

        # copy dataDaily isBigCh to dataHourly isBigChDaily
        dataHourly[ dataDaily, isBigChDaily := isBigCh, on = 'date' ]


        Question 2



        # select first bar of the day
        dataHourly[, .SD[1], by = date ]


        Optionally



        # remove date column from hourly data
        dataHourly[, date := NULL ]


        Note




        • library(data.table) not necessary as QuantTools loads it automatically


        • please read data.table manual it will save you lots of time trying to figure out simple manipulations similar to what you asked








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 10:11









        sokovalevsky

        16




        16






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53232415%2fhow-to-logical-compare-two-data-frames-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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini