R 程序创建空数据框


2023年2月7日, Learn eTutorial
1788

如何使用 R 程序创建一个空数据框?

这里我们解释如何编写 R 程序来创建一个空数据框。为此,我们使用内置函数 **data.frame()** 来创建它。数据框用于存储数据表,数据表包含等长向量列表。数据框由函数 **data.frame()** 创建,该函数具有紧密耦合的变量集合。

在此 R 程序中,我们直接将值传递给内置函数。并打印函数结果。在此程序中,我们使用变量“Df”作为数据框。数据框由不同的数据类型组成,包括**整数、双精度浮点数、字符、逻辑值、因子和作为因子的字符串。**

算法

步骤 1:将变量 **Df** 视为数据框。

步骤 2:使用不同的数据类型调用 **data.frame()**

步骤 3:将函数结果赋值给 **Df**

步骤 4: 打印函数的结果

 

R 源代码

                                          Df = data.frame(Ints=integer(),
                Doubles=double(),
                Characters=character(),
                Logicals=logical(),
                Factors=factor(),
                stringsAsFactors=FALSE)
print("Structure of the empty dataframe:")
print(str(Df))

                                      

输出

[1] "Structure of the empty dataframe:"
'data.frame': 0 obs. of  5 variables:
 $ Ints      : int 
 $ Doubles   : num 
 $ Characters: chr 
 $ Logicals  : logi 
 $ Factors   : Factor w/ 0 levels: 
NULL