Skip to content
Prev 378603 / 398502 Next

Problem with combining 2 data frame

Hi Javad,
You have a number of problems with your code, such as:
1. you should set df1 and df2 without factors
2. you define a function f(x,y) but the body of the function never refers
to x and y

The following code does what I think you are looking for:

df1 = data.frame(x1 = letters[1:26],x2 = NA,stringsAsFactors = FALSE)
df2 = data.frame(x1 = letters[10:15],x2 = c("1a","2a","3a","4a","5a","6a"),
stringsAsFactors = FALSE)

aa <- sapply( 1:nrow(df2), function(i) { df1$x2[ df1$x1==df2$x1[i] ] <<-
df2$x2[i] } )

HTH,
Eric
On Sat, Feb 16, 2019 at 4:11 AM javad bayat <j.bayat194 at gmail.com> wrote: