📜  从 R 中的 DataFrame 创建表

📅  最后修改于: 2022-05-13 01:55:04.433000             🧑  作者: Mango

从 R 中的 DataFrame 创建表

在本文中,我们将讨论如何使用 R 编程语言从给定的数据帧创建表。

函数:

桌子(): 此函数是执行交互式数据分析的基本函数。因为它只是创建分类变量的表格结果。

示例 1:用 R 语言创建给定数据框的频率表:-



在本例中,我们将使用 R 语言中的 table()函数构建 R 语言中的简单频率表。该表仅提供数据框中元素的频率。

R
gfg_data <- data.frame(
  Country = c("France","Spain","Germany","Spain","Germany",
              "France","Spain","France","Germany","France"), 
    
  age = c(44,27,30,38,40,35,52,48,45,37),
    
  salary = c(6000,5000,7000,4000,8000), 
    
  Purchased=c("No","Yes","No","No","Yes",
              "Yes","No","Yes","No","Yes"))
  
gfg_table<-table(gfg_data$Country)
gfg_table


R
gfg_data <- data.frame(
  Country = c("France","Spain","Germany","Spain","Germany",
              "France","Spain","France","Germany","France"), 
    
  age = c(44,27,30,38,40,35,52,48,45,37),
  salary = c(6000,5000,7000,4000,8000), 
    
  Purchased=c("No","Yes","No","No","Yes","Yes",
              "No","Yes","No","Yes"))
  
gfg_table = as.table(table(gfg_data$Country))
prop.table(gfg_table)


R
gfg_data <- data.frame(
  Country = c("France","Spain","Germany","Spain","Germany",
              "France","Spain","France","Germany","France"), 
    
  age = c(44,27,30,38,40,35,52,48,45,37),
  salary = c(6000,5000,7000,4000,8000), 
    
  Purchased=c("No","Yes","No","No","Yes","Yes",
              "No","Yes","No","Yes"))
  
gfg_table =table(gfg_data$salary>6000)
gfg_table


R
gfg_data <- data.frame(
  Country = c("France","Spain","Germany","Spain","Germany",
              "France","Spain","France","Germany","France"), 
    
  age = c(44,27,30,38,40,35,52,48,45,37),
  salary = c(6000,5000,7000,4000,8000), 
    
  Purchased=c("No","Yes","No","No","Yes","Yes",
              "No","Yes","No","Yes"))
  
gfg_table =table(gfg_data$salary,gfg_data$Country)
gfg_table


输出:

France Germany   Spain  
     4       3       3 

示例2:用R语言用给定数据框的比例创建频率表:

在这里,我们将使用prop.table()函数,它的工作原理与简单的 table()函数非常相似,可以从给定的数据框中按比例获取频率表。

电阻

gfg_data <- data.frame(
  Country = c("France","Spain","Germany","Spain","Germany",
              "France","Spain","France","Germany","France"), 
    
  age = c(44,27,30,38,40,35,52,48,45,37),
  salary = c(6000,5000,7000,4000,8000), 
    
  Purchased=c("No","Yes","No","No","Yes","Yes",
              "No","Yes","No","Yes"))
  
gfg_table = as.table(table(gfg_data$Country))
prop.table(gfg_table)

输出:

France Germany   Spain 
    0.4     0.3     0.3 

示例 3:使用 R 语言中给定数据框的条件创建频率表:



在这个例子中,我们将使用 table()函数在 R 语言中构建简单的频率表,其中的条件作为 R 语言的函数参数。该表仅提供与数据框中函数中的给定条件匹配的元素的频率。

这里我们将使用 R 语言的 table()函数从数据框中制作一个薪水列的频率表,条件是薪水大于 6000。

电阻

gfg_data <- data.frame(
  Country = c("France","Spain","Germany","Spain","Germany",
              "France","Spain","France","Germany","France"), 
    
  age = c(44,27,30,38,40,35,52,48,45,37),
  salary = c(6000,5000,7000,4000,8000), 
    
  Purchased=c("No","Yes","No","No","Yes","Yes",
              "No","Yes","No","Yes"))
  
gfg_table =table(gfg_data$salary>6000)
gfg_table

输出:

FALSE  TRUE 
    6     4 

示例 4:从 R 语言中的给定数据框创建一个 2路交叉表:

在此示例中,我们将使用 R 语言的 table()函数在 R 语言中构建简单的 2 路交叉表。该表仅提供数据框中不同列元素的频率。

电阻

gfg_data <- data.frame(
  Country = c("France","Spain","Germany","Spain","Germany",
              "France","Spain","France","Germany","France"), 
    
  age = c(44,27,30,38,40,35,52,48,45,37),
  salary = c(6000,5000,7000,4000,8000), 
    
  Purchased=c("No","Yes","No","No","Yes","Yes",
              "No","Yes","No","Yes"))
  
gfg_table =table(gfg_data$salary,gfg_data$Country)
gfg_table

输出:

France Germany Spain
  4000      0       1     1
  5000      0       0     2
  6000      2       0     0
  7000      1       1     0
  8000      1       1     0