for循环匹配列

时间:2014-10-09 15:58:44

标签: r

我有两个不同的数据集,A和B,它们的长度不同,如果日期匹配,我需要在B中添加一列到A的末尾。以下是数据集B的外观:

 Variable_1    Variable_2       Date         Variable_3
 v1             v2          10/12/2014        0
 v1a            v2a         10/11/2014        18
 v1b            v2b         10/10/2014        15
 v1c            v2c         10/08/2014        0
 v1d            v2d         10/09/2014        0
 v1e            v2e         10/01/2014        0

A:

 Date              Variable_4         Variable_5    Variable_0
 10/11/2014          v4                v5            N
 10/12/2014          v4a              v5a            N
 10/08/2014          v4b              v5b            N
 10/09/2014          v4c              v5c            N
 10/25/2014          v4d              v5d            N
 10/31/2014          v4e              v5e            N

所以我需要一个循环来说明A中的Date与B中的Date相同,然后将Variable_3添加到数据集A.

所以在循环结束后它应该看起来像:

 Date              Variable_4         Variable_5      Variable_0
 10/11/2014          v4                v5              18 
 10/12/2014          v4a              v5a              0
 10/08/2014          v4b              v5b              0
 10/09/2014          v4c              v5c              0
 10/25/2014          v4d              v5d              N
 10/31/2014          v4e              v5e              N

目前我有这个循环,但它似乎没有用?

   lgthWeather <- length(Park$PRCP)
   lgthData <- length(Data$Date)
   count <- 0 


    #### broken loop ###

for(i in 1:lgthWeather)
{
  for(j in 1:lgthData)
  {
    if(Data$Date[j] == Park$Date[i])
    {
      Data$Precip[j] <- Park$PRCP[i] 
      j <- j + 1 
    }
    if(Data$Date[j] != Park$PRCP[i])
    {
      j <- j + 1
    }
  }
i <- i + 1 
}

任何建议都将不胜感激。

以下解决方案:

    merge(A, B[,c("Date","Variable_3")], by="Date", all.x=TRUE)

    Date Variable_4 Variable_5 Variable_0 Variable_3
    1 10/08/2014        v4b        v5b          N          0
    2 10/09/2014        v4c        v5c          N          0
    3 10/11/2014         v4         v5          N         18
    4 10/12/2014        v4a        v5a          N          0
    5 10/25/2014        v4d        v5d          N         NA
    6 10/31/2014        v4e        v5e          N         NA

2 个答案:

答案 0 :(得分:2)

看起来merge将会执行您正在寻找的内容:

merge(A, B[,c("Date","Variable_3")], by="Date", all.x=TRUE)

        Date Variable_4 Variable_5 Variable_0 Variable_3
1 10/08/2014        v4b        v5b          N          0
2 10/09/2014        v4c        v5c          N          0
3 10/11/2014         v4         v5          N         18
4 10/12/2014        v4a        v5a          N          0
5 10/25/2014        v4d        v5d          N         NA
6 10/31/2014        v4e        v5e          N         NA

答案 1 :(得分:2)

由于您的日期是唯一的,因此您可以使用带有match的矢量子集。

A$Variable_3 <- B$Variable_3[match(A$Date, B$Date)]
A[order(A$Date),]
#         Date Variable_4 Variable_5 Variable_0 Variable_3
# 3 10/08/2014        v4b        v5b          N          0
# 4 10/09/2014        v4c        v5c          N          0
# 1 10/11/2014         v4         v5          N         18
# 2 10/12/2014        v4a        v5a          N          0
# 5 10/25/2014        v4d        v5d          N         NA
# 6 10/31/2014        v4e        v5e          N         NA