Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

R Data Frames

R Data Frames

A data frame can be understood as what we commonly call a "table".

A data frame is a data structure in R, a special kind of two-dimensional list.

Each column of a data frame has a unique column name, all columns have equal length, the data type within the same column must be consistent, and different columns may have different data types.

R data frames are created using the data.frame() function, with the following syntax:

data.frame(…, row.names = NULL, check.rows = FALSE,

           check.names = TRUE, fix.empty.names = TRUE,

           stringsAsFactors = default.stringsAsFactors())

The following creates a simple data frame containing name, employee ID, and monthly salary:

Example

table

=

data.frame

(

    Name

=

c

(

"Zhang San"

,

"Li Si"

)

,

    Employee ID

=

c

(

"001"

,

"002"

)

,

    Monthly salary

=

c

(

1000

,

2000

)

   

)

print

(

table

)

View table data

The output of the code above is:

姓名 工号 月薪

1 张三  001 1000

2 李四  002 2000

The data structure of a data frame can be displayed with the str() function:

Example

table

=

data.frame

(

    Name

=

c

(

"Zhang San"

,

"Li Si"

)

,

    Employee ID

=

c

(

"001"

,

"002"

)

,

    Monthly salary

=

c

(

1000

,

2000

)

)

Get the data structure

str

(

table

)

The output of the code above is:

'data.frame':   2 obs. of  3 variables:

 $ 姓名: chr  "张三" "李四"

 $ 工号: chr  "001" "002"

 $ 月薪: num  1000 2000

summary() can display a summary of the data frame:

Example

table

=

data.frame

(

    Name

=

c

(

"Zhang San"

,

"Li Si"

)

,

    Employee ID

=

c

(

"001"

,

"002"

)

,

    Monthly salary

=

c

(

1000

,

2000

)

   

)

Display the summary

print

(

summary

(

table

)

)

 

The output of the code above is:

姓名               工号                月薪     

Length:2           Length:2           Min.   :1000  

Class :character   Class :character   1st Qu.:1250  

Mode  :character   Mode  :character   Median :1500  

                                      Mean   :1500  

                                      3rd Qu.:1750  

                                      Max.   :2000  

We can also extract specified columns:

Example

table

=

data.frame

(

    Name

=

c

(

"Zhang San"

,

"Li Si"

)

,

    Employee ID

=

c

(

"001"

,

"002"

)

,

    Monthly salary

=

c

(

1000

,

2000

)

)

Extract specified columns

result

<-

data.frame

(

table

$name,

table

$salary

)

print

(

result

)

The output of the code above is:

table.姓名 table.月薪

1       张三       1000

2       李四       2000

The following form displays the first two rows:

Example

table

=

data.frame

(

    Name

=

c

(

"Zhang San"

,

"Li Si"

,

"Wang Wu"

)

,

    Employee ID

=

c

(

"001"

,

"002"

,

"003"

)

,

    Monthly salary

=

c

(

1000

,

2000

,

3000

)

)

print

(

table

)

Extract the first two rows

print

(

"---Output the first two rows----"

)

result

<-

table

[

1

:

2

,

]

print

(

result

)

The output of the code above is:

姓名 工号 月薪

1 张三  001 1000

2 李四  002 2000

3 王五  003 3000

[1] "---输出前面两行----"

  姓名 工号 月薪

1 张三  001 1000

2 李四  002 2000

We can read the data of a specified column in a specified row using a coordinate-like form; here we read the data in columns 1 and 2 of rows 2 and 3::

Example

table

=

data.frame

(

    Name

=

c

(

"Zhang San"

,

"Li Si"

,

"Wang Wu"

)

,

    Employee ID

=

c

(

"001"

,

"002"

,

"003"

)

,

    Monthly salary

=

c

(

1000

,

2000

,

3000

)

)

Read the data in columns 1 and 2 of rows 2 and 3:

result

<-

table

[

c

(

2

,

3

)

,

c

(

1

,

2

)

]

print

(

result

)

The output of the code above is:

姓名 工号

2 李四  002

3 王五  003

Extending a data frame

We can extend an existing data frame; in the following example, we add a department column:

Example

table

=

data.frame

(

    Name

=

c

(

"Zhang San"

,

"Li Si"

,

"Wang Wu"

)

,

    Employee ID

=

c

(

"001"

,

"002"

,

"003"

)

,

    Monthly salary

=

c

(

1000

,

2000

,

3000

)

)

Add a department column

table

$department

<-

c

(

"Operations"

,

"Technology"

,

"Edit"

)

print

(

table

)

The output of the code above is:

姓名 工号 月薪 部门

1 张三  001 1000 运营

2 李四  002 2000 技术

3 王五  003 3000 编辑

We can use the cbind() function combines multiple vectors into a data frame:

Example

Create a vector

sites

<-

c

(

"Google"

,

"Runoob"

,

"Taobao"

)

likes

<-

c

(

222

,

111

,

123

)

url

<-

c

(

"www.google.com"

,

"www.runoob.com"

,

"www.taobao.com"

)

Combine vectors into a data frame

addresses

<-

cbind

(

sites,likes,

url

)

View the data frame

print

(

addresses

)

The output of the code above is:



     sites    likes url             

[1,] "Google" "222" "www.google.com"

[2,] "Runoob" "111" "www.runoob.com"

[3,] "Taobao" "123" "www.taobao.com"

To merge two data frames, you can use rbind() functions as follows:

Example

table

=

data.frame

(

    Name

=

c

(

"Zhang San"

,

"Li Si"

,

"Wang Wu"

)

,

    Employee ID

=

c

(

"001"

,

"002"

,

"003"

)

,

    Monthly salary

=

c

(

1000

,

2000

,

3000

)

)

newtable

=

data.frame

(

    Name

=

c

(

"Xiao Ming"

,

"Xiaobai"

)

,

    Employee ID

=

c

(

"101"

,

"102"

)

,

    Monthly salary

=

c

(

5000

,

7000

)

)

Merge two data frames

result

<-

rbind

(

table

,newtable

)

print

(

result

)

The output of the code above is:

姓名 工号 月薪

1 张三  001 1000

2 李四  002 2000

3 王五  003 3000

4 小明  101 5000

5 小白  102 7000