R 在ifelse条件和函数下创建多个列,其中参数取决于输出列的名称

R 在ifelse条件和函数下创建多个列,其中参数取决于输出列的名称,r,R,这是我的数据: # Test datasets test_df <- data.frame(A =c(1, 2, 3, 3, 4), AKH_UL =c(111, 222, 333, 444, 555), AKH_LL = c(222, 333, 444, 555, 666), AKH_UU = c(213, 242, 253, 546, 243), AKH_LU = c(453, 855, 784, 352, 585), FFL_UL =c(11

这是我的数据:

# Test datasets
test_df <- data.frame(A =c(1, 2, 3, 3, 4), AKH_UL =c(111, 222, 333, 444, 555), AKH_LL = c(222, 333, 444, 555, 666),
                     AKH_UU = c(213, 242, 253, 546, 243), AKH_LU = c(453, 855, 784, 352, 585), FFL_UL =c(111, 222, 333, 444, 555), FFL_LL = c(222, 333, 444, 555, 666), FFL_UU = c(213, 242, 253, 546, 243), FFL_LU = c(453, 855, 784, 352, 585))
现在,我为每一列分两个步骤进行。例如,对于AKH:

test_df$AKH <-
  ifelse(test_df$A ==  1, 
      myfunction1(test_df$AKH, test_df$AKH_LL, test_df$AKH_UL, test_df$AKH_LU, test_df$AKH_UU),
        ifelse((test_df$A == 2, 
           myfunction2(test_df$AKH, test_df$AKH_LL, test_df$AKH_LU),
              ifelse((test_df$A == 3,
                 myfunction3(test_df$AKH, test_df$AKH_UL, test_df$AKH_UU),
                     99999))))

我对第二项也做了同样的计算,只是在公式中用FFL代替AKH

这在rl中看起来非常可怕,不仅有两个,还有10个专栏,我担心其他孩子看到我的脚本时会取笑我

我签出了问题,但无法将其完全转移到我的问题中,因为我不知道如何在公式中的变量名和列名之间建立连接

将test_df$AKH替换为test_df[,column_names[i]],并用于和粘贴函数

例如:

column_names <- c("AKH", "FFL")

for(i in 1:length(column_names){

test_df[,column_names[i]]<-
 ifelse(test_df$A ==  1, 
     myfunction1(test_df[,column_names[i]]
                 test_df[,paste(column_names[i],"LL",sep = "_")], 
                 test_df[,paste(column_names[i],"UL",sep = "_")], 
                 test_df[,paste(column_names[i],"LU",sep = "_")], 
                 test_df[,paste(column_names[i],"UU",sep = "_")]),
      ifelse((test_df$A == 2, 
          myfunction2(test_df[,column_names[i]], 
                      test_df[,paste(column_names[i],"LL",sep = "_")],
                      test_df[,paste(column_names[i],"LU",sep = "_")]),
             ifelse((test_df$A == 3,
                myfunction3(test_df[,column_names[i]],
                            test_df$[,paste(column_names[i],"UL",sep = "_")], 
                            test_df$[,paste(column_names[i],"UU",sep = "_")]),
                            99999))))
}

将test_df$AKH替换为test_df[,列名称[i]],并用于和粘贴函数

例如:

column_names <- c("AKH", "FFL")

for(i in 1:length(column_names){

test_df[,column_names[i]]<-
 ifelse(test_df$A ==  1, 
     myfunction1(test_df[,column_names[i]]
                 test_df[,paste(column_names[i],"LL",sep = "_")], 
                 test_df[,paste(column_names[i],"UL",sep = "_")], 
                 test_df[,paste(column_names[i],"LU",sep = "_")], 
                 test_df[,paste(column_names[i],"UU",sep = "_")]),
      ifelse((test_df$A == 2, 
          myfunction2(test_df[,column_names[i]], 
                      test_df[,paste(column_names[i],"LL",sep = "_")],
                      test_df[,paste(column_names[i],"LU",sep = "_")]),
             ifelse((test_df$A == 3,
                myfunction3(test_df[,column_names[i]],
                            test_df$[,paste(column_names[i],"UL",sep = "_")], 
                            test_df$[,paste(column_names[i],"UU",sep = "_")]),
                            99999))))

}

您的简化函数不会在任何地方使用成本,因此您可能可以忽略这些。此外,您实际上不需要更新函数中的数据帧;他们应该只返回您的成本计算值。因此,您的函数应该如下所示:

# Case 1: 
myfunction1 <- function(cost_LL, cost_UL, cost_LU, cost_UU) {
  cost_LL * cost_UL + cost_UU * cost_LU
}

# Case 2:
myfunction2 <- function(cost_LL, cost_LU) {
  cost_LL * cost_LU 
}

# Case 3: 
myfunction3 <-function(cost_UL, cost_UU) {
  cost_UL * cost_UU
}
这应该适用于任意数量的变量集

编辑: 为了获得宽格式的成本,我们需要首先添加一个id变量,因为A列中有重复项。其余部分与上述类似,只是最后一行将结果转换为每个变量的广泛形式

test_df %>%
  group_by(A) %>%
  mutate(id=row_number()) %>%
  pivot_longer(cols=-c(A,id), names_to=c("ID", ".value"),
               names_pattern="(.+)_([U|L][L|U])") %>% # Run to here to see the result
  group_by(A, id, ID) %>%
  transmute(cost=ifelse(A==1, mapply(myfunction1, LL, UL, LU, UU),
                        ifelse(A==2, mapply(myfunction2, LL, LU),
                               ifelse(A==3, mapply(myfunction3, UL, UU), NA)))) %>%
  pivot_wider(id_cols=c(A, id), names_from=ID, values_from = cost)

# A tibble: 5 x 4
# Groups:   A, id [5]
      A    id    AKH    FFL
  <dbl> <int>  <dbl>  <dbl>
1     1     1 121131 121131
2     2     1 284715 284715
3     3     1  84249  84249
4     3     2 242424 242424
5     4     1     NA     NA

您的简化函数不会在任何地方使用cost,因此您可能可以忽略这些函数。此外,您实际上不需要更新函数中的数据帧;他们应该只返回您的成本计算值。因此,您的函数应该如下所示:

# Case 1: 
myfunction1 <- function(cost_LL, cost_UL, cost_LU, cost_UU) {
  cost_LL * cost_UL + cost_UU * cost_LU
}

# Case 2:
myfunction2 <- function(cost_LL, cost_LU) {
  cost_LL * cost_LU 
}

# Case 3: 
myfunction3 <-function(cost_UL, cost_UU) {
  cost_UL * cost_UU
}
这应该适用于任意数量的变量集

编辑: 为了获得宽格式的成本,我们需要首先添加一个id变量,因为A列中有重复项。其余部分与上述类似,只是最后一行将结果转换为每个变量的广泛形式

test_df %>%
  group_by(A) %>%
  mutate(id=row_number()) %>%
  pivot_longer(cols=-c(A,id), names_to=c("ID", ".value"),
               names_pattern="(.+)_([U|L][L|U])") %>% # Run to here to see the result
  group_by(A, id, ID) %>%
  transmute(cost=ifelse(A==1, mapply(myfunction1, LL, UL, LU, UU),
                        ifelse(A==2, mapply(myfunction2, LL, LU),
                               ifelse(A==3, mapply(myfunction3, UL, UU), NA)))) %>%
  pivot_wider(id_cols=c(A, id), names_from=ID, values_from = cost)

# A tibble: 5 x 4
# Groups:   A, id [5]
      A    id    AKH    FFL
  <dbl> <int>  <dbl>  <dbl>
1     1     1 121131 121131
2     2     1 284715 284715
3     3     1  84249  84249
4     3     2 242424 242424
5     4     1     NA     NA

非常感谢你的回答!我最终需要宽格式的输出,以便用另一个数据集对其进行rbind。我知道我可以在后记中重塑它,但如果可能的话,我更喜欢一种没有中间这一步的方式。我将尝试使您的解决方案适应这种工作方式。这是非常可能的,但我必须添加一个id变量。。。。检查我的编辑…非常感谢您的回答!我最终需要宽格式的输出,以便用另一个数据集对其进行rbind。我知道我可以在后记中重塑它,但如果可能的话,我更喜欢一种没有中间这一步的方式。我将尝试使您的解决方案适应这种工作方式。这是非常可能的,但我必须添加一个id变量。。。。检查我的编辑。。。。