如何创建带有列名称的空数据表,然后将数据表追加到该表? [英] How to create an empty datatable with columns names and then append datatables to it?

查看:113
本文介绍了如何创建带有列名称的空数据表,然后将数据表追加到该表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我想创建一个带有列名的空数据表,但是失败:

First I want to create an empty datatable with column names but it fails:

data <- data.table(va, vb, vc)

> Error in data.table(va, vb, vc) : object 'va' not found

第二,我想向其附加数据表,但它也失败了:

Second I want to append datatable to it but it fails too :

data2 <- data.table(va=c(-1,0,1), vb=c(-1,0,1), vc=c(-1,0,1))
data2
   va vb vc
1: -1 -1 -1
2:  0  0  0
3:  1  1  1
merge(data2,data2)

> Error in merge.data.table(data2, data2) : 
      Can not match keys in x and y to automatically determine appropriate `by` parameter. Please set `by` value explicitly.

显然,该函数无法使用两个相同的数据表来识别by参数.有什么主意吗?

Apparently the function can't identify the by parameters with two identical datatables. Any idea?

推荐答案

创建空的data.table 使用(假设所有列均为数字):

To create an empty data.table use (assuming all columns are numeric):

library(data.table)    
data <- data.table(va=numeric(), vb=numeric(), vc=numeric())
data

其结果是:

> data
Empty data.table (0 rows) of 3 cols: va,vb,vc

对所有列进行自连接使用(即使结果相同;-):

To do a self join over all columns use (even though the result is the same ;-):

data2 <- data.table(va=c(-1,0,1), vb=c(-1,0,1), vc=c(-1,0,1))
data2
merge(data2, data2,by=names(data2))

必须指定by参数的原因是merge的已记录语义:

The reason why you have to specify the by parameter is the documented semantics of merge:

作者:

x和y中要合并的共享列名称的向量.这个默认 到两个表之间的共享键列.如果y没有密钥 列,默认为x的键.

A vector of shared column names in x and y to merge on. This defaults to the shared key columns between the two tables. If y has no key columns, this defaults to the key of x.

由于您尚未设置任何键,因此不清楚合并数据表的"join"列.

Since you don't have set any keys the "join" columns to merge the data tables are unclear.

如果省略by参数(如上面引用的共享键列),则没有隐式的使用所有列"语义.

There is no implicit "use all column" semantics if you omit the by parameter (as cited above the shared key columns are taken).

要将表的所有行附加到另一行,请使用rbind(行绑定")而不是merge:

To append all rows of a data.table to another one you use rbind ("row bind") instead of merge:

data3 <- rbind(data2, data2)
data3

这将导致:

> data3
   va vb vc
1: -1 -1 -1
2:  0  0  0
3:  1  1  1
4: -1 -1 -1
5:  0  0  0
6:  1  1  1

这篇关于如何创建带有列名称的空数据表,然后将数据表追加到该表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