我有2个数据表,我想通过绑定行来合并。 dput如下:
>dput(x)
structure(list(NN = c("test", "test", "test", "test", "test"),
gpn = c("gas", "gas", "gas", "gas", "gas"), sitename = c("agip",
"agip", "agip", "agip", "agip"), code = c("F123", "F123",
"F123", "F123", "F123"), Brand = c("bp", "shell", "ionic",
"meta", "robo"), starttime = structure(c(1533097609, 1533118867,
1533183999, 1533270359, 1533356437), class = c("POSIXct",
"POSIXt"), tzone = ""), endtime = structure(c(1533118867,
1533183999, 1533270359, 1533356437, 1533443068), class = c("POSIXct",
"POSIXt"), tzone = ""), price = c(1.592, 1.592, 1.598, 1.598,
1.598), otherprice = c(1.998, 2.11, 2.114, 2.134, 1.989)), .Names = c("NN",
"gpn", "sitename", "code", "Brand", "starttime", "endtime", "price",
"otherprice"), row.names = c(NA, -5L), class = c("data.table",
"data.frame"), .internal.selfref = <pointer: 0x00000000025c0788>)
>dput(y)
structure(list(NN = c("test", "test", "test", "test", "test"),
gpn = c("gas", "gas", "gas", "gas", "gas"), sitename = c("agip",
"agip", "agip", "agip", "agip"), code = c("F123", "F123",
"F123", "F123", "F123"), Brand = c("dino", "dino", "dino",
"dino", "dino"), otherprice = c(1.987, 2.456, 3.456, 1.989,
1.99), starttime = structure(c(1533097399, 1533118999, 1533184999,
1533271999, 1533359357), tzone = "", class = c("POSIXct",
"POSIXt")), endtime = structure(c(1533118100, 1533182999,
1533270000, 1533356000, 1533443000), class = c("POSIXct",
"POSIXt"), tzone = "")), .Names = c("NN", "gpn", "sitename",
“代码”,“品牌”,“ otherprice”,“开始时间”,“结束时间”),类= c(“ data.table”, “ data.frame”),.internal.selfref =)
我想将y到x的行绑定,以使以下匹配:
伪代码:
If(NN,来自'x'的代码与NN,来自'y'的代码)&'x $ starttime'<'y $ starttime'&'x $ endtime'>'y $ endtime'
然后从“ y”获取NN,代码,品牌,价格,结果框架将变为:
NN gpn sitename code Brand starttime endtime price otherprice
test gas agip F123 bp 01/08/2018 05:26 01/08/2018 11:21 1.592 1.998
test gas agip F123 shell 01/08/2018 11:21 02/08/2018 05:26 1.592 2.110
test gas agip F123 Ionic 02/08/2018 05:26 03/08/2018 05:25 1.598 2.114
test gas agip F123 meta 03/08/2018 05:25 04/08/2018 05:20 1.598 2.134
test gas agip F123 robo 04/08/2018 05:20 05/08/2018 05:24 1.598 1.989
test gas agip F123 dino 01/08/2018 05:26 01/08/2018 11:21 1.592 1.987
test gas agip F123 dino 01/08/2018 11:21 02/08/2018 05:26 1.592 2.456
test gas agip F123 dino 02/08/2018 05:26 03/08/2018 05:25 1.598 3.456
test gas agip F123 dino 03/08/2018 05:25 04/08/2018 05:20 1.598 1.989
test gas agip F123 dino 04/08/2018 05:20 05/08/2018 05:24 1.598 1.990
我已经检查了InsertRow
和rbind
,但是都没有。
有什么想法吗?
谢谢。
答案 0 :(得分:1)
我们可以使用
/**********************************************************************
file: main_ws2812_basic.cpp - basic ws2812 test
Copyright (C) 2018 p. chu
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
**********************************************************************/
/*****************************************************************//**
* @note neo8 is an 8-led strip
* @note ws2812 is very bright (25% power is enough)
* @note ws2812 specifies 5-V control signal
********************************************************************/
#include "de10_baseline.h"
#include "ws2812_core.h"
Ws2812Core light_matrix(get_slot_addr(BRIDGE_BASE,14),3,3);
int main() {
// rgb values of a rain bow (red, orange, yellow, ..., purple)
const int rainbow[] = { 0xff0000, 0xc04000, 0x808000, 0x00ff00,
0x008080, 0x0000ff, 0x4000c0, 0x800080 };
// for (int i = 0; i < 4; i++){
// }
int i = 0;
int j = 0;
for(i = 0; i<3; i++){
for(j=0; j<3; j++){
light_matrix.wr_pix(i,j,0x000000);
}
}
while (1) {
light_matrix.wr_pix(0,0,0x000000); //green
light_matrix.wr_pix(0,1,0x000000); //green
light_matrix.wr_pix(0,2,0x000000); //green
light_matrix.wr_pix(1,0,0x000000); //blue
light_matrix.wr_pix(1,1,0x000000); //blue
light_matrix.wr_pix(1,2,0x000000); //blue
light_matrix.wr_pix(2,0,0x000000); //green
light_matrix.wr_pix(2,1,0x000000); //green
light_matrix.wr_pix(2,2,0x000000); //green
// 8 shades of blue
// neopixel.set_brightness(0.5);
// //int blue = 0x000007;
//
// for (int i = 0; i < 8; i++) {
// blue = blue + 31;
// neopixel.wr_pix(i, 0, blue);
// }
//
// sleep_ms(3000);
// // brightness control (increasing exponentially to 25%)
// // 100^(1/1000)= 1.0233; i.e., 1.0233^1000 = 100
// double b = 0.0025;
// for (int n = 0; n < 1000; n++) {
// b = b * 1.0046;
// neopixel.set_brightness(b);
// // set rainbow color
// for (int i = 0; i < 8; i++) {
// neopixel.wr_pix(i, 0, rainbow[0]);
// }
// sleep_ms(5);
// }
// sleep_ms(3000);
// // animation
// for (int n = 0; n < 160; n++) {
// for (int i = 0; i < 8; i++) {
// int k = (i + n) % 8;
// neopixel.wr_pix(i, 0, rainbow[0]);
// sleep_ms(10);
// }
// }
// sleep_ms(2000);
}//while
}//main
进行合并,但是由于所提供的结构,警告很少。从dplyr
中,我们看到dput()
没有y
变量,sitename
和starttime
是因素,需要转换为posixct,并且该变量starttime
中的brand
具有小写的“ b”,而y
中的Brand
具有大写的“ B”。
针对这些观察,我们可以执行以下操作:
x
我们可以创建满足上述条件的子集(即选择# Change time to posixct to be consistent with x.
# NOTE: You may have to adjust the time zone
y$starttime = as.POSIXct(y$starttime)
y$endtime = as.POSIXct(y$endtime)
# Chance 'brand' to 'Brand'
colnames(y)[colnames(y)=="brand"] = "Brand"
# Change other variabes as mentioned in commen
y$otherprice = y$price
y$price = x$price
y$sitename = x$sitename
和x
的子集,其中y
和x$starttime < y$starttime
)
x$endtime > y$endtime
现在我们可以将子集绑定在一起
# Create subsets which match criteria
x.subset = subset(x, subset = x$starttime < y$starttime & x$endtime > y$endtime)
y.subset = subset(y, subset = x$starttime < y$starttime & x$endtime > y$endtime)
答案 1 :(得分:1)
要合并数据集,似乎一个好的解决方案是仅rbind
个数据集,同时使用merge
对数据集进行开始/结束时间限制。在下面的示例中,rbind
函数将merge
条件嵌套在开始和结束日期:
library('data.table')
z <- rbind(
x = x,
y = merge(x[, .(code, starttime.x = starttime, endtime.x = endtime, price)], y,
by = 'code',
allow.cartesian = TRUE,
all.x = TRUE)[starttime.x < starttime & endtime.x > endtime,
.SD,
.SDcols = c(paste0(names(y)), 'price')])
z
输出:
NN gpn sitename code Brand starttime endtime price otherprice
1: test gas agip F123 bp 2018-08-01 00:26:49 2018-08-01 06:21:07 1.592 1.998
2: test gas agip F123 shell 2018-08-01 06:21:07 2018-08-02 00:26:39 1.592 2.110
3: test gas agip F123 ionic 2018-08-02 00:26:39 2018-08-03 00:25:59 1.598 2.114
4: test gas agip F123 meta 2018-08-03 00:25:59 2018-08-04 00:20:37 1.598 2.134
5: test gas agip F123 robo 2018-08-04 00:20:37 2018-08-05 00:24:28 1.598 1.989
6: test gas agip F123 dino 2018-08-01 06:23:19 2018-08-02 00:09:59 1.592 2.456
7: test gas agip F123 dino 2018-08-02 00:43:19 2018-08-03 00:20:00 1.598 3.456
8: test gas agip F123 dino 2018-08-03 00:53:19 2018-08-04 00:13:20 1.598 1.989
9: test gas agip F123 dino 2018-08-04 01:09:17 2018-08-05 00:23:20 1.598 1.990