首页 > 解决方案 > How to duplicate data based on matches in two different columns

问题描述

I have accelerometer data sampled at 12.5Hz. For one dataset (X1), I have compressed these datapoints down into 1 second intervals, but I now need to recombine it with another column (X2) (sampled at 12.5Hz) to generate an equation. I want to duplicate the values in X1 if the time data matches with X2

My files currently look something like this

X1
Time                           Accx1
2019-03-28 07:47:07            5
2019-03-28 07:47:08            1
2019-03-28 07:47:09            7

X2
Time                           Accx2
2019-03-28 07:47:07            2
2019-03-28 07:47:07            8
2019-03-28 07:47:07            1
2019-03-28 07:47:07            9
2019-03-28 07:47:07            1
2019-03-28 07:47:07            1
2019-03-28 07:47:07            2
2019-03-28 07:47:07            3
2019-03-28 07:47:07            1


And I want them to look like this:
Time                           Accx1         Accx2
2019-03-28 07:47:07            5             2
2019-03-28 07:47:07            5             8
2019-03-28 07:47:07            5             1
2019-03-28 07:47:07            5             9
2019-03-28 07:47:07            5             1
2019-03-28 07:47:07            5             1
2019-03-28 07:47:07            5             2
2019-03-28 07:47:07            5             3
2019-03-28 07:47:07            5             1

标签: r

解决方案


我相信您正在寻找合适的加入。这应该有效:

merge(X1, X2, by = "Time", all.x = FALSE, all.y = TRUE)

推荐阅读