VOOZH about

URL: https://www.geeksforgeeks.org/r-language/how-to-create-rename-recode-and-merge-variables-in-r/

⇱ How to Create, Rename, Recode and Merge Variables in R - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Create, Rename, Recode and Merge Variables in R

Last Updated : 23 Jul, 2025

Variable manipulation is a key part of working with data in the R Programming Language. These actions, whether they involve adding new variables, renaming old ones, recoding them, or merging them together, are critical for every data analysis process. In this article, we'll delve into the intricacies of how to perform these operations effectively using R.

What are the variables in R?

Variables in R serve as a container for storing data values. They are essential components in any data analysis process since they contain the information that we change and analyze. Variables can include a variety of data types, such as numeric, character, logical, and factor.

Creating Variables

Creating variables in R is a basic operation. To assign values to variables, use the assignment operator like <- or =.

Output:

[1] 10

[1] "Hello, world!"

[1] "HY"

Renaming Variables

Sometimes you may need to rename variables in your dataset. renaming variables in datasets for clarity, consistency, or compatibility can be done using functions like names().

Output:

 var1 var2
1 1 A
2 2 B
3 3 C

new_var1 new_var2
1 1 A
2 2 B
3 3 C

Recoding Variables

Recoding variables entails transforming the values of a variable into different categories or values based on specific conditions or criteria, often accomplished using conditional statements or functions like ifelse().

Output:

 A B C
1 1 4 7
2 2 5 8
3 3 6 9

B C A
1 4 7 1
2 5 8 2
3 6 9 3

Merging Variables

Merging variables merges numerous variables or datasets using common identifiers, such as keys or indices, to produce a single dataset that includes information from all merged variables or datasets. In R, functions like as merge() and cbind() can be used to achieve this.

Output:

 ID var1
1 1 A
2 2 B
3 3 C

ID var2
1 1 10
2 2 20
3 3 30

ID var1 var2
1 1 A 10
2 2 B 20
3 3 C 30

Conclusion

Variable manipulation is an essential component of data analysis in R. Whether you're adding, renaming, recoding, or combining variables, mastering these actions is critical for gaining valuable insights from your data. Following the strategies and recommended practices discussed in this article will prepare you to confidently handle variable manipulation jobs in R.

Comment
Article Tags:

Explore