English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

数据框R

数据框(Data frame)可以理解成我们常说的"表格"。

数据框是 R 语言的数据结构,是特殊的二维列表。

数据框每一列都有一个唯一的列名,长度都是相等的,同一列的数据类型需要一致,不同列的数据类型可以不一样。

R 语言数据框使用 data.frame() 函数来创建,语法格式如下:

data.frame(…, row.names = NULL, check.rows = FALSE,
           check.names = TRUE, fix.empty.names = TRUE,
           stringsAsFactors = default.stringsAsFactors())
  • ...: 列向量,可以是任何类型(字符型、数值型、逻辑型),一般以 tag = value 的形式表示,也可以是 value。

  • row.names: 行名,默认为 NULL,可以设置为单个数字、字符串或字符串和数字的向量。

  • check.rows: 检测行的名称和长度是否一致。

  • check.names: 检测数据框的变量名是否合法。

  • fix.empty.names: 设置未命名的参数是否自动设置名字。

  • stringsAsFactors: 布尔值,字符是否转换为因子,factory-fresh 的默认值是 TRUE,可以通过设置选项(stringsAsFactors=FALSE)来修改。

以下创建一个简单的数据框,包含姓名、工号、月薪:

table = data.frame(
    姓名 <- c("张三", "李四"),
    工号 <- c("001","002"),
    月薪 <- c(1000, 2000)
    
)
print(table) # 查看 table 数据

执行以上代码输出结果为:

姓名 工号 月薪
1 张三  001 1000
2 李四  002 2000

数据框的数据结构可以通过 str() 函数来展示:

table = data.frame(
    姓名 <- c("张三", "李四"),
    工号 <- c("001","002"),
    月薪 <- c(1000, 2000)
)
# 获取数据结构
str(table)

执行以上代码输出结果为:

'data.frame':   2 obs. of  3 variables:
 $ 姓名: chr  "张三" "李四"
 $ 工号: chr  "001" "002"
 $ 月薪: num 1000 2000

summary() È possibile visualizzare informazioni riassuntive del data frame:

table = data.frame(
    姓名 <- c("张三", "李四"),
    工号 <- c("001","002"),
    月薪 <- c(1000, 2000)
    
)
# Visualizzare riassunto
print(summary(table))

执行以上代码输出结果为:

姓名               工号                月薪     
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

Possiamo anche estrarre colonne specifiche:

table = data.frame(
    姓名 <- c("张三", "李四"),
    工号 <- c("001","002"),
    月薪 <- c(1000, 2000)
)
# Estrazione delle colonne specifiche
result <- data.frame(table$姓名,table$月薪)
print(result)

执行以上代码输出结果为:

table.姓名 table.月薪
1       张三         1000
2       李四         2000

La seguente forma mostra le prime due righe:

table = data.frame(
    姓名 = c("张三", "李四","王五"),
    工号 = c("001","002","003"),
    月薪 = c(1000, 2000,3000)
)
print(table)
# Estrazione delle prime due righe
print("---Output delle prime due righe----")
result <- table[1:2,]
print(result)

执行以上代码输出结果为:

姓名 工号 月薪
1 张三  001 1000
2 李四  002 2000
3 王五  003 3000
[1] "---Output delle prime due righe----"
  姓名 工号 月薪
1 张三  001 1000
2 李四  002 2000

Possiamo leggere i dati delle colonne specifiche delle righe specifiche in forma simile a un coordinate, nel seguente esempio leggiamo i dati delle righe 2 e 3 delle colonne 1 e 2:

table = data.frame(
    姓名 = c("张三", "李四","王五"),
    工号 = c("001","002","003"),
    月薪 = c(1000, 2000,3000)
)
# Leggere i dati delle righe 2 e 3 delle colonne 1 e 2:
result <- table[c(2,3),c(1,2)]
print(result)

执行以上代码输出结果为:

姓名 工号
2 李四 002
3 王五 003

Estendere il data frame

Possiamo estendere un data frame esistente, nel seguente esempio aggiungiamo una colonna di dipartimento:

table = data.frame(
    姓名 = c("张三", "李四","王五"),
    工号 = c("001","002","003"),
    月薪 = c(1000, 2000,3000)
)
# Aggiungere colonna dipartimento
table$部门 <- c("运营","技术","编辑")
print(table)

执行以上代码输出结果为:

姓名 工号 月薪 部门
1 张三 001 1000 运营
2 李四 002 2000 技术
3 王五 003 3000 编辑

Possiamo utilizzare cbind() La funzione combina più vettori in un data frame:

# Creare vettore
sites <- c("Google","w3codebox","Taobao")
likes <- c(222,111,123)
url <- c("www.google.com","it.oldtoolbag.com","www.taobao.com")
# 将向量组合成数据框
addresses <- cbind(sites,likes,url)
# 查看数据框
print(addresses)

执行以上代码输出结果为:

     sites    likes url             
[1,] "Google" "222" "www.google.com"
[2,] "w3codebox" "111" "it.oldtoolbag.com"
[3,] "Taobao" "123" "www.taobao.com"

如果要对两个数据框进行合并可以使用 rbind() 函数:

table = data.frame(
    姓名 = c("张三", "李四","王五"),
    工号 = c("001","002","003"),
    月薪 = c(1000, 2000,3000)
)
newtable = data.frame(
    姓名 = c("小明", "小白"),
    工号 = c("101","102"),
    月薪 = c(5000, 7000)
)
# 合并两个数据框
result <- rbind(table,newtable)
print(result)

执行以上代码输出结果为:

姓名 工号 月薪
1 张三  001 1000
2 李四  002 2000
3 王五  003 3000
4 小明  101 5000
5 小白  102 7000