0

I have two dataframes both have 2 columns (2 variables).

I was thinking that I could use something similar to vlookup in excel? And maybe I could create a new dataset using for loop and put the quotients in this dataset I don't know how exactly I could do that.

(I ALSO NEED TO PUT THE VALUES IN A DATASET so the suggested post does not answer my question completely)

example:

dataframe1
number amount
 1  2
 2  3
 3  4


dataframe2
number amount
 1  5
 2  6
 4  2
   
 
qwerqwer
  • 1
  • 2
  • Does this answer your question? [Simple lookup to insert values in an R data frame](https://stackoverflow.com/questions/17844143/simple-lookup-to-insert-values-in-an-r-data-frame) – zephryl Feb 25 '22 at 02:01
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. It's more difficult to answer abstractly. – MrFlick Feb 25 '22 at 02:07

1 Answers1

2

Assuming that you imported Dataframe1 as Dataframe1, and Dataframe2 as Dataframe2, and both are data.frame.

library(tidyverse)
Dataframe1 %>%
  inner_join(Dataframe2 %>% rename(Amount2 = Amount),
             by="id") -> Dataframe

At this point you can perform your operation

library(tidyverse)
Dataframe %>%
  mutate(result = Amount/Amount2) -> Dataframe

and check if the column result is what you were looking for.

To find the highest ratio:

Dataframe$result %>% max(na.rm = T)

But there are many other ways to record this value; this is the most straightforward.

GiulioGCantone
  • 195
  • 1
  • 10