简体   繁体   English

根据来自另一个数据框的两个条件过滤一个数据框

[英]Filter one dataframe based on two conditions from another dataframe

I have two dataframes, the first one has 30 thousand lines the second 571. 我有两个数据帧,第一个有3万行,第二个571。

I need filter the first one with 2 criteria of the second. 我需要使用第二个条件的2个条件过滤第一个条件。

Criteria A: (fctr) DF1$Col1 == [i]DF2$Col1 条件A:(fctr)DF1 $ Col1 == [i] DF2 $ Col1

Criteria B: (date) DF1$Col2 <= [i]DF2$Col2 准则B :(日期)DF1 $ Col2 <= [i] DF2 $ Col2

df1 = data.frame(col1 = c("a","a","a","b","b","b","b","c","c"), col2 = c("10/02", "15/02", "14/03", "05/03", "07/03", "15/03", "20/03", "12/03", "15/03"))
df2 = data.frame(col1 = c("a","b","c"), col2 = c("15/02", "15/03", "15/03"))

I need something like this: 我需要这样的东西:

dataframe3 = filter(df1, col1 == [i]df2$col1 & col2 <= [i]df2$col2)

#or

for(i in df2$col1){
  a=filter(df1, col1 ==i)
  for(e in df2$col2){ #here is the problem, i don't want loop in all dates
    b[]=filter(a, col2 <=e)}

If I understand correctly, this should do what you're looking for: 如果我正确理解,这应该可以满足您的需求:

# Add year to make the columns readable as dates
df1$col2 <- as.Date(paste0(df1$col2, "/2019"), format = "%d/%m/%Y")
df2$col2 <- as.Date(paste0(df2$col2, "/2019"), format = "%d/%m/%Y")

# Create df3 such that col1 mathces both dataframes
df3 <- merge(df1, df2, by = "col1", all.y = TRUE)

# Keep rows where df1$col2 <= df2$col2
df3 <- df3[df3$col2.x <= df3$col2.y, c("col1", "col2.x")]

# Rename columns
setnames(df3, "col2.x", "col2")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据另一个 dataframe 的两列过滤 dataframe - filter dataframe based on two columns of another dataframe 根据 R 中的两个匹配条件将一个数据帧中的值添加到另一个数据帧 - Adding values from one dataframe to another based on two matching conditions in R 通过另一个条件过滤一个 dataframe - filter one dataframe via conditions in another 循环根据条件将值从一个数据框添加到另一个数据框的问题 - Problem with loop to add values from one dataframe and to another based on conditions 如何使用另一个数据框根据多个条件过滤数据框 - How to filter a dataframe based on multiple conditions with another dataframe 根据 R 中一列下另一个数据帧值的最后两位数字过滤一个 dataframe - Filter one dataframe based on the last two digits of another dataframe's value under one column in R 如何根据日期条件将交易数量从一个数据帧合并到另一个数据帧 - How Merge transaction qty from one dataframe into another dataframe based on date conditions 如何通过两种条件将变量值从一个数据帧传输到另一数据帧? - How to transfer variable value from one dataframe to another dataframe by two conditions? 根据不同长度的另一个数据帧中是否存在NA过滤一个数据帧 - Filter one dataframe based on presence of NA in another dataframe of different length 根据r中另一个数据框中的列过滤一个数据框中的行 - Filter rows in one dataframe based on columns in another dataframe in r
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM