简体   繁体   中英

How to subset a column from one file to the same column in another file?

I have two different .csv files. One of the files, let's call it CA, has a set of dates and times in one column (structured as: mm/dd/yyyy hh:mm:ss ). I have another .csv file, let's call is CA_adjusted, that has a column structured in the same manner.

I want to subset CA_adjusted with the dates and times of CA, so that I can pull out all relevant data in CA_adjusted. I would, preferably, like to subset that Date and Time column itself from CA to the Date and Time column in CA_adjusted. How do I do this?

I must admit I'm not 100% clear what type of join you need, because these things are hard to describe! But, I think a semi_join will do what you want. It will return the table you specify with matching cases in another table. An example:

install.packages("dplyr")
require("dplyr")

set.seed(0)
x <- sample(1:100, 100, replace = TRUE)
x <- data.frame(x)
y <- sample(1:100, 100, replace = TRUE)
y <- data.frame(y)
y$x <- y$y         # so dplyr knows which columns to join on
which(x$x == y$y)

a <- semi_join(x, y)  # returns table x with matching rows in y

Check out the other join options in dplyr if this isn't exactly what you want:

?dplyr::join

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM