Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7413259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:40:04+00:00 2026-05-29T06:40:04+00:00

If you have a dataframe like this mydf <- data.frame(firstcol = c(1,2,1), secondcol =

  • 0

If you have a dataframe like this

mydf <- data.frame(firstcol = c(1,2,1), secondcol = c(3,4,5))

Why would

mydf[mydf$firstcol,]

work but

mydf[firstcol,]

wouldn’t?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:40:05+00:00Added an answer on May 29, 2026 at 6:40 am

    You can do this:

    mydf[,"firstcol"]
    

    Remember that the column goes second, not first.

    In your example, to see what mydf[mydf$firstcol,] gives you, let’s break it down:

    > mydf$firstcol
    [1] 1 2 1
    

    So really mydf[mydf$firstcol,] is the same as

    > mydf[c(1,2,1),]
        firstcol secondcol
    1          1         3
    2          2         4
    1.1        1         3
    

    So you are asking for rows 1, 2, and 1. That is, you are asking for your row one to be the same as row 1 of mydf, your row 2 to be the same as row 2 of mydf and your row 3 to be the same as row 1 of mydf; and you are asking for both columns.

    Another question is why the following doesn’t work:

    > mydf[,firstcol]
    Error in `[.data.frame`(mydf, , firstcol) : object 'firstcol' not found
    

    That is, why do you have to put quotes around the column name when you ask for it like that but not when you do mydf$firstcol. The answer is just that the operators you are using require different types of arguments. You can look at '$' to see the form x$name and thus the second argument can be a name, which is not quoted. You can then look up ?'[', which will actually lead you to the same help page. And there you will find the following, which explains it. Note that a “character” vector needs to have quoted entries (that is how you enter a character vector in R (and many other languages).

    i, j, ...: indices specifying elements to extract or replace.  Indices
          are ‘numeric’ or ‘character’ vectors or empty (missing) or
          ‘NULL’.  Numeric values are coerced to integer as by
          ‘as.integer’ (and hence truncated towards zero).  Character
          vectors will be matched to the ‘names’ of the object (or for
          matrices/arrays, the ‘dimnames’): see ‘Character indices’
          below for further details.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a big dataframe, but small example would be like this: mydf <-
Suppose I have a dataframe like this one: df <- data.frame (id = c(a,
I have a dataframe with numeric entries like this one test <- data.frame(x =
I have a dataframe in R like this: dat = data.frame(Sample = c(1,1,2,2,3), Start
Say I have a dataframe source <- data.frame(ID=c(1,2), COUNT=c(3,4)) that looks like this: ID
I have a data frame with gaps like this: Var1 Var2 Var3 1 NA
I have a data.frame in R that looks like this: score rms template aln_id
I have a data frame in R that looks like this: > TimeOffset, Source,
I have a data.frame that looks like this > head(df) Memory Memory Memory Memory
I have a data frame holding information on options like this > chData myIdx

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.