简体   繁体   中英

Finding the corresponding values based on two criteria in R

I have two data frames

df1 <- data.frame(Region = c(1:5), Code = c(10,11,12,15,15), date = c("2018-12","2018-11","2019-01","2019-01","2019-02"))
df2 <- data.frame(Code = c(10,11,12,13,14,15,16,17,18,19),"2018-10" = c(50:59),"2018-11" = c(20:29),"2018-12" = c(25:34),"2019-01" = c(32:41),"2019-01" = c(40:49),"2019-02" = c(40:49))

I would like to match and store the corresponding values of df1$Region in df3.

The result should look as follows

df3 <- data.frame(Region = c(1:5),Results=c(25,21,34,45,45))

We can use row/column indexing to extract the values by match ing the the 'Code' columns as row index, and the 'date/column names` of the the two dataset to get the column index (without using any external packages)

cbind(df1['Region'], Results = df2[-1][cbind(match(df1$Code, df2$Code), 
        match(df1$date,
         sub('^X(\\d{4})\\.', "\\1-", names(df2)[-1])))])
#  Region Results
#1      1      25
#2      2      21
#3      3      34
#4      4      37
#5      5      45

NOTE: The column names in the OP's post had ^X at the beginning and its format was . instead of - as it is created with check.names = TRUE (default)


if the datasets were created with check.names = FALSE , the above solution can be further simplified

cbind(df1['Region'], Results = df2[-1][cbind(match(df1$Code, df2$Code),
           match(df1$date, names(df2)[-1]))])
#  Region Results
#1      1      25
#2      2      21
#3      3      34
#4      4      37
#5      5      45

Update

If the column names are duplicated and wants to match based on that info, then

i1 <- duplicated(df1$date)
v1 <- numeric(nrow(df1))
v1[!i1] <- df2[-1][cbind(match(df1$Code[!i1], 
             df2$Code),match(df1$date[!i1], names(df2)[-1]))]
v1[i1] <- rev(df2[-1])[cbind(match(df1$Code[i1], 
            df2$Code),match(df1$date[i1], rev(names(df2)[-1])))]
cbind(df1['Region'], Results = v1)
#   Region Results
#1      1      25
#2      2      21
#3      3      34
#4      4      45
#5      5      45

NOTE: No external packages used

One option involving dplyr and tidyr could be:

df1 %>%
 inner_join(df2 %>%
             pivot_longer(-Code), by = c("Code" = "Code",
                                         "date" = "name"))


  Region Code    date value
1      1   10 2018-12    25
2      2   11 2018-11    21
3      3   12 2019-01    34
4      4   15 2019-01    37
5      5   15 2019-02    45

I considered two columns in df2 with the same name as a typo.

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