r-应该使用哪个()Match()函数根据另一个data.frame中定义的colname提取数据?
随着时间的推移,我有数千个实体的数字数据框。我在同一时间段有另一个数据帧,随着时间的推移有不同的列名:r-应该使用哪个()Match()函数根据另一个data.frame中定义的colname提取数据?,r,match,apply,R,Match,Apply,随着时间的推移,我有数千个实体的数字数据框。我在同一时间段有另一个数据帧,随着时间的推移有不同的列名: > mfdf #date Entity1 Entity2 Entity3 ... EntityN #1988 1 13 16 ... 17 #1989 2 14 3 ... 11 #1990 6 15 8 ... 4 #... ... ... ... ...
> mfdf
#date Entity1 Entity2 Entity3 ... EntityN
#1988 1 13 16 ... 17
#1989 2 14 3 ... 11
#1990 6 15 8 ... 4
#... ... ... ... ... 6
#2018 4 1 8 ... 5
我想根据列名的数据框提取该年的相应实体编号:
> curationdf
#date V1 V2
#1988 Entity64 Entity2
#1989 Entity1 Entity57
#1990 Entity1500 Entity70
#...
#2018 Entity23 Entity9
如果这是在excel中完成的,这将是一个=If(MATCH())作业,但我不熟悉如何在R编程中构造类似的东西。不是这么短,而是一个使用tidyr和dplyr的解决方案:
gather(df,var,val,-year) %>% inner_join(curationdf,"year") %>%
filter((var==v1)|(var==v2)) %>%
mutate(var=ifelse(var==v1,"v1","v2")) %>% spread(var,val)