📜  R Dataframe中多个变量的频率计数

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

R Dataframe 中多个变量的频率计数

数据框可能包含重复或缺失值。每列可能包含相同变量的任意数量的重复或重复实例。数据统计和分析主要依赖于计算特定变量在每列中包含的实例数的频率或计数的任务,在 R 编程语言中,有多种方法可以做到这一点。

方法一:使用apply()方法

基 R 中的 apply 方法返回通过将函数应用于数组或矩阵的边距而获得的向量或数组或值列表。它具有以下语法:

apply ( df , axis , FUN)

table() 方法采用属于向量的交叉分类因子来构建每个因子级别组合的计数列联表。列联表基本上是多个变量的计数和/或百分比的表格。它从提供给方法的因子变量中排除了任何缺失值的计数。返回的输出采用表格的形式。该方法可用于交叉制表和统计分析。

示例 1:这里我们为数据框的所有列按列返回,指示在该特定列中出现的变量值实例的频率。



R
set.seed(1)  
# creating a data frame
data_frame <- data.frame(col1 =  sample(letters[1:3], 8,
                                        replace = TRUE),
                         col2 =  sample(letters[1:3], 8, 
                                        replace = TRUE),
                         col3 =  sample(letters[1:3], 8,
                                        replace = TRUE),
                         col4 =  sample(letters[1:3], 8, 
                                        replace = TRUE)
                        )
  
print ("Original DataFrame")
print (data_frame)
  
# calculating frequency of multiple variables
mod_frame <- apply(data_frame, 2 , table)
print ("Frequencies")
print (mod_frame)


R
set.seed(1)  
  
# creating a data frame
data_frame <- data.frame(col1 =  sample(letters[1:3], 8,
                                        replace = TRUE) ,
                         col2 =  sample(letters[1:3], 8, 
                                        replace = TRUE),
                         col3 =  sample(letters[1:3], 8,
                                        replace = TRUE),
                         col4 =  sample(letters[1:3], 8, 
                                        replace = TRUE)
                        )
  
print ("Original DataFrame")
print (data_frame)
sel_col <- c("col1", "col3")
  
# calculating frequency of multiple variables
mod_frame <- apply(data_frame[sel_col], 2, table)
print ("Frequencies")
print (mod_frame)


R
library("plyr")
set.seed(1)  
  
# creating a data frame
data_frame <- data.frame(col1 =  sample(letters[1:3], 8,
                                        replace = TRUE) ,
                         col2 =  sample(letters[1:3], 8,
                                        replace = TRUE),
                         col3 =  sample(letters[1:3], 8,
                                        replace = TRUE),
                         col4 =  sample(letters[1:3], 8,
                                        replace = TRUE)
)
  
print ("Original DataFrame")
print (data_frame)
sel_col <- c("col1")
  
# calculating frequency of multiple variables
mod_frame <- count(data_frame, sel_col)
print ("Frequencies")
print (mod_frame)


输出:

[1] "Original DataFrame" 
col1 col2 col3 col4 
1    a    b    b    a 
2    c    c    b    b 
3    a    c    c    a 
4    b    a    a    a 
5    a    a    c    b 
6    c    a    a    b 
7    c    b    a    b 
8    b    b    a    a 
[1] "Frequencies" 
$col1

a b c 
3 2 3 

$col2  
a b c  
3 3 2  
 $col3  
a b c  
4 2 2   
$col4  
a b  
4 4 

示例 2:也仅针对特定列,通过以向量的形式指定所需的列名称并使用数据框索引 df[cols] 寻址它们。输出以表格形式返回,其中列标题是所需的列名称,行标题是找到的不同值。

电阻

set.seed(1)  
  
# creating a data frame
data_frame <- data.frame(col1 =  sample(letters[1:3], 8,
                                        replace = TRUE) ,
                         col2 =  sample(letters[1:3], 8, 
                                        replace = TRUE),
                         col3 =  sample(letters[1:3], 8,
                                        replace = TRUE),
                         col4 =  sample(letters[1:3], 8, 
                                        replace = TRUE)
                        )
  
print ("Original DataFrame")
print (data_frame)
sel_col <- c("col1", "col3")
  
# calculating frequency of multiple variables
mod_frame <- apply(data_frame[sel_col], 2, table)
print ("Frequencies")
print (mod_frame)

输出:

[1] "Original DataFrame"
col1 col2 col3 col4
1    a    b    b    a
2    c    c    b    b
3    a    c    c    a
4    b    a    a    a
5    a    a    c    b
6    c    a    a    b
7    c    b    a    b
8    b    b    a    a 
[1] "Frequencies" 
col1 col3 
a    3    4 
b    2    2 
c    3    2

方法二:使用plyr包

plyr 包优选用于对数据进行实验,即创建、修改和删除数据框的列,使它们服从多个条件和用户定义的函数。可以使用以下命令将其下载并加载到工作区中:

install.packages("lpyr")

该包的 count() 方法用于分别返回指定列中包含的变量的频率计数。它可能包含多个列,所有可能的组合都是根据交叉连接生成的。它们中的唯一组合与它们各自的计数一起返回。

count (df , args..) , where args.. are the column names

输出仅返回 count() 方法中指定的列。

电阻

library("plyr")
set.seed(1)  
  
# creating a data frame
data_frame <- data.frame(col1 =  sample(letters[1:3], 8,
                                        replace = TRUE) ,
                         col2 =  sample(letters[1:3], 8,
                                        replace = TRUE),
                         col3 =  sample(letters[1:3], 8,
                                        replace = TRUE),
                         col4 =  sample(letters[1:3], 8,
                                        replace = TRUE)
)
  
print ("Original DataFrame")
print (data_frame)
sel_col <- c("col1")
  
# calculating frequency of multiple variables
mod_frame <- count(data_frame, sel_col)
print ("Frequencies")
print (mod_frame)

输出:

[1] "Original DataFrame"
col1 col2 col3 col4
1    a    b    b    a
2    c    c    b    b
3    a    c    c    a
4    b    a    a    a
5    a    a    c    b
6    c    a    a    b
7    c    b    a    b
8    b    b    a    a 
[1] "Frequencies"
  col1 freq 
1    a    3 
2    b    2 
3    c    3