从以其他查找列为条件的查找列中获取值,该查找列与其他数据框中的列名匹配
take value from lookup column conditional on other lookup column that is matching column names from other dataframe
我有这个数据框:
df <- tribble(
~original1, ~original2,
1, 3,
2, 4)
original1 original2
<dbl> <dbl>
1 1 3
2 2 4
我有这个查找 table:
lookup <- structure(list(original_names = c("original1", "original2", "original3"
), handy_names = c("handy_original1", "handy_original2", "handy_original3"
), label_names = c("label_original1", "label_original2", "label_original3"
)), class = "data.frame", row.names = c(NA, -3L))
original_names handy_names label_names
1 original1 handy_original1 label_original1
2 original2 handy_original2 label_original2
3 original3 handy_original3 label_original3
我想将 label_names
作为标签属性分配给数据框 df
到目前为止我已经尝试过:
library(sjlabelled)
library(dplyr)
df %>%
set_label(label = lookup$label_names[lookup$original_names %in% names(df)])
# gives
> get_label(df)
original1 original2
"" ""
我想要的输出
> get_label(df)
original1 original2
"label_original1" "label_original2"
尝试作业
set_label(df) <- lookup$label_names[lookup$original_names %in% names(df)]
-测试
> get_label(df)
original1 original2
"label_original1" "label_original2"
请注意,当我们使用set_label
时,它不是就地赋值,即我们可能需要赋值回'df'
library(dplyr)
df <- df %>%
set_label(label = lookup$label_names[lookup$original_names %in% names(df)])
现在检查
> get_label(df)
original1 original2
"label_original1" "label_original2"
我有这个数据框:
df <- tribble(
~original1, ~original2,
1, 3,
2, 4)
original1 original2
<dbl> <dbl>
1 1 3
2 2 4
我有这个查找 table:
lookup <- structure(list(original_names = c("original1", "original2", "original3"
), handy_names = c("handy_original1", "handy_original2", "handy_original3"
), label_names = c("label_original1", "label_original2", "label_original3"
)), class = "data.frame", row.names = c(NA, -3L))
original_names handy_names label_names
1 original1 handy_original1 label_original1
2 original2 handy_original2 label_original2
3 original3 handy_original3 label_original3
我想将 label_names
作为标签属性分配给数据框 df
到目前为止我已经尝试过:
library(sjlabelled)
library(dplyr)
df %>%
set_label(label = lookup$label_names[lookup$original_names %in% names(df)])
# gives
> get_label(df)
original1 original2
"" ""
我想要的输出
> get_label(df)
original1 original2
"label_original1" "label_original2"
尝试作业
set_label(df) <- lookup$label_names[lookup$original_names %in% names(df)]
-测试
> get_label(df)
original1 original2
"label_original1" "label_original2"
请注意,当我们使用set_label
时,它不是就地赋值,即我们可能需要赋值回'df'
library(dplyr)
df <- df %>%
set_label(label = lookup$label_names[lookup$original_names %in% names(df)])
现在检查
> get_label(df)
original1 original2
"label_original1" "label_original2"