R语言 创建数据的表格表示 – table()函数
R语言中的 table() 函数用于创建一个带有变量名称和频率的表格形式的数据分类表示。
语法: table(x)
参数:
x: 要转换的对象
例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