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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:53:22+00:00 2026-06-02T18:53:22+00:00

I have a factor RFyhat which I’m looking to convert to a numeric vector.

  • 0

I have a factor RFyhat which I’m looking to convert to a numeric vector. I’ve already discovered that

as.numeric(levels(RFyhat))[RFyhat]

works as desired, and I’ve played around a bit with this construction:

c(1,2,20,4,5,6,7)[RFyhat]

also works as expected (RFyhat has 7 levels).

So I understand the behavior of this construction, but I’m wondering if anyone can explain how this syntax is intended to work, or whether it is just ‘syntactic sugar’. More specifically, does [RFyhat] act as an index vector? If it does, how do factors generally behave when used as an index?

  • 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-02T18:53:27+00:00Added an answer on June 2, 2026 at 6:53 pm

    Yes, I believe that factors gets converted to integers when used for indexing, rather than characters or anything else.

    Look at this example

    > fac <- factor(letters[c(1,1,2,1,3,3,2,1)])
    > vec <- c(b=1, a=2, c=3)
    > vec[fac]
    b b a b c c a b 
    1 1 2 1 3 3 2 1 
    

    So element 1 of fac has returned element 1 of vec, regardless of the different order of names.

    Personally I’d prefer as.integer(as.character(RFyhat)) to as.numeric(levels(RFyhat))[...].

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

Sidebar

Related Questions

I have data.frame that contains several factors and i want to rename factor levels
I have a factor vector x looking like this: 1992-02-13 2011-03-10 1998-11-30 Can I
I have an object that is a factor with a number of levels: x
I have a factory class, DocumentLoaderFactory , which simply returns an instance that implements
I have a data set that I use the model.matrix() function on to convert
I have a two factor based authentication server which i need to integrate to
I have a column that's a factor variable. I need to change the value
I have a data frame with several factor columns containing NaN 's that I
I have a factor variable which is comprised of times. When I attempt to
I have an R data frame containing a factor that I want to expand

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.