Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:如何使用函数根据条件将值从一个数据帧替换为另一个数据帧?_R_Function_Date_Dataframe_Match - Fatal编程技术网

R:如何使用函数根据条件将值从一个数据帧替换为另一个数据帧?

R:如何使用函数根据条件将值从一个数据帧替换为另一个数据帧?,r,function,date,dataframe,match,R,Function,Date,Dataframe,Match,目标:将一个日期从一个数据帧映射到另一个数据帧,假设它在某个日期间隔内。例如,假设我们需要在2017年12月20日至2017年12月25日或2017年12月26日至2017年12月30日的时间间隔内交付礼物,并在2017年12月23日收到礼物寄件人的回复。我想创建一个函数,该函数可以根据响应日期所在的日期间隔确定将响应日期放置在何处。在上述示例中,响应日期将在2017年12月20日至2017年12月25日之间。 注:以下术语“匹配”表示一个数据帧与另一个数据帧之间满足特定条件 下面是一些示例代码

目标:将一个日期从一个数据帧映射到另一个数据帧,假设它在某个日期间隔内。例如,假设我们需要在2017年12月20日至2017年12月25日或2017年12月26日至2017年12月30日的时间间隔内交付礼物,并在2017年12月23日收到礼物寄件人的回复。我想创建一个函数,该函数可以根据响应日期所在的日期间隔确定将响应日期放置在何处。在上述示例中,响应日期将在2017年12月20日至2017年12月25日之间。 注:以下术语“匹配”表示一个数据帧与另一个数据帧之间满足特定条件

下面是一些示例代码来演示我的意思

# Creating the Data Frame with a start and end date interval
StartDate <- seq(as.Date("2000/1/1"), by = "month", length.out = 10)

EndDate <- StartDate +7

Dates_Interval <- data.frame(StartDate,EndDate)

# Creating a second data frame with the response dates only 
ResponseDate <- seq(as.Date("2000/1/6"), by = "month", length.out = 10)

Response_Substitute <- data.frame(ResponseDate)
# Substituting random NA values 
Response_Substitute[c(1,5,8),] <- NA


# > Response_Substitute 
#     ResponseDate
#    1          <NA>
#    2    2000-02-09
#    3    2000-03-06
#    4    2000-04-06
#    5          <NA>
#    6    2000-06-06
#    7    2000-07-06
#    8          <NA>
#    9    2000-09-06
#    10   2000-10-06

# Creating a function which evaluates a value in data frame two             
# (Response_Substitute) and checks 
# whether it meets
# a condition in Dates_Interval. 

dateresponses <- function(x,y,z) {
  sub_date <- ifelse ( y <=x && x <= z, x, NA)
  converteddate <- as.Date(sub_date, origin = "1899-12-30")
  return(converteddate)
}
# Example of the function in use to show how it matches a certain condition. 
x <- Response_Substitute[2,1] 
b <- dateresponses(x,Dates_Interval[2,1],Dates_Interval[2,2])


# > b
# [1] "1930-02-04"

# Example of the function in use to show when a response date does not 
# match a certain condition
   x <- Response_Substitute[2,1] <- as.Date("2000/2/9")
   b <- dateresponses(x,Dates_Interval[2,1],Dates_Interval[2,2])
# > b
#  [1] NA

# Example of the function in use to show when there is no response date in      
# the Response_Substitute variable 
   x <- Response_Substitute[1,1] 
   b <- dateresponses(x,Dates_Interval[2,1],Dates_Interval[2,2])
# > b
#  [1] NA
对于
NA
但不匹配任何间隔的响应日期,可以创建另一个数据帧,如下所示:

Unmatched_Response_Date <- data.frame(seq(as.Date("2000/1/9"), by = "month", 
length.out = 2))

colnames(Unmatched_Response_Date) <- "Unmatched Responses"

Unmatched_Response_Date
# > Unmatched_Response_Date
# Unmatched Responses
# 1          2000-01-09
# 2          2000-02-09

Unmatched\u Response\u Date以下是您提供的代码:

