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 8299679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:14:40+00:00 2026-06-08T16:14:40+00:00

Following code subsets a dataframe, but includes the row names in the subset: mpg

  • 0

Following code subsets a dataframe, but includes the row names in the subset:

mpg = subset(mtcars, cyl=='6', select=mpg)

How can I convert the resulting mpg object to a vector containing the values only? In other words, I need to get rid of the row names from the resulting mpg object

  • 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-06-08T16:14:45+00:00Added an answer on June 8, 2026 at 4:14 pm

    You have a number of options.

    Firstly, since you are using subset, you can specify the argument drop=TRUE, but see the warning later in this answer):

    subset(mtcars, cyl=='6', select=mpg, drop=TRUE)
    [1] 21.0 21.0 21.4 18.1 19.2 17.8 19.7
    

    But in the longer run you should get familiar with the R subset operators [, [[, and $:

    mtcars[mtcars$cyl==6, "mpg"]
    [1] 21.0 21.0 21.4 18.1 19.2 17.8 19.7
    

    And you can simplify that last line of code by using the function with (but, again, see the warning):

    with(mtcars, mtcars[cyl==6, "mpg"])
    [1] 21.0 21.0 21.4 18.1 19.2 17.8 19.7
    

    Warning: Some aspects of subset and with may be considered harmful. All objects are first evaluated in the context of the data frame. So, if you happen to use a variable with the same name as a column in your data frame, your expression will evaluate to the column name, not your variable:

    with(mtcars,mtcars[cyl==6,"mpg"])
    [1] 21.0 21.0 21.4 18.1 19.2 17.8 19.7
    
    gear <- 6
    with(mtcars,mtcars[cyl==gear,"mpg"])
    [1] 22.8 24.4 22.8 32.4 30.4 33.9 27.3 21.4
    
    subset(mtcars, cyl==gear, select=mpg, drop=TRUE)
    [1] 22.8 24.4 22.8 32.4 30.4 33.9 27.3 21.4
    

    The with and subset call has got gear from the data frame, and not from where you set it to 6.

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

Sidebar

Related Questions

Following code iterates through many data-rows, calcs some score per row and then sorts
In the following code: all_subsets = [] subsets = [[], [2]] left_most = 1
I'm looking for code which can do the following. Given a snippet of XML,
I am trying to extract a subset from a huge dataset. The following code
Following code produces a nested array as a result for keys containing three items:
Following code takes like 2500 milliseconds on an i7-*3.4 GHz windows-7 64-bit computer to
Following code worked fine abstract class FunctionRunnable<V> implements Runnable { protected abstract V calculate();
Following code: <%= render 'shared/error_messages', f.object %> where f.object is instance of a class
Following code gets executed whenever I want to persist any entity. Things seems to
Following code is a pretty simple and complete JQuery Dialog. Everything works. Problem is

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.