Home > Enterprise >  Join dataframe to vector [r]
Join dataframe to vector [r]

Time:12-07

I would like to join a dataframe

df <- structure(list(IDDATE = c(1669935600, 1669939200, 1669942800, 1669946400, 
1669950000, 1669953600, 1669957200, 1669960800, 1669964400, 1669968000, 
1669971600, 1669975200, 1669978800, 1669982400, 1669986000, 1669989600, 
1669993200, 1669996800, 1670000400, 1670004000, 1670007600, 1670011200, 
1670014800, 1670018400, 1670022000, 1670025600, 1670029200, 1670032800, 
1670036400, 1670040000, 1670043600, 1670047200, 1670050800, 1670054400, 
1670058000, 1670061600, 1670065200, 1670068800, 1670072400, 1670076000, 
1670079600, 1670083200, 1670086800, 1670090400, 1670094000, 1670097600, 
1670101200, 1670104800, 1670108400), Value = c(32, 18, 31, 29, 
34, 35, 21, 24, 35, 34, 31, 19, 29, 31, 29, 28, 19, 35, 22, 15, 
28, 18, 25, 17, 17, 24, 28, 35, 34, 35, 19, 29, 31, 33, 25, 21, 
28, 27, 22, 27, 15, 29, 27, 22, 17, 34, 17, 17, 31)), class = "data.frame", row.names = c(NA, 
-49L))

With this vector file

IDDATE <- c(1669849200, 1669852800, 1669856400, 1669860000, 1669863600, 
1669867200, 1669870800, 1669874400, 1669878000, 1669881600, 1669885200, 
1669888800, 1669892400, 1669896000, 1669899600, 1669903200, 1669906800, 
1669910400, 1669914000, 1669917600, 1669921200, 1669924800, 1669928400, 
1669932000, 1669935600, 1669939200, 1669942800, 1669946400, 1669950000, 
1669953600, 1669957200, 1669960800, 1669964400, 1669968000, 1669971600, 
1669975200, 1669978800, 1669982400, 1669986000, 1669989600, 1669993200, 
1669996800, 1670000400, 1670004000, 1670007600, 1670011200, 1670014800, 
1670018400, 1670022000, 1670025600, 1670029200, 1670032800, 1670036400, 
1670040000, 1670043600, 1670047200, 1670050800, 1670054400, 1670058000, 
1670061600, 1670065200, 1670068800, 1670072400, 1670076000, 1670079600, 
1670083200, 1670086800, 1670090400, 1670094000, 1670097600, 1670101200, 
1670104800, 1670108400, 1670112000, 1670115600, 1670119200, 1670122800, 
1670126400, 1670130000, 1670133600, 1670137200, 1670140800, 1670144400, 
1670148000, 1670151600, 1670155200, 1670158800, 1670162400, 1670166000, 
1670169600, 1670173200, 1670176800, 1670180400, 1670184000, 1670187600, 
1670191200, 1670194800, 1670198400, 1670202000, 1670205600, 1670209200, 
1670212800, 1670216400, 1670220000, 1670223600, 1670227200, 1670230800, 
1670234400, 1670238000, 1670241600, 1670245200, 1670248800, 1670252400, 
1670256000, 1670259600, 1670263200, 1670266800, 1670270400, 1670274000, 
1670277600, 1670281200, 1670284800)

The purpose is to see eventual gaps in the dataframe and how many rows are missing. I have tried to use join functions from dplyr but does not seems to work...

library(dplyr)
library(tidyverse)
Date <- df %>% left_join(IDDATE, by = "IDDATE")

I have this error

Error in `auto_copy()`:
! `x` and `y` must share the same src.
ℹ set `copy` = TRUE (may be slow).
Run `rlang::last_error()` to see where the error occurred.

EDIT

I need to see eventual gaps between values, and do further data analysis regarding that. And not just see how much missing value there is.

CodePudding user response:

Turn vector in dataframe and then merge

df2 <- data.frame(IDDATE)
merge(df2,df,by='IDDATE',all.x=T)

        IDDATE Value
1   1669849200    NA
2   1669852800    NA
3   1669856400    NA
4   1669860000    NA
5   1669863600    NA
6   1669867200    NA
7   1669870800    NA
8   1669874400    NA
9   1669878000    NA
10  1669881600    NA
11  1669885200    NA
12  1669888800    NA
13  1669892400    NA
14  1669896000    NA
15  1669899600    NA
16  1669903200    NA
17  1669906800    NA
18  1669910400    NA
19  1669914000    NA
20  1669917600    NA
21  1669921200    NA
22  1669924800    NA
23  1669928400    NA
24  1669932000    NA
25  1669935600    32
26  1669939200    18
27  1669942800    31
28  1669946400    29
29  1669950000    34
30  1669953600    35
31  1669957200    21
32  1669960800    24
33  1669964400    35
34  1669968000    34
35  1669971600    31
36  1669975200    19
37  1669978800    29
38  1669982400    31
39  1669986000    29
40  1669989600    28
41  1669993200    19
42  1669996800    35
43  1670000400    22
44  1670004000    15
45  1670007600    28
46  1670011200    18
47  1670014800    25
48  1670018400    17
49  1670022000    17
50  1670025600    24
51  1670029200    28
52  1670032800    35
53  1670036400    34
54  1670040000    35
55  1670043600    19
56  1670047200    29
57  1670050800    31
58  1670054400    33
59  1670058000    25
60  1670061600    21
61  1670065200    28
62  1670068800    27
63  1670072400    22
64  1670076000    27
65  1670079600    15
66  1670083200    29
67  1670086800    27
68  1670090400    22
69  1670094000    17
70  1670097600    34
71  1670101200    17
72  1670104800    17
73  1670108400    31
74  1670112000    NA
75  1670115600    NA
76  1670119200    NA
77  1670122800    NA
78  1670126400    NA
79  1670130000    NA
80  1670133600    NA
81  1670137200    NA
82  1670140800    NA
83  1670144400    NA
84  1670148000    NA
85  1670151600    NA
86  1670155200    NA
87  1670158800    NA
88  1670162400    NA
89  1670166000    NA
90  1670169600    NA
91  1670173200    NA
92  1670176800    NA
93  1670180400    NA
94  1670184000    NA
95  1670187600    NA
96  1670191200    NA
97  1670194800    NA
98  1670198400    NA
99  1670202000    NA
100 1670205600    NA
101 1670209200    NA
102 1670212800    NA
103 1670216400    NA
104 1670220000    NA
105 1670223600    NA
106 1670227200    NA
107 1670230800    NA
108 1670234400    NA
109 1670238000    NA
110 1670241600    NA
111 1670245200    NA
112 1670248800    NA
113 1670252400    NA
114 1670256000    NA
115 1670259600    NA
116 1670263200    NA
117 1670266800    NA
118 1670270400    NA
119 1670274000    NA
120 1670277600    NA
121 1670281200    NA
122 1670284800    NA


  • Related