StartDate <- seq(as.Date("2000/1/1"), by = "month", length.out = 10)
EndDate <- StartDate +7
Dates_Interval <- data.frame(StartDate,EndDate)
# Creating a second data frame with the response dates only 
ResponseDate <- seq(as.Date("2000/1/6"), by = "month", length.out = 10)
Response_Substitute <- data.frame(ResponseDate)
# Substituting random NA values 
Response_Substitute[c(1,5,8),] <- NA
输出:

   ResponseDate      Date2  StartDate    EndDate
 1:         <NA>       <NA> 2000-01-01 2000-01-08
 2:   2000-02-06 2000-02-05 2000-02-01 2000-02-08
 3:   2000-03-06 2000-03-05 2000-03-01 2000-03-08
 4:   2000-04-06 2000-04-05 2000-04-01 2000-04-08
 5:         <NA>       <NA> 2000-05-01 2000-05-08
 6:   2000-06-06 2000-06-05 2000-06-01 2000-06-08
 7:   2000-07-06 2000-07-05 2000-07-01 2000-07-08
 8:         <NA>       <NA> 2000-08-01 2000-08-08
 9:   2000-09-06 2000-09-05 2000-09-01 2000-09-08
10:   2000-10-06 2000-10-05 2000-10-01 2000-10-08
这对你的问题有效吗?
进一步阅读:,

以下是您提供的代码:

StartDate <- seq(as.Date("2000/1/1"), by = "month", length.out = 10)
EndDate <- StartDate +7
Dates_Interval <- data.frame(StartDate,EndDate)
# Creating a second data frame with the response dates only 
ResponseDate <- seq(as.Date("2000/1/6"), by = "month", length.out = 10)
Response_Substitute <- data.frame(ResponseDate)
# Substituting random NA values 
Response_Substitute[c(1,5,8),] <- NA
输出:

   ResponseDate      Date2  StartDate    EndDate
 1:         <NA>       <NA> 2000-01-01 2000-01-08
 2:   2000-02-06 2000-02-05 2000-02-01 2000-02-08
 3:   2000-03-06 2000-03-05 2000-03-01 2000-03-08
 4:   2000-04-06 2000-04-05 2000-04-01 2000-04-08
 5:         <NA>       <NA> 2000-05-01 2000-05-08
 6:   2000-06-06 2000-06-05 2000-06-01 2000-06-08
 7:   2000-07-06 2000-07-05 2000-07-01 2000-07-08
 8:         <NA>       <NA> 2000-08-01 2000-08-08
 9:   2000-09-06 2000-09-05 2000-09-01 2000-09-08
10:   2000-10-06 2000-10-05 2000-10-01 2000-10-08
这对你的问题有效吗?
进一步阅读:,

为什么不在函数中的
ifelse
中使用
NA
而不是
“N/A”
?这将返回
NA
,而不是错误示例的错误消息。@这是一个很好的观点!我将把它改成代码来解决限制问题。这可以帮助我们解决整个问题谢谢。。这似乎也有类似的想法。如果您愿意,我可以针对您的特定问题写一封回复。@jacobsg我查看了您发送给我的链接,并将看看我可以如何处理它。不过,如果你能对我的特殊问题做出回应,那就太好了。非常感谢!为什么不在函数中的
ifelse
中使用
NA
而不是
“N/A”
?这将返回
NA
,而不是错误示例的错误消息。@这是一个很好的观点!我将把它改成代码来解决限制问题。这可以帮助我们解决整个问题谢谢。。这似乎也有类似的想法。如果您愿意,我可以针对您的特定问题写一封回复。@jacobsg我查看了您发送给我的链接,并将看看我可以如何处理它。不过,如果你能对我的特殊问题做出回应,那就太好了。非常感谢!
   ResponseDate      Date2  StartDate    EndDate
 1:         <NA>       <NA> 2000-01-01 2000-01-08
 2:   2000-02-06 2000-02-05 2000-02-01 2000-02-08
 3:   2000-03-06 2000-03-05 2000-03-01 2000-03-08
 4:   2000-04-06 2000-04-05 2000-04-01 2000-04-08
 5:         <NA>       <NA> 2000-05-01 2000-05-08
 6:   2000-06-06 2000-06-05 2000-06-01 2000-06-08
 7:   2000-07-06 2000-07-05 2000-07-01 2000-07-08
 8:         <NA>       <NA> 2000-08-01 2000-08-08
 9:   2000-09-06 2000-09-05 2000-09-01 2000-09-08
10:   2000-10-06 2000-10-05 2000-10-01 2000-10-08
# Removes the Date2 Column
join_df[, Date2:=NULL] 
# Generate list of responses that didn't join
setdiff(Response_Substitute$ResponseDate, join_df$ResponseDate)