[R] simple "for loop" program for merging datasets?

J S
Thu, 19 Mar 2009 21:35:04 -0000

Dear R community,

I would like to merge two datasets based on the categorical predictor “country”.

Dataset A:

Country           Measure1
Afganistan        1
Afganistan        1
Russia              5
Poland 3
Poland              2

Dataset B:

Country           Measure 2
Russia              2
Afganistan        10
Poland              15

My program does not work:

Country_A<-A$Country
Country_B<-B$Country
Measure2<-B$Measure2

for(i in 1:nrow(B)){
for(j in 1:nrow(A){
w[j]<-ifelse(Country_A[j]= =(Country_B[i]),Measure2[i], NA)
}}

A2<-cbind(B,w)


Thanks, Julia

_________________________________________________________________


	[[alternative HTML version deleted]]