Find that values that are immediately below a give

2019-07-12 18:06发布

I have two data frames:

a <- c(10, 20, 30)
c <- c(1, 50, 100)

df1 <- data.frame(cbind(a, b, c))

x <- c(80, 30, 15)
z <- c(10, 46, 99)

df2 <- data.frame(cbind(x, y, z))

I want to find the values in c that are immediately below the values in z, and then return the equivalent values in a.

So matching z to c will give me the locations: 1, 1, 2, and I want to output those locations from a (i.e 10, 10, 20)

Edit: For each value in z I want to find the location of the value that is below it in c, then return the value in a based on that location

标签: r
1条回答
三岁会撩人
2楼-- · 2019-07-12 18:48

You can use outer with the comparison <. Then colSums should add the TRUEs and give you your answer given that df1 is ordered on c, i.e.

colSums(outer(df1$c, df2$z, `<`))
#[1] 1 1 2

or

df1$a[colSums(outer(df1$c, df2$z, `<`))]
#[1] 10 10 20
查看更多
登录 后发表回答