有没有更好的方法来组合这些数据框和匹配值?

Is there a better way to combine these dataframes and match values?

我有两个使用 left_join()

合并的数据框

data1 可以简化为...

Date <- as.Date(c('2011-7-26','2011-7-26','2010-11-1','2010-11-1','2009-5-10','2009-5-10','2008-3-25','2008-3-25','2007-3-14','2007-3-14'))
Location <- c("A","B","A","B","A","B","A","B","A","B")
Result <- sample(1:30, 10)
data1 <- data.frame(Date,Location,Result)

data2 可以简化为...

Date <- as.Date(c('2011-7-26','2009-5-10','2007-3-14'))
Flow_A <- c(6,2,9)
Flow_B <- c(10,11,25)
data2 <- data.frame(Date,Flow_A,Flow_B)

按日期组合后,我有这个

data3 <- left_join(data2, data1, by = "Date")

        Date Flow_A Flow_B Location Result
1 2011-07-26      6     10        A     11
2 2011-07-26      6     10        B     17
3 2009-05-10      2     11        A      6
4 2009-05-10      2     11        B     22
5 2007-03-14      9     25        A     20
6 2007-03-14      9     25        B      1

结果中的每个值都对应于一个特定的位置(A 或 B),我想根据位置(即将 Flow_A 和 Flow_B 列合并为一列 'Flow',并且只有正确的值)。我已经能够使用 mutate()ifelse()grepl() 和非常简单的函数的组合来做到这一点:

a <- data3$Flow_A
Choose_A <- function(a) {
  return(a)}
d <- data3$Flow_B
Choose_B <- function(b) {
  return(b)} 

data3 <- mutate(data3, Flow =
             ifelse(grepl("A", Location), Choose_A(a), 
             ifelse(grepl("B", Location), Choose_B(b),NA)))

        Date Flow_A Flow_B Location Result Flow
1 2011-07-26      6     10        A     11    6
2 2011-07-26      6     10        B     17   10
3 2009-05-10      2     11        A      6    2
4 2009-05-10      2     11        B     22   11
5 2007-03-14      9     25        A     20    9
6 2007-03-14      9     25        B      1   25

但这看起来很笨拙。有没有更好(更有效)的方法来实现这个?

请原谅我的无知——我还在学习! 谢谢!

您可以使用 match 创建一个列号向量以从每一行中提取,并使用 cbind 创建一个矩阵,该矩阵用于从 'Flow_A''Flow_B' 取决于 Location 列。

column_num <- match(paste0('Flow_', data3$Location), names(data3))
row_num <- seq_len(nrow(data3))
data3$Flow <- data3[cbind(row_num, column_num)]