Swapping two variables by creating a temporary variable

I am new to R programming. I have two Variables
A <- "Jack,Harris,Nelly"
B <- "Robert,Josh,Mary"
I need to copy all three names of vector A to Vector B and Vector B to Vector A (swapping). I created a third vector C <- vector("character",length=3) and I got stuck there. Can you please help me?

I really appreciate your help. Thanks

Welcome @r_beginner! If you're new to R, I'd highly recommend checking out the free book R for Data Science. There's lots of example code you can work through to get a thorough introduction to R. You might take a look at Chapter 4 which covers basic coding and objection assignment in R.

As to your specific question, you could do something like the following to swap A and B:

A <- c("Jack", "Harris", "Nelly")
B <- c("Robert","Josh","Mary")

C <- A
A <- B
B <- C

A
#> [1] "Robert" "Josh"   "Mary"
B
#> [1] "Jack"   "Harris" "Nelly"

Created on 2018-02-07 by the reprex package (v0.1.1.9000).

2 Likes

Thank you! That's very helpful.