user3036416
user3036416

Reputation: 1255

Merging data frame different rows and different columns

I have a data frame like this

df1

  a       b       c

  10      11    10
  00021   11    00021
  022     1     00021
  00054   32    00054

and another one like this

df1

  name     n

  10       1
  00021    2
  022      1
  00054    1

that counts how many times the variables in the column c of df1 appear. I would like to add to the first data frame one column like n in df2; I tried to merge the two data frame but without success..

Upvotes: 0

Views: 125

Answers (1)

Sven Hohenstein
Sven Hohenstein

Reputation: 81683

You can use merge:

merge(df1, df2, by.x = "c", by.y = "name")

      c     a  b n
1 00021 00021 11 2
2 00021   022  1 2
3 00054 00054 32 1
4    10    10 11 1

Upvotes: 2

Related Questions