R 中的data.table是data.frame的增强版本。由于它的执行速度和键入的代码较少,它在 R 中变得流行。 data.table 的目的是创建与数据框相同的表格数据,但语法有所不同。在下面的示例中,我们可以看到数据表的语法:
R
DataTable = data.table(name = c("a", "b", "c", "d"),
id = (7, 0, 3, 4))
DataTable
R
# student id
stuid = c(2, 5, 3, 4, 6, 7, 4, 2, 0)
# student age
age = c(23, 45, 67, 23, 41, 43, 54, 67, 89)
# sex of the student
sex = c(1, 1, 0, 0, 0, 1, 0, 1, 1)
# student info
stuinfo = data.frame(empidno, age, sex, status)
stuinfo
在上面的例子中,我们使用了函数data.table()然后我们使用了名为 c() 的函数,这意味着连接,用于将数据打印为一个系列,名称将是字符串类型,所以我们使用了“”和 id 作为整数类型,因此无需在引号中指定。
R 中的data.frame类似于用于创建表格数据的数据表,但数据表提供的功能比数据框多得多,因此,通常所有人都更喜欢data.table而不是data.frame 。但是 data.frame 也最好使用,现在让我们看看下面的数据框语法。与data.table的语法类似,data.frame在这里显然也是一样的,我们使用函数data.frame()代替data.table()。
电阻
# student id
stuid = c(2, 5, 3, 4, 6, 7, 4, 2, 0)
# student age
age = c(23, 45, 67, 23, 41, 43, 54, 67, 89)
# sex of the student
sex = c(1, 1, 0, 0, 0, 1, 0, 1, 1)
# student info
stuinfo = data.frame(empidno, age, sex, status)
stuinfo
上面的例子以表格的形式给了我们学生的数据。如果我们在这里观察,data.table的代码比data.frame的代码少,因此data.table编译时间短,输出速度快,这使得数据表得到广泛应用。
差异表
data.table |
data.frame |
---|---|
Syntax: data.table() | Syntax: data.frame() |
data.table is a rewritten form of data.frame in optimized c (or) data.table inherits from data.frame. |
data.frame is the base class in R and it is the default in R. |
data.table is used for more complex data structures and for big data. |
data.frame is used to build small tables and matrices etc. |
data.table is very much faster than a spark in many instances. | data.frame is 20 times slower than data.table |
The in-built features such as rolling joins, overlapping range makes users sort out the wide range of problems. |
data.frame lacks these features but it is good for beginners. |
Code efficient (we can able to write less number of lines of code in data.table) |
We need to write some more lines of code when compared to data.table |
To convert data.table to data.frame we use : setDF(dt) where DF = data frame and dt = data table. |
To convert data.frame to data.table we use : setDT(df) where DT = data table and df = data frame. |
data.table is widely used due to its advanced features and speed and memory. |
data.frame is also used but not that much of data.table and it is very good for beginners. |