官术网_书友最值得收藏!

Renaming the data variable

The use of a data frame enables the user to select and filter data by row names and column names. As not all imported datasets contain row names and column names, we need to rename this dataset with a built-in naming function.

Getting ready

In this recipe, you need to prepare your environment with R installed and a computer that can access the Internet.

How to do it…

Perform the following steps to rename data:

  1. First, download employees.csv from the GitHub link https://github.com/ywchiu/rcookbook/raw/master/chapter3/employees.csv:
    > download.file("https://github.com/ywchiu/rcookbook/raw/master/chapter3/employees.csv", " employees.csv")
    
  2. Additionally, download salaries.csv from the GitHub link https://github.com/ywchiu/rcookbook/raw/master/chapter3/salaries.csv:
    > download.file("https://github.com/ywchiu/rcookbook/raw/master/chapter3/salaries.csv", "salaries.csv")
    
  3. Next, read the file into an R session with the read.csv function:
    > employees <- read.csv('employees.csv', head=FALSE)
    > salaries <- read.csv('salaries.csv', head=FALSE)
    
  4. Use the names function to view the column names of the dataset:
    > names(employees)
    [1] "V1" "V2" "V3" "V4" "V5" "V6"
    > names(salaries)
    [1] "V1" "V2" "V3" "V4"
    
  5. Next, rename columns with a given names vector:
    > names(employees) <- c("emp_no", "birth_date", "first_name", "last_name", "gender", "hire_date")
    > names(employees)
    [1] "emp_no" "birth_date" "first_name" "last_name" 
    [5] "gender" "hire_date"
    
  6. Besides using names, you can also rename columns with the colnames function:
    > colnames (salaries) <- c("emp_no", "salary", "from_date", "to_date")
    > colnames (salaries)
    [1] "emp_no" "salary" "from_date" "to_date"
    
  7. In addition to revising the column names, we can also revise row names with the rownames function:
    > rownames (salaries) <- salaries$emp_no
    

How it works…

In this recipe, we demonstrated how to rename datasets with the names function. First, we used the download.file function to download both salaries.csv and employees.csv from GitHub. Then, we used the names function to examine the column names of these two datasets. To revise the column names of these two datasets, we simply assigned a character vector to the name of the dataset. We can also revise column names with the colnames function. Finally, we can revise the row names of the dataset to emp_no with the rownames function.

There's more…

To avoid having to specify column names and row names separately with the colnames and rownames functions, we can use the dimnames function to specify both column names and row names in one operation:

> dimnames(employees) <- list(c(1,2,3,4,5,6,7,8,9,10), c("emp_no", "birth_date", "first_name", "last_name", "gender", "hire_date"))

In this code, the first input vector within the list indicates the row names, and the second input vector points to the column names.

主站蜘蛛池模板: 阜南县| 揭东县| 桐乡市| 宜兰市| 苏尼特右旗| 库尔勒市| 休宁县| 阳谷县| 漳州市| 桂平市| 张北县| 涞水县| 腾冲县| 竹北市| 托里县| 夏河县| 承德县| 壤塘县| 金沙县| 西乌珠穆沁旗| 隆化县| 收藏| 泰兴市| 南溪县| 汕尾市| 浑源县| 辽阳市| 宁城县| 定兴县| 额尔古纳市| 临桂县| 新沂市| 陇南市| 高雄市| 婺源县| 栾城县| 绥阳县| 永定县| 元谋县| 白银市| 固阳县|