Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中多个Vlookup的循环_R_Loops_Dataframe_Vlookup - Fatal编程技术网

R中多个Vlookup的循环

R中多个Vlookup的循环,r,loops,dataframe,vlookup,R,Loops,Dataframe,Vlookup,我有一个如下所示的数据框: lhs1=c("A","D","C","B") lhs2=c("B","A","C","I") lhs3=c("I","B","A","D") lhs4=c("A","C","B","D") df <- data.frame(lhs1,lhs2,lhs3,lhs4) lhs1 lhs2 lhs3 lhs4 1 A B I A 2 D A B C 3 C C A B 4 B

我有一个如下所示的数据框:

lhs1=c("A","D","C","B")
lhs2=c("B","A","C","I")
lhs3=c("I","B","A","D")
lhs4=c("A","C","B","D")

df <- data.frame(lhs1,lhs2,lhs3,lhs4)



   lhs1 lhs2 lhs3 lhs4
1    A    B    I    A
2    D    A    B    C
3    C    C    A    B
4    B    I    D    D
lhs1    lhs2    lhs3    lhs4    lhs1.sale   lhs2.sale   lhs3.sale   lhs4.sale
   A       B       I       A       12         23            42         12
   D       A       B       C       35         12            23         34
   C       C       A       B       34         34            12         23
   B       I       D       D       23         42            35         35
请帮助我创建一个循环,这样就可以为R创建多个vlookup。

试试这个

df[paste(names(df), "sale", sep = ".")] <- look$sale[match(unlist(df), look$category)]
df
#   lhs1 lhs2 lhs3 lhs4 lhs1.sale lhs2.sale lhs3.sale lhs4.sale
# 1    A    B    I    A        12        23        42        12
# 2    D    A    B    C        35        12        23        34
# 3    C    C    A    B        34        34        12        23
# 4    B    I    D    D        23        42        35        35
试试这个

df[paste(names(df), "sale", sep = ".")] <- look$sale[match(unlist(df), look$category)]
df
#   lhs1 lhs2 lhs3 lhs4 lhs1.sale lhs2.sale lhs3.sale lhs4.sale
# 1    A    B    I    A        12        23        42        12
# 2    D    A    B    C        35        12        23        34
# 3    C    C    A    B        34        34        12        23
# 4    B    I    D    D        23        42        35        35

这是一个data.table解决方案

library(data.table)
setkey(setDT(look),category)      # convert look to data.table; index on category
cn <- paste0(names(df),".sales")  # names for the new columns
setDT(df)[,c(cn):=lapply(.SD,function(col)look[col]$sale)]
df
#    lhs1 lhs2 lhs3 lhs4 lhs1.sales lhs2.sales lhs3.sales lhs4.sales
# 1:    A    B    I    A         12         23         42         12
# 2:    D    A    B    C         35         12         23         34
# 3:    C    C    A    B         34         34         12         23
# 4:    B    I    D    D         23         42         35         35

这是一个data.table解决方案

library(data.table)
setkey(setDT(look),category)      # convert look to data.table; index on category
cn <- paste0(names(df),".sales")  # names for the new columns
setDT(df)[,c(cn):=lapply(.SD,function(col)look[col]$sale)]
df
#    lhs1 lhs2 lhs3 lhs4 lhs1.sales lhs2.sales lhs3.sales lhs4.sales
# 1:    A    B    I    A         12         23         42         12
# 2:    D    A    B    C         35         12         23         34
# 3:    C    C    A    B         34         34         12         23
# 4:    B    I    D    D         23         42         35         35