R language Learning note-variable renaming

Source: Internet
Author: User

In the R language, variables are created when they are assigned, and cannot be declared beforehand. The variable renaming here actually refers to renaming the sub-variables in the variable, and if the object-oriented perspective is like modifying the property name of a class, its data remains unchanged. In a general object-oriented language, modifying a property name is not allowed, modifying the property name is tantamount to deleting the property, recreating a property, and assigning a value.

Why in the R language can modify the property name, which should be related to the main work of R, R as a statistical analysis of the language, the need to deal with a large number of data, processing the subject is a data object, if the object-oriented approach, the first to modify the properties of the class, and then re-instantiate, let the property assignment, This is a nightmare for R variables that already have a lot of data.

Fortunately, the R language solves this problem, the data subject does not move, only need to modify its label (or variable name) on it.

Modify the variable name mainly for the data frame, modify the way:

Suppose you already have a data frame mydata, which is structured as:

> MyData
X1 X2 sumx Meanx
1 111 2222) 2333 1166.5

1. Use the visual interface to modify

Using the command line:

> Fix (MyData)
> MyData
Y1 x2 sumx Meanx
1 111 2222) 2333 1166.5
In the popup dialog box, modify the variable name in the table. The result of this example is to modify the name of the variable x1 to y1

2. Use the command line to modify

Prerequisite: The reshape package needs to be installed first.

After the reshape package is installed, load the package, and then use the transform () command to modify it:

> Library (Reshape)
> Rename (mydata,x1= "y1")
Error in rename (mydata, x1 = "y1"): Parameter not Used (x1 = "Y1")
> Rename (mydata,c (x1= "y1"))
Y1 x2 sumx Meanx
1 111 2222) 2333 1166.5
>
The result modifies the name of the variable x1 to y1.

Note: The modified parameters are placed in a vector in the form C (oldname= "NewName", oldname2= "Newname2")

R language Learning note-variable renaming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.