0

Sorry if this has been asked before. I am fairly new to R, and strugling a tad with formulating the question correctly.

I have two dataframes imported from a csv, looking similar to this:

df1: 
    date time vdd temp seat volume output
    ...  ...  1.8 25   1    ...    ... 
    ...  ...  1.8 -40  0    ...    ... 
    ...  ...  ... ...  ...  ...    ... 

And

df2:
    seat temp vdd noise
    0    -40  1.8 5.3E-09
    1    25   1.8 4.9E-09
    .     .    .   .  

I want the second dataframe to work as a lookup table, to create a new column in the first dataframe, producing a result like this:

df1: 
    date time vdd temp seat volume output noise
    ...  ...  1.8 25   1    ...    ...    4.9E-09
    ...  ...  1.8 -40  0    ...    ...    5.3E-09
    ...  ...  ... ...  ...  ...    ...    ...

I have tried looking at merge and match, but I can't wrap my head around how to produce the result I want.

Thanks for any help I can get.

0

2 Answers 2

1

You can easily use merge() as you mentioned in your post.

merge(df1, df2, by=c("seat", "temp", "vdd"))

By default this will keep all records from the first data frame and only corresponding records from the second. To keep all records from both, use all=T.

Alternatively you can omit the by= argument and it will merge on all common columns.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I tried using merge, but I had several problems - but my problem was generated by not having the same name of the collumns.
0

There are several ways you could approach this, but I find this to be the easiest:

  1. Install this package if you don't already have it:

install.packages("sqldf")

It lets you use SQL commands to transform data.

  1. Join the data as you see fit:

require(sqldf)

output_table <- sqldf("select df1.*, df2.noise from df1 join df2 on df1.seat = df2.seat")

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.