haritha madiraju
haritha madiraju

Reputation: 25

Matching 2 date columns with unequal length

i have the following sample of data

X <- c("11/12/2016", "12/12/2016", "13/12/2016","14/12/2016","15/12/2016","16/12/2016", "17/12/2016")
Y <- c("11/12/2016", "13/12/2016", "14/12/2016", "18/12/2016")

the output i want is something like this

X                        Y
11/12/2016         11/12/2016     
12/12/2016             NA
13/12/2016         13/12/2016 
14/12/2016         14/12/2016 
15/12/2016             NA
16/12/2016             NA
17/12/2016             NA

i have tried the following code but not getting the desired output

> X <- as.Date(data$X)
> Y <- as.Date(data$Y)
> Z <- NA
> for (i in 1:length(X)) {
+ if(X[i] == Y){
+ Z <- Y}
+ else NA }

Upvotes: 0

Views: 106

Answers (3)

Santi
Santi

Reputation: 368

Got the answer!

What you want is to "match" the values of one long vector for your other vector. For that, the function match is perfect because return the vector position of the matched elements. First, input the data (I added some corrections):

# Input data
X <- c("11/12/2016", "12/12/2016", "13/12/2016","14/12/2016","15/12/2016","16/12/2016", "17/12/2016")
Y <- c("11/12/2016", "13/12/2016", "14/12/2016", "18/12/2016")

# Transform into dates
X <- as.Date(X,"%d/%m/%Y")
Y <- as.Date(Y, "%d/%m/%Y")

Then, I create the data.frame Z based on the long vector X and I add the matched values of the vector Y:

# Run function match so you can see what can of output generates
match(x = X, table = Y)

# Create data.frame
Z <- data.frame(X = X, 
                # add matched values
              Y = Y[match(x = X, table = Y)]) 

Hope this helps.

Upvotes: -1

Tino
Tino

Reputation: 2101

You could use merge:

X <- c("11/12/2016", "12/12/2016", "13/12/2016","14/12/2016","15/12/2016","16/12/2016", "17/12/2016")
Y <- c("11/12/2016", "13/12/2016", "14/12/2016", "18/12/2016")

df_X <- data.frame(X)

df_Y <- data.frame(X = Y, Y = Y)

merge(df_X, df_Y, all = TRUE)

Or, if you like a tidyverse-approach:

library(tidyverse)
X <- c("11/12/2016", "12/12/2016", "13/12/2016","14/12/2016","15/12/2016","16/12/2016", "17/12/2016")
Y <- c("11/12/2016", "13/12/2016", "14/12/2016", "18/12/2016")

df_X <- tibble(X)

df_Y <- tibble(X = Y, Y = Y)

full_join(df_X, df_Y)

The important part is, that you duplicate your column you want to match and name it accordingly, or use the by-argument.

Upvotes: 0

Henry Navarro
Henry Navarro

Reputation: 953

Try this:

Your data:

> X <- c("11/12/2016", "12/12/2016", "13/12/2016","14/12/2016","15/12/2016","16/12/2016", "17/12/2016")
> Y <- c("11/12/2016", "13/12/2016", "14/12/2016", "18/12/2016")

Creating new vector of NA's and doing the match:

> Z<-rep(NA,length(X))
> Z[which(X %in% Y)]<-X[which(X %in% Y)]
> Z
[1] "11/12/2016" NA           "13/12/2016" "14/12/2016" NA           NA           NA 

Your data frame:

> data.frame(X,Y=Z)
           X          Y
1 11/12/2016 11/12/2016
2 12/12/2016       <NA>
3 13/12/2016 13/12/2016
4 14/12/2016 14/12/2016
5 15/12/2016       <NA>
6 16/12/2016       <NA>
7 17/12/2016       <NA>

Upvotes: 5

Related Questions