0

I have two dataframes. The first dataframe, DF 1, has the relevant data and is the dataframe I would like to add a conditional column for. The other dataframe, DF 2, has the information on which the new column in DF A is to be created.

Here is an illustration of what I am trying to accomplish.

DF 1:

a = c("A", "A", "A", "B", "B", "B")
b = c(1,200,10,12,35,45)
df1 = cbind(a,b)
df1 = as.data.frame(df1)
df1

DF 2:

c = c(A,B)
d = c("Al", "Bob")
df2 = cbind(c,d)
df2 = as.data.frame(df2)
df2

I am aiming to create a conditional column in DF 1 so that if a row has "A," the value for that new column for that row will be "Al." Similarly, if it is "B," then the value for the new column would be "Bob."

The result would look like this:

e = c("A", "A", "A", "B", "B", "B")
f = c(1,200,10,12,35,45)
g = c("Al", "Al", "Al, "Bob", "Bob", "Bob")
df1_new = cbind(e,f,g)
df1_new = as.data.frame(df1_new)
df1_new

For a scenario with only two variables, I know that I can use mutate with ifelse. However, I have around 900 variables and it would too cumbersome to hand code all of them out. Thus, I'm hoping to find a way to get DF 1 to use the information in DF 2 so that the new column can be created automatically.

Any insight is greatly appreciated.

Tee
  • 149
  • 8

0 Answers0