我想将值从R中的命名列表映射到数据框。 R中是否有等效的熊猫地图功能?
list = (a=1, b=2)
df = data.frame(a)
df.map(list)
in r?
我想将list的值映射到df。
如果您是指熊猫中的地图,则可以将其应用于这样的Pd系列:
import pandas as pd
x = pd.Series(['a','b','c'])
LIST = {'a':1,'b':2}
x.map(LIST)
x = pd.Series(['a','b','c'])
0 1.0
1 2.0
2 NaN
在R中,您执行:
df = data.frame(x = c("a","b","c"))
LIST = list(a=1,b=2)
as.character(LIST[match(df$x,names(LIST))])
"1" "2" "NULL"
或使用github中的dplyr:
dplyr:::recode(df$x,!!!LIST)
[1] 1 2 NA