OneBite.Dev - Coding blog in a bite size

swap a string in R

Code snippet on how to swap a string in R

  a <- "Hello"
  b <- "World"
  temp <- a
  a <- b
  b <- temp

This code snippet uses R programming language to swap two strings, “Hello” and “World”. First, two variables, a and b, are initialized with the strings. Then a temporary variable, temp, is created to store the value of the a variable. Next, the value of the b variable is assigned to the a variable, and the value of the temp variable is assigned to the b variable. After that, the b variable contains the original value of the a variable and the a variable contains the original value of the b variable. Thus, the string values have been swapped.

r