Preface: I am used to Pandas but am new to R. I am sure this question covers very basic R. I have such a little idea I'm not even sure what to search This is most likely a duplicate - Please let me know and I'll gladly delete and go find the answer.
I am aware there is next to no "what have you tried here". If you are even able to point me to some documentation I will delete this and go read it.
Many thanks for your help :)
I have two dataframes:
key=
letter id
A 0
B 1
C 2
and
data=
name value
A 24
C 46
B 36
C 5
I want to write a function to match and find data$name == key$letter
, and return the corresponding value in key$id
in a new column in data
.
The output dataframe would look as so:
data
name value id
A 24 0
C 46 2
B 36 1
C 5 2
Essentially I want to use key
as a reference data frame.
I could do this trivially in Python using np.where
apply
match
and loc
but in R, I have no idea where to begin.
I have read through the following questions on which
apply
and index
, and compare
but I have been unable to figure out how to write this statement.
Return indices of rows whose elements (columns) all match a reference vector Finding the index inside a vector satisfying a condition How to find the indices of an R list meeting multiple criteria Is there an R function for finding the index of an element in a vector? Compare two data.frames to find the rows in data.frame 1 that are not present in data.frame 2
Can you help?