简体   繁体   中英

Select row in dataframe based on row in another dataframe in R

Problem

I want to retrieve a value in df2 based on the value of the first row and first column in df1 .

The first row and first column in df1 has to match a value in the second column in df2 .

I'm unsure how this can be done with R.

x1 <- rep(9:40)
x2 <- seq(32, 1, by = -1)
df1 <- data.frame(x1, x2)

y1 <- seq(10)
y2 <- seq(153, 0, by = -16)
df2 <- data.frame(y1,y2)

Expected Output

# df1[1, 1] == 9 and 
# df2[10, 2] == 9,
# therefore

> df2[10, 1]
[1] 10

Straightforward:

df2[df2$y2 == df1[1, 1], 1]

Data

x1 <- 9:40
x2 <- 32:1
df1 <- data.frame(x1, x2)

y1 <- 1:10
y2 <- seq(153, 0, by = -16)
df2 <- data.frame(y1, y2)

I may not understand your question because the answer I'm going to give us is quite easy. What if you call the 'cell' present at the intersection between the first row and the first column of df1 : df1[1,1] .

Then, I'd just call it in the way df2[df1[1,1],1]

Did I miss something?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM