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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:36:06+00:00 2026-06-14T08:36:06+00:00

I have numeric vectors, such as c(1, 2, 3, 3, 2, 1, 3) or

  • 0

I have numeric vectors, such as c(1, 2, 3, 3, 2, 1, 3) or c(1, 4, 1, 4, 4, 1), and I would like to keep individual element’s position, but swap/reverse the value, so that we get c(3, 2, 1, 1, 2, 3, 1), c(4, 1, 4, 1, 1, 4) respectively.

To achieve that, I came up with a rather rough and ugly code below with lots of debugging and patching…

blah <- c(1, 4, 1, 4, 4, 1, 3)
blah.uniq <- sort(unique(blah))
blah.uniq.len <- length(blah.uniq)
j <- 1
end <- ceiling(blah.uniq.len / 2)
if(end == 1) {end <- 2} # special case like c(1,4,1), should get c(4,1,4) 
for(i in blah.uniq.len:end) {
  x <- blah == blah.uniq[i]
  y <- blah == blah.uniq[j]
  blah[x] <- blah.uniq[j]
  blah[y] <- blah.uniq[i]
  j = j + 1
}
blah

Is there an easier way to do this?

  • 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-14T08:36:08+00:00Added an answer on June 14, 2026 at 8:36 am

    Congratulations! You might have finally found a use for factors , I was still looking for one 🙂

    x <- c(1, 2, 3, 3, 2, 1, 3)
    # [1] 1 2 3 3 2 1 3
    y <- factor(x)
    # [1] 1 2 3 3 2 1 3
    # Levels: 1 2 3
    levels(y) <- rev(levels(y))
    # [1] 3 2 1 1 2 3 1
    # Levels: 3 2 1
    

    Built on that idea, here is a function that returns an object with the same class as the input:

    swap <- function(x) {
        f <- factor(x)
        y <- rev(levels(f))[f]
        class(y) <- class(x)
        return(y)
    }
    swap(c(1, 2, 3, 3, 2, 1, 3))
    # [1] 3 2 1 1 2 3 1
    swap(c(1, 4, 1, 4, 4, 1))
    # [1] 4 1 4 1 1 4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a (Numeric 3 float) vector class that I would love to align
I have a numeric vector, it contains patches of elements that are repeating, something
So I have a numeric value and on those two functions can be applied.
I have a dataframe with numeric entries like this one test <- data.frame(x =
I have a dataframe with some numeric columns. Some row has a 0 value
So I have a list, each element of which is a numeric vector. Each
To check whether value is numeric in LS, I would write the following code:
I have a numeric vector, let's say something like: x <- rep(1:6, 300) What
Assume we have two numeric vectors x and y . The Pearson correlation coefficient
I have a list NList of numeric vector like [[1]] [1] 1959 9 4

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.