簡體   English   中英

R:使用另一個數據框中的列名,條件和值在一個數據框中創建一個新列

[英]R: Create a new column in a dataframe, using column name, condition and value from another dataframe

將基本數據幀視為:

data <-  data.frame(amount_bin = c("10K-25K", "25K-35K", "35K-45K", "45K-50K", "50K+", "10K-25K", "25K-35K", "35K-45K", "45K-50K", "50K+", "10K-25K", "25K-35K", "35K-45K", "45K-50K", "50K+"),
                   risk_score = c("0-700", "700-750", "750-800", "800-850", "850-900", "0-700", "700-750", "750-800", "800-850", "850-900", "0-700", "700-750", "750-800", "800-850", "850-900"))

並在另一個數據幀中將信息分組為:

group_info <- data.frame(variable = c("amount_bin_group", "amount_bin_group", "amount_bin_group", "amount_bin_group", "amount_bin_group",
                                 "risk_score_group", "risk_score_group", "risk_score_group", "risk_score_group", "risk_score_group"),
                    bin = c("10K-25K", "25K-35K", "35K-45K", "45K-50K", "50K+",
                            "0-700", "700-750", "750-800", "800-850", "850-900"),
                    group = c("1", "1", "2", "2", "3",
                              "a", "a", "a", "b", "b"))

我想在稱為“ amount_bin_group”和“ risk_score_group”的基本數據幀(數據)中創建2列,當來自group_info和數據的bin列相同時,它們將從列group_info $ group中獲取值。 為簡單起見,我們假設基本列始終是group_info $ variable名稱減去“ group”字符串。 這意味着,當我們要創建列amount_bin_group時,基本列在基本數據幀中將始終為amount_bin。

預期結果數據幀為:

final_data <-  data.frame(amount_bin = c("10K-25K", "25K-35K", "35K-45K", "45K-50K", "50K+", "10K-25K", "25K-35K", "35K-45K", "45K-50K", "50K+", "10K-25K", "25K-35K", "35K-45K", "45K-50K", "50K+"),
                   risk_score = c("0-700", "700-750", "750-800", "800-850", "850-900", "0-700", "700-750", "750-800", "800-850", "850-900", "0-700", "700-750", "750-800", "800-850", "850-900"),
                   amount_bin_group = c("1", "1", "2", "2", "3", "1", "1", "2", "2", "3", "1", "1", "2", "2", "3"),
                   risk_score_group = c("a", "a", "a", "b", "b", "a", "a", "a", "b", "b", "a", "a", "a", "b", "b"))

我剛剛想到的解決方案是迭代合並數據幀,即:

final_data <- merge(data, group_info[, c("bin", "group")], by.x = "amount_bin", by.y = "bin")

final_data$amount_bin_group <- final_data$group
final_data$group <- NULL

但是,我相信可以有一個更有效的解決方案。 請注意,有多個此類列,而不僅僅是兩個。 因此,也許循環會有所幫助。

您的group_info太整潔了。 我真不敢說我在說。 通過將其分為兩個數據框,或將每個半框分成自己的列,您可以自己進行簡單的左連接以獲取答案。

final_data_calc <- data %>%
  left_join(
    group_info %>% 
      filter(variable == 'amount_bin_group') %>% 
      rename(amount_bin_group = group,amount_bin = bin) %>% 
      select(-variable)
  ) %>%
  left_join(
    group_info %>% 
      filter(variable == 'risk_score_group') %>% 
      rename(risk_score_group = group,risk_score = bin) %>% 
      select(-variable)
  )

#   amount_bin risk_score amount_bin_group risk_score_group
#1     10K-25K      0-700                1                a
#2     25K-35K    700-750                1                a
#3     35K-45K    750-800                2                a
#4     45K-50K    800-850                2                b
#5        50K+    850-900                3                b
#6     10K-25K      0-700                1                a
#7     25K-35K    700-750                1                a
#8     35K-45K    750-800                2                a
#9     45K-50K    800-850                2                b
#10       50K+    850-900                3                b
#11    10K-25K      0-700                1                a
#12    25K-35K    700-750                1                a
#13    35K-45K    750-800                2                a
#14    45K-50K    800-850                2                b
#15       50K+    850-900                3                b

您可以只使用for循環來繼續合並不同的集合:

for (i in unique(group_info$variable)) {
  data <- merge(
    data, group_info[group_info$variable==i,c("bin","group")],
    by.x=sub("_group","",i), by.y="bin"
  )
  names(data)[names(data)=="group"] <- i
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM