我正在尝试根据两列从数据框中的列返回一个值。 所以这是一个例子:
year<-c(rep("2010",3),rep("2011",3),rep("2012",3))
var1<-c(rep(c("aaa","bbb","ccc"),3))
var2<-c(0.5,0.45,0.1,0.3,0.23,0.58,0.8,0.75,0.44)
df1<-data.frame(year,var1,var2)
ID<-c("z1","z2","z3","z4","z5")
year<-c(2011,2011,2012,2010,2010)
var3<-c("aaa","aaa","ccc","bbb","ccc")
df2<-data.frame(ID,year,var3)
如果我只有一个条件,我会做这样的事情:
df2$rate<-df1$var2[match(df2$year,df1$year)]
问题是我有两个,我尝试了一些如:
df2$rate<-df1$var2[match(df2$year,df1$year)& match(df2$var3,df1$var1)]
但不幸的是,我没有尝试过。有没有根据两个条件返回值的简单方法?
对于这个特殊情况,我可以将变量和年份合并为一列并应用之前的方法,但我正在寻找更优雅的解决方案。
df1$join<-paste(df1$var1,df1$year,sep="")
df2$join2<-paste(df2$var3,df2$year,sep="")
df2$rate<-df1$var2[match(df2$join2,df1$join)]
提前感谢任何消化
答案 0 :(得分:1)
您可以尝试merge
merge(df2, df1, by.x=c("year", "var3"), by.y=c("year", "var1"), sort=FALSE)
# year var3 ID var2
#1 2011 aaa z1 0.30
#2 2011 aaa z2 0.30
#3 2012 ccc z3 0.44
#4 2010 bbb z4 0.45
#5 2010 ccc z5 0.10
或者您可以使用left_join
dplyr
library(dplyr)
colnames(df2)[3] <- "var1"
year
列为factor
。将其更改为character
班级或numeric
。您可以在stringsAsFactors=FALSE
data.frame(..)
df1$year <- as.character(df1$year)
df2$year <- as.character(df2$year)
left_join(df2,df1)
#Joining by: c("year", "var1")
#ID year var1 var2
#1 z1 2011 aaa 0.30
#2 z2 2011 aaa 0.30
#3 z3 2012 ccc 0.44
#4 z4 2010 bbb 0.45
#5 z5 2010 ccc 0.10