说我们有这个数据框:
lookup <- data.frame(
"value" = c("1", "2","F","M",0,1),
"label" = c("May2018", "Dec2019",'Female','Male',"No","Yes"),
"var_name" = c("V1","V1","V2","V2","V3","V3")
)
此查询表:
df[,paste0('V1','_unc')] <- lookup$label[match(unlist(df[,'V1']), lookup[,'value'])]
df[,paste0('V2','_unc')] <- lookup$label[match(unlist(df[,'V2']), lookup[,'value'])]
df[,paste0('V3','_unc')] <- lookup$label[match(unlist(df[,'V3']), lookup[,'value'])]
> df
ID V1 V2 V3 V1_unc V2_unc V3_unc
1 001 1 F 0 May2018 Female No
2 002 1 M 1 May2018 Male May2018
3 003 2 F 1 Dec2019 Female <NA>
我想基于查找表创建“未编码”变量
我尝试了此操作,但是由于某些原因,它不适用于V3,有任何线索吗?
for (var in c('V1','V2','V3')) {
df[,paste0(var,'_unc')] <- lookup$label[match(unlist(df[,var]), lookup[,'value'])]
}
我也在循环中进行了尝试(与V3相同):
var formCol = new FormCollection(new Dictionary<string, Microsoft.Extensions.Primitives.StringValues>
{
{ "Field1", "String Value 1" },
{ "Field2", "String Value 2" },
{ "Field3", "String Value 3" }
});
// Call create controller with test data
_controller.Create(formCol);