简体   繁体   中英

R: use values in existing columns to index position of values in another dataframe

(Apologies if duplicate: I've had a look and can't find this question asked).

I'm trying to create a new column in a dataframe1 which will be based on the values in dataframe2. For each row, the index of the value I want from dataframe2 is specified in two columns: lineno is row, and position is column.

df1:

      lineno position
1      1        1
2      1        9
3      2       11
4      2        3
5      2       17

This seemed straightforward; I thought I could do it with:

df1 %>% mutate(new_col = df2[lineno, position])

But this seems to return the entire dataframe? For each row in df1$new_col the result is <data.frame [1,000 × 5,714]>

Any ideas?

I suggest you add purrr to your repertoire:

df1 <- read.table(text='      lineno position
1      1        1
2      1        9
3      2       11
4      2        3
5      2       10', header=TRUE)
df2 <- mtcars

library(dplyr)
library(purrr)
df1 %>% mutate(
  new_col = map2(lineno, position, ~ df2[.x,.y])
)
#   lineno position new_col
# 1      1        1      21
# 2      1        9       1
# 3      2       11       4
# 4      2        3     160
# 5      2       10       4

You can be "safer" if you use one of the specific map2_ variants, such as map2_dbl .

对于dplyr您可以rowwise添加

df1 %>%rowwise()%>%  mutate(new_col = df2[lineno, position])

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