Applying sapply in two dataframe but selecting different column name

Hi,

I have a problem when implementing sapply in my dataframes.
I wish to index the same column between both of columns and refer one of the column as the list index.

The example for this is here:

df1 = data.frame(
   number = c(1:5), 
   variable = c("A","B","C","D","E"))
df2 = data.frame(
   number = c(1,2,2,3,5), 
   variable = c("AA","BB","CC","DD","EE"))

# I implemented
sapply(df1$number, function(x) which(df2$number == x))

# it returns
[[1]]
[1] 1

[[2]]
[1] 2 3

[[3]]
[1] 4

[[4]]
integer(0)

[[5]]
[1] 5

I wish to return to the AA, BB and etc for each list or even more than one in each list. Maybe someone has a suggestion regarding this? Thank you very much!

Best
IS

I am not sure I understand your goal. Is it something like this?

df1 = data.frame(
   number = c(1:5), 
   variable = c("A","B","C","D","E"))
df2 = data.frame(
   number = c(1,2,2,3,5), 
   variable = c("AA","BB","CC","DD","EE"))
 
sapply(df1$number, function(x) df2[df2$number == x,"variable"])
[[1]]
[1] "AA"

[[2]]
[1] "BB" "CC"

[[3]]
[1] "DD"

[[4]]
character(0)

[[5]]
[1] "EE"
1 Like

Hi, yes! This is the answer I was looking for! Thank you so much!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.