Merge (join) data frames - too many rows in result

前端 未结 3 1725
萌比男神i
萌比男神i 2021-01-16 19:50

I have two data frames(df1 and df2). I want to join them using merge function.

df1 has 3903 rows and df2 has 351 rows.

I want to left join df2 to df1 by a c

3条回答
  •  余生分开走
    2021-01-16 20:31

    I can't be sure without seeing an example of your problem, but usually the syntax is:

    df <- merge(df1, df2, by.all="name_of_column_in_common", all.x=T)
    

    However, if the columns you are matching on have duplicated values, r will match all possible combinations. So,

    df1 <- data.frame(id=c("a","a","b","c"), x1=rnorm(4))
    df2 <- data.frame(id=c("a","a","b"), x2=rnorm(3))
    df <- merge(df1, df2, by.all="id", all.x=T)
    

    Will give you a df of dimensions 6 by 3, as each "a" in df2 has been matched to each "a" in df1, 2 by 2 for 4 permutations.

提交回复
热议问题