You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用ifelse语句基于key获取R语言data.frame中对应val值

Solution for Retrieving Values by Key with ifelse() (and Better Alternatives)

Hey there! Let's break down how to get the corresponding val from your keyval data frame using ifelse()—plus some cleaner, more scalable methods since nested ifelse() can get messy fast.

First, let's make sure we're working with the same data (I added stringsAsFactors = FALSE to avoid common factor-related issues):

keyval <- data.frame(
  key = c('facebook', 'facebook mai - android', 'facebook mai - ios', 'facebook remarketing','facebook ig', 'app - android', 'bing - sem broad', 'bing - trademark', 'google - sem broad' , 'google - trademark' , 'google display', 'pla', 'google retargeting'),
  val = c(6,21,20,7,9,21,5,19,3,17,3,12,4),
  stringsAsFactors = FALSE
)

Using Nested ifelse()

If you specifically want to use ifelse(), you'll need to nest multiple ifelse() calls since each ifelse() only handles one binary condition. The second argument of each ifelse() is the value to return when the condition is TRUE, and the third argument is the next ifelse() check for the next key.

For example, if your target key is "facebook ig":

input_key <- "facebook ig"

result_val <- ifelse(input_key == keyval$key[1], keyval$val[1],
                    ifelse(input_key == keyval$key[2], keyval$val[2],
                           ifelse(input_key == keyval$key[3], keyval$val[3],
                                  ifelse(input_key == keyval$key[4], keyval$val[4],
                                         ifelse(input_key == keyval$key[5], keyval$val[5],
                                                ifelse(input_key == keyval$key[6], keyval$val[6],
                                                       ifelse(input_key == keyval$key[7], keyval$val[7],
                                                              ifelse(input_key == keyval$key[8], keyval$val[8],
                                                                     ifelse(input_key == keyval$key[9], keyval$val[9],
                                                                            ifelse(input_key == keyval$key[10], keyval$val[10],
                                                                                   ifelse(input_key == keyval$key[11], keyval$val[11],
                                                                                          ifelse(input_key == keyval$key[12], keyval$val[12],
                                                                                                 ifelse(input_key == keyval$key[13], keyval$val[13], NA))))))))))))

# Output: 9 (matches the val for "facebook ig")
result_val

As you can see, this works, but it's hard to read and maintain—especially if you add more keys later.

Better Alternatives

1. Use match() (Most Efficient Base R Method)

This is the cleanest way in base R. match() finds the position of your input key in keyval$key, then we use that position to pull the corresponding val:

input_key <- "facebook ig"

result_val <- keyval$val[match(input_key, keyval$key)]

# Output: 9
result_val

If the input key doesn't exist in keyval$key, it returns NA (which is usually what you want for missing matches).

2. Use dplyr::case_when() (More Readable for Multiple Conditions)

If you prefer a tidyverse approach, case_when() lets you write each key-value pair as a clear line, which is easier to scan and edit:

library(dplyr)

input_key <- "facebook ig"

result_val <- case_when(
  input_key == "facebook" ~ 6,
  input_key == "facebook mai - android" ~ 21,
  input_key == "facebook mai - ios" ~ 20,
  input_key == "facebook remarketing" ~ 7,
  input_key == "facebook ig" ~ 9,
  input_key == "app - android" ~ 21,
  input_key == "bing - sem broad" ~ 5,
  input_key == "bing - trademark" ~ 19,
  input_key == "google - sem broad" ~ 3,
  input_key == "google - trademark" ~ 17,
  input_key == "google display" ~ 3,
  input_key == "pla" ~ 12,
  input_key == "google retargeting" ~ 4,
  TRUE ~ NA_real_  # Catch-all for unmatched keys
)

# Output: 9
result_val

Final Notes

  • Stick with match() if you want a lightweight base R solution.
  • Use case_when() if you need to add extra logic alongside key matching (e.g., ranges or complex conditions).
  • Avoid nested ifelse() unless you have a tiny number of keys—it's prone to typos and hard to debug.

内容的提问来源于stack exchange,提问作者teo93

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:21:19