在 R 编程中创建数据的表格表示 - table()函数
R 语言中的table()
函数用于以表格的形式创建具有变量名称和频率的数据的分类表示。
Syntax: table(x)
Parameters:
x: Object to be converted
示例 1:
# R Program to create
# a tabular representation of data
# Creating a vector
vec = c(2, 4, 3, 1, 2, 3, 2, 1, 4, 2)
# Calling table() Function
table(vec)
输出:
vec
1 2 3 4
2 4 2 2
示例 2:
# R Program to create
# a tabular representation of data
# Creating a data frame
df = data.frame(
"Name" = c("abc", "cde", "def"),
"Gender" = c("Male", "Female", "Male")
)
# Calling table() function
table(df)
输出:
Gender
Name Female Male
abc 0 1
cde 1 0
def 0 1