1

I have dataframe

df1 ->

ID Name
1  Test1
2  Test2
3  Test3

I have another dataframe df2

df2 ->

ID Name
1  Char
2  Float
3  Decimal
4  String

Now I want to update df1 name values based on names from df2 using ID

Now my df1 output should be

ID Name
1  Char
2  Float
3  Decimal 

Please let me know how to acheive this.

1
  • 1
    I would try to be clearer about whether you want to do a lookup by ID, or just match identically numbered rows. Commented Nov 17, 2015 at 2:13

4 Answers 4

4

We can use match

df1$Name <- df2$Name[match(df1$ID, df2$ID)]

Or as @thelatemail mentioned, this can be solved with merge

merge(df1["ID"], df2, all.x=TRUE)
Sign up to request clarification or add additional context in comments.

1 Comment

Also merge - merge(df1["ID"], df2, all.x=TRUE)
2

Another option:

library(qdapTools)
df1$Name <- df1$ID %l% df2

Comments

0

Not sure if you were looking for something besides simply this:

df1$Name <- df2$Name[1:3]

Comments

0

Use the ID to get the right names.

df1$Name <- df2$Name[df1$ID]

Note that this solution is applicable if the number of observations in df1 is less than or equals to the number of observations in df2, otherwise, you will end up with NA. Also, the ID is the row number.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.