我有两个数据框我想根据第一列和另一列找到相应的价格。
v1<-c("Dec 1999", "Oct 1999", "Jul 1999", "Apr 1999", "Jan 1999")
v2<-c("Jan 2000", "Nov 1999", "Aug 1999", "May 1999", "Feb 1999")
v3<-c("Mar 2000", "Jan 2000", "Oct 1999", "Jul 1999", "Apr 1999")
v4<-c("Apr 2000", "Feb 2000", "Nov 1999", "Aug 1999", "May 1999")
v5<-c("May 2000", "Mar 2000", "Dec 1999", "Sep 1999", "Jun 1999")
Df1<-data.frame(v1, v2, v3, v4, v5)
colnames(Df1) <- c("condition1", "condition2.1", "condition2.2", "condition2.3", "condition2.4")
v6<-c("Dec 1999", "Nov 1999", "Jul 1999", "Apr 1999", "Jan 1999")
v7<-c("Jan 2000", "Feb 2000", "Aug 1999", "Feb 1999", "Feb 1999")
df2<- data.frame(v6,v7)
colnames(df2) <- c("col1", "Col2")
df2$Prices<-seq(1,length(df2$col1),by=1)
colnames(df2)[3] <- c( "prices")
我想创建一个包含以下内容的表:
df $ condidition1 | condition2.1 | (来自df2 $ price的相应的rpice)| condition2.2 | (来自df2 $ price的相应的rpice)| ....
你会怎么做呢?我厌倦了一些不同的东西,似乎没有工作。
答案 0 :(得分:1)
这不是很清楚,你应该添加你期望的输出。
但我认为您正在寻找类似merge
的内容。
也许:
> merge(df1, df2, by.x = c("v1", "v2"), by.y = c("col1", "col2"), all.x = TRUE)
v1 v2 v3 v4 v5 prices
1 Apr 1999 May 1999 Jul 1999 Aug 1999 Sep 1999 NA
2 Dec 1999 Jan 2000 Mar 2000 Apr 2000 May 2000 1
3 Jan 1999 Feb 1999 Apr 1999 May 1999 Jun 1999 5
4 Jul 1999 Aug 1999 Oct 1999 Nov 1999 Dec 1999 3
5 Oct 1999 Nov 1999 Jan 2000 Feb 2000 Mar 2000 NA