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

  • Home
  • SEARCH
  • 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 6067893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:39:27+00:00 2026-05-23T09:39:27+00:00

Is it possible to directly access elements of vectors in a data.frame? # DataFrame

  • 0

Is it possible to directly access elements of vectors in a data.frame?

# DataFrame
nerv <- data.frame(
    c(letters[1:10]),
    c(1:10)
)

str(nerv)

# Accessing 5th element of the second variable/column
# This should give "5", but it does NOT work
nerv[2][5]

# Works, but I need to know the NAME of the column
nerv$c.1.10.[5]

I tried several things, but none of them worked. I just have the index of the column but not the name, since I want to interate several columns using a loop.

It seems that I have a basic knowledge gap and I hope you can help me to fill it.

  • 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-23T09:39:27+00:00Added an answer on May 23, 2026 at 9:39 am

    You want:

    > nerv[5,2]
    [1] 5
    

    The general pattern is [r, c] where r indexes the rows, and c indexes the columns/variables, that you want to extract. One or both of these can be missing, in which case, it means give me all of the rows/columns that do not have indexes. E.g.

    > nerv[, 2] ## all rows, variable 2
     [1]  1  2  3  4  5  6  7  8  9 10
    > nerv[2, ] ## row 2, all variables
      c.letters.1.10.. c.1.10.
    2                b       2
    

    Notice that for the first of those, R has dropped the empty dimension resulting in a vector. To suppress this behaviour, add drop = FALSE to the call:

    > nerv[, 2, drop = FALSE] ## all rows, variable 2
       c.1.10.
    1        1
    2        2
    3        3
    4        4
    5        5
    6        6
    7        7
    8        8
    9        9
    10      10
    

    We can also use the list-style notation in extracting components of the data frame. [ will extract the component (column) as a one-column data frame, whilst [[ will extract the same thing but will drop dimensions. This behaviour comes from the usual behaviour on a list, where [ returns a list, whereas [[ returns the thing inside the indexed component. Some example might help:

    > nerv[2]
       c.1.10.
    1        1
    2        2
    3        3
    4        4
    5        5
    6        6
    7        7
    8        8
    9        9
    10      10
    > nerv[[2]]
     [1]  1  2  3  4  5  6  7  8  9 10
    > nerv[[1:2]]
    [1] 2
    

    This also explains why nerv[2][5] failed for you. nerv[2] returns a data frame with a single column, which you then try to retrieve column 5 from.

    The details of this are all included in the help file ?Extract.data.frame or ?`[[.data.frame`

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to access an element of an STL linked list directly by
Given this sample XML data, is it possible to access a Key directly? eg:
Is it possible to directly access MySql databases through an Adobe AIR/Flex application? If
Possible Duplicate: Any way to access array directly after method call? In C# and
Is it possible to have a Silverlight client app directly access the Azure Table
Is it possible to directly access third party web services using Ajax? Mostly I've
Is it possible to access the window object directly from the document object in
Is it possible to read a disk directly with .NET? By directly, I mean
Is it possible to use the with statement directly with CSV files? It seems
It is possible to send highlighted code directly to the F# interactive console in

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.