我正在处理一个大型调查数据框,其中每个问题的回答都是一个数字.对于像年龄这样的数字调查问题,数字就是数字.但对于多项 Select 题,数字是与保存在单独查找数据框中的文本相对应的代码.

How can I replace all the numbers for each variable with their corresponding label from the lookup dataframe?

示例数据:

df_numeric <- 
  tibble::tribble(
    ~gender, ~age, ~city, ~yearly_income, ~fav_colour,  ~over_100_more_vars,
          1,   22,     1,          55000,           1,                "...",
          2,   31,     2,         122000,           2,                "...",
          1,   41,     1,         101000,           2,                "...",
          2,   19,     5,          76000,           1,                "...",
          1,   64,     7,          32000,           6,                "...")
    
df_lookup <- 
  tibble::tribble(
           ~variable, ~number,        ~label,
            "gender",       1,        "Male",
            "gender",       2,      "Female",
              "city",       1,    "New York", 
              "city",       2,      "Sydney",
              "city",       5,      "London",
              "city",       7,       "Paris",
        "fav_colour",       1,         "Red",
        "fav_colour",       2,        "Blue",
        "fav_colour",       6,      "Purple",
   "one_of_100_more",       1,       "Label",
   "one_of_100_more",       2,       "Label",
   "two_of_100_more",       1,       "Label",
               "etc",       1,         "etc")

理想情况下,我想做的事情是这样的:判断df_NUMERIC中的变量名,在df_lookup中查找该变量,然后对于该特定变量,将每个‘number’替换为其对应的‘Label’,然后移动到下一个变量,将其数字替换为其标签,然后移动到下一个...它应该看起来像这样

df_output <- 
  tibble::tribble(
    ~gender, ~age,      ~city, ~yearly_income, ~fav_colour,  ~over_100_more_vars,
    "Male",   22,  "New York",          55000,       "Red",                "...",
  "Female",   31,    "Sydney",         122000,      "Blue",                "...",
    "Male",   41,  "New York",         101000,      "Blue",                "...",
  "Female",   19,    "London",          76000,       "Red",                "...",
    "Male",   64,     "Paris",          32000,    "Purple",                "...")

重要注意事项:

  • 有数百个变量,所以在代码中写出每个变量的名称是不可行的(例如this answer).

  • 我们只需要替换性别、城市等字符变量.不需要替换年龄和收入等数值变量的值,因为这些值已经是正确的格式.这些已采用正确格式的数值变量不在df_lookup中.

推荐答案

New edition 我会提供这个tidyverse解决方案(当前版本包含年龄处理):

library(tidyverse) 
df_numeric %>% 
  mutate(across(-yearly_income, as.character)) %>% 
  pivot_longer(-c("yearly_income", "age") ) %>% 
  left_join(mutate(df_lookup, number = as.character(number)), by = c("name" = "variable", "value" = "number")) %>% 
  select(-value) %>% 
  pivot_wider(id_cols = c("yearly_income", "age"), values_from = label, names_from = name)

# A tibble: 5 x 6
  yearly_income age   gender city     fav_colour over_100_more_vars
          <dbl> <chr> <chr>  <chr>    <chr>      <chr>             
1         55000 22    Male   New York Red        <NA>              
2        122000 31    Female Sydney   Blue       <NA>              
3        101000 41    Male   New York Blue       <NA>              
4         76000 19    Female London   Red        <NA>              
5         32000 64    Male   Paris    Purple     <NA>

R相关问答推荐

如何将具有重复名称的收件箱合并到R中的另一列中,而结果不同?

如何创建构成多个独立列条目列表的收件箱框列?

在R底座中更改白天和夜晚的背景 colored颜色

R Tidymodels textercipes-使用spacyR进行标记化-如何从生成的标记列表中删除标点符号

使用gggrassure减少地块之间的空间

如何编辑ggplot的图例字使用自定义对象(gtable)?'

条形图和在Ploly中悬停的问题

如何对2个列表元素的所有组合进行操作?

`lazy_dt`不支持`dplyr/across`?

将选定的索引范围与阈值进行比较

如何使这些react 表对象相互独立?

扩展R中包含列表的数据框

根据r中每行中的日期序列,使用列名序列创建新列

有没有办法通过str_Detect()或其他字符串匹配函数来连接两个长度不等的数据帧?

如何显示准确的p值而不是<;0.001*?

如何移动点以使它们的打印不重叠

隐藏基于 case 总数的值

条形图中的条形图没有try 赋予它们的 colored颜色

对一个数据帧中另一个数据帧中的值进行计数

识别部分重复行,其中一行为NA,其重复行为非NA