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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:39:47+00:00 2026-05-13T19:39:47+00:00

Each time when I have to recode some set of variables, I have SPSS

  • 0

Each time when I have to recode some set of variables, I have SPSS recode function in mind. I must admit that it’s quite straightforward. There’s a similar recode function in car package, and it does the trick, but let’s presuppose that I want to get things done with factor.

I have data.frame with several variables with value range from 1 to 7. I want to “reverse” variable values, hence replacing 1s with 7s, 2s with 6s, 3s with 5s etc. I can utilize factor function:

# create dummy factor
set.seed(100)
x <- as.factor(round(runif(100,1,7)))
y <- factor(x, levels = rev(levels(x)))

And if I run:

> levels(x)
[1] "1" "2" "3" "4" "5" "6" "7"
> levels(y)
[1] "7" "6" "5" "4" "3" "2" "1"

Problem starts when I want to recode factors that do not have equal levels. If some factor, z, has levels c("1", "3", "4", "6", "7"), is there any chance that I can “reverse” levels so 1=7, 2=6, 3=5 etc. by utilizing factor function?

Other efficient recode functions should suffice!

  • 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-13T19:39:47+00:00Added an answer on May 13, 2026 at 7:39 pm

    You must provide levels argument to factor (as Dirk wrote):

    set.seed(2342472)
    ( x <- round(runif(10,1,7)) )
    #  [1] 7 5 5 3 1 2 5 3 3 2
    ( xf <- as.factor(x) )
    # [1] 7 5 5 3 1 2 5 3 3 2
    # Levels: 1 2 3 5 7
    ( yf <- factor(x,levels=7:1) )
    # [1] 7 5 5 3 1 2 5 3 3 2
    # Levels: 7 6 5 4 3 2 1
    

    you could do this on existing factor too

    ( yxf <- factor(xf,levels=7:1) )
    # [1] 7 5 5 3 1 2 5 3 3 2
    #Levels: 7 6 5 4 3 2 1
    

    As you see levels were extended in desire order.

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

Sidebar

Related Questions

I have a rails 3 form that has 100+ questions. Each time a radio
Each time a python file is imported that contains a large quantity of static
I want to output the function name each time it is called, I can
I have a java application that stores some GPS data in a strange format:
Each time before committing I have a lot of changed files in my project.
I have a repeater which creates a child named wholeProject each time the repeater
I have a linq query from two database, however, each time the program stops
I have a folder that contains 1000+ sub folders. In each subfolder there are
How do I run a batch file each time windows boots up also I
It's a WiX MSI installer and each time I uninstall and reinstall it 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.