why can't subset data frame by some keys?











up vote
0
down vote

favorite












such data frame as follows:



df <- data.frame(yr = rep(2000:2017,each=46),
dayorder = rep(seq(1,365,8),time=18),
fid = rep(1:46,time=18),
Value = runif(46*18)
)


when I sieve by keys:



df[which(dayorder==9),]
yr dayorder fid Value
2 2000 9 2 0.3424053
48 2001 9 2 0.6639720
94 2002 9 2 0.5530076
140 2003 9 2 0.9757845
.....


When use 'yr' key ,report error:



df[which(yr==2001),]
Error in which(yr == 2001) : object 'yr' not found


same error by 'fid' key"



df[which(fid==2),]
Error in which(fid == 2) : object 'fid' not found


but df$yr is existed:



df$yr
[1] 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000
.....


What's the problem of data frame?










share|improve this question




























    up vote
    0
    down vote

    favorite












    such data frame as follows:



    df <- data.frame(yr = rep(2000:2017,each=46),
    dayorder = rep(seq(1,365,8),time=18),
    fid = rep(1:46,time=18),
    Value = runif(46*18)
    )


    when I sieve by keys:



    df[which(dayorder==9),]
    yr dayorder fid Value
    2 2000 9 2 0.3424053
    48 2001 9 2 0.6639720
    94 2002 9 2 0.5530076
    140 2003 9 2 0.9757845
    .....


    When use 'yr' key ,report error:



    df[which(yr==2001),]
    Error in which(yr == 2001) : object 'yr' not found


    same error by 'fid' key"



    df[which(fid==2),]
    Error in which(fid == 2) : object 'fid' not found


    but df$yr is existed:



    df$yr
    [1] 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000
    .....


    What's the problem of data frame?










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      such data frame as follows:



      df <- data.frame(yr = rep(2000:2017,each=46),
      dayorder = rep(seq(1,365,8),time=18),
      fid = rep(1:46,time=18),
      Value = runif(46*18)
      )


      when I sieve by keys:



      df[which(dayorder==9),]
      yr dayorder fid Value
      2 2000 9 2 0.3424053
      48 2001 9 2 0.6639720
      94 2002 9 2 0.5530076
      140 2003 9 2 0.9757845
      .....


      When use 'yr' key ,report error:



      df[which(yr==2001),]
      Error in which(yr == 2001) : object 'yr' not found


      same error by 'fid' key"



      df[which(fid==2),]
      Error in which(fid == 2) : object 'fid' not found


      but df$yr is existed:



      df$yr
      [1] 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000
      .....


      What's the problem of data frame?










      share|improve this question















      such data frame as follows:



      df <- data.frame(yr = rep(2000:2017,each=46),
      dayorder = rep(seq(1,365,8),time=18),
      fid = rep(1:46,time=18),
      Value = runif(46*18)
      )


      when I sieve by keys:



      df[which(dayorder==9),]
      yr dayorder fid Value
      2 2000 9 2 0.3424053
      48 2001 9 2 0.6639720
      94 2002 9 2 0.5530076
      140 2003 9 2 0.9757845
      .....


      When use 'yr' key ,report error:



      df[which(yr==2001),]
      Error in which(yr == 2001) : object 'yr' not found


      same error by 'fid' key"



      df[which(fid==2),]
      Error in which(fid == 2) : object 'fid' not found


      but df$yr is existed:



      df$yr
      [1] 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000
      .....


      What's the problem of data frame?







      r






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited yesterday

























      asked yesterday









      Cobin

      16411




      16411
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          You need to add df$ before the column name, in the first case you don't get an error probably because dayorder is a variable defined in your environment.



          It should've thrown an error like the others.



          df[which(df$dayorder==9),] # or df[df$dayorder==9,] 


          When I call like this df[which(df$dayorder==9),] I get:




          Error in which(dayorder == 9) : object 'dayorder' not found




          Another solution using with, avoids too many df$ (also note you can remove which():



          with(df, df[dayorder==9, ])





          share|improve this answer





















          • lol reason for the downvote? you people need to grow up.
            – RLave
            21 hours ago


















          up vote
          0
          down vote













          you can try a tidyverse as well



          library(tidyverse)
          filter(df, yr == 2001)





          share|improve this answer





















          • or base R: subset(df, yr == 2001)
            – snoram
            yesterday










          • Yes of course...but IMO should be included in the other answer as a baseR solution ;)
            – Jimbou
            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%2f53372098%2fwhy-cant-subset-data-frame-by-some-keys%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








          up vote
          2
          down vote



          accepted










          You need to add df$ before the column name, in the first case you don't get an error probably because dayorder is a variable defined in your environment.



          It should've thrown an error like the others.



          df[which(df$dayorder==9),] # or df[df$dayorder==9,] 


          When I call like this df[which(df$dayorder==9),] I get:




          Error in which(dayorder == 9) : object 'dayorder' not found




          Another solution using with, avoids too many df$ (also note you can remove which():



          with(df, df[dayorder==9, ])





          share|improve this answer





















          • lol reason for the downvote? you people need to grow up.
            – RLave
            21 hours ago















          up vote
          2
          down vote



          accepted










          You need to add df$ before the column name, in the first case you don't get an error probably because dayorder is a variable defined in your environment.



          It should've thrown an error like the others.



          df[which(df$dayorder==9),] # or df[df$dayorder==9,] 


          When I call like this df[which(df$dayorder==9),] I get:




          Error in which(dayorder == 9) : object 'dayorder' not found




          Another solution using with, avoids too many df$ (also note you can remove which():



          with(df, df[dayorder==9, ])





          share|improve this answer





















          • lol reason for the downvote? you people need to grow up.
            – RLave
            21 hours ago













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          You need to add df$ before the column name, in the first case you don't get an error probably because dayorder is a variable defined in your environment.



          It should've thrown an error like the others.



          df[which(df$dayorder==9),] # or df[df$dayorder==9,] 


          When I call like this df[which(df$dayorder==9),] I get:




          Error in which(dayorder == 9) : object 'dayorder' not found




          Another solution using with, avoids too many df$ (also note you can remove which():



          with(df, df[dayorder==9, ])





          share|improve this answer












          You need to add df$ before the column name, in the first case you don't get an error probably because dayorder is a variable defined in your environment.



          It should've thrown an error like the others.



          df[which(df$dayorder==9),] # or df[df$dayorder==9,] 


          When I call like this df[which(df$dayorder==9),] I get:




          Error in which(dayorder == 9) : object 'dayorder' not found




          Another solution using with, avoids too many df$ (also note you can remove which():



          with(df, df[dayorder==9, ])






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          RLave

          2,8141820




          2,8141820












          • lol reason for the downvote? you people need to grow up.
            – RLave
            21 hours ago


















          • lol reason for the downvote? you people need to grow up.
            – RLave
            21 hours ago
















          lol reason for the downvote? you people need to grow up.
          – RLave
          21 hours ago




          lol reason for the downvote? you people need to grow up.
          – RLave
          21 hours ago












          up vote
          0
          down vote













          you can try a tidyverse as well



          library(tidyverse)
          filter(df, yr == 2001)





          share|improve this answer





















          • or base R: subset(df, yr == 2001)
            – snoram
            yesterday










          • Yes of course...but IMO should be included in the other answer as a baseR solution ;)
            – Jimbou
            yesterday















          up vote
          0
          down vote













          you can try a tidyverse as well



          library(tidyverse)
          filter(df, yr == 2001)





          share|improve this answer





















          • or base R: subset(df, yr == 2001)
            – snoram
            yesterday










          • Yes of course...but IMO should be included in the other answer as a baseR solution ;)
            – Jimbou
            yesterday













          up vote
          0
          down vote










          up vote
          0
          down vote









          you can try a tidyverse as well



          library(tidyverse)
          filter(df, yr == 2001)





          share|improve this answer












          you can try a tidyverse as well



          library(tidyverse)
          filter(df, yr == 2001)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          Jimbou

          9,45611230




          9,45611230












          • or base R: subset(df, yr == 2001)
            – snoram
            yesterday










          • Yes of course...but IMO should be included in the other answer as a baseR solution ;)
            – Jimbou
            yesterday


















          • or base R: subset(df, yr == 2001)
            – snoram
            yesterday










          • Yes of course...but IMO should be included in the other answer as a baseR solution ;)
            – Jimbou
            yesterday
















          or base R: subset(df, yr == 2001)
          – snoram
          yesterday




          or base R: subset(df, yr == 2001)
          – snoram
          yesterday












          Yes of course...but IMO should be included in the other answer as a baseR solution ;)
          – Jimbou
          yesterday




          Yes of course...but IMO should be included in the other answer as a baseR solution ;)
          – Jimbou
          yesterday


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372098%2fwhy-cant-subset-data-frame-by-some-keys%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

          Volksrepublik China

          How to test boost logger output in unit testing?

          Write to the output between two pipeline