繁体   English   中英

基于另一个在 dataframe 中创建新列,并与 R 中的另一个数据集匹配

[英]Create new column in dataframe based on another and matching to another dataset in R

我有以下数据框,我想根据以下条件创建一个新列:

  1. 如果 Price.of.Books 为 BLANK,则 R 将根据 Count.of.Books..Match 中的值查找匹配的数据帧。
  2. 如果 Price.of.Books 不是 BLANK,它将保留该值

在 Excel 中,可以使用 ifelse() 和 ifelse() function 中的 vlookup() 来完成。 在 R 中可以做类似的事情吗? 所以它适用于更大的数据集?

Dataframe

指数 分组 书数..匹配。 图书价格
i1 一个 1 空白的
i2 一个 2 空白的
i3 2 12
i4 6 空白的
i5 C 4 10
i6 C 1 11.5
i7 D 3 8.5
i8 D 6 空白的
i9 4 空白的
df = structure(list(Index = structure(1:9, .Label = c("I1", "I2", 
                    "I3", "I4", "I5", "I6", "I7", "I8", "I9"), class = "factor"), 
                 Grouping = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L
                 ), .Label = c("A", "B", "C", "D", "E"), class = "factor"), 
                 Count.of.Books..Match. = c(1L, 2L, 2L, 6L, 4L, 1L, 3L, 6L, 
                  4L), Price.of.Books = structure(c(5L, 5L, 3L, 5L, 1L, 2L, 
                   4L, 5L, 5L), .Label = c("10", "11.5", "12", "8.5", "BLANK"
                   ), class = "factor")), row.names = c(NA, -9L), class = "data.frame")

匹配数据框

匹配 图书价格
1 6
2 7
3 8
4 9.5
5 12
6 13
match = structure(list(Match = 1:6, Price.of.Books = c(6, 7, 8, 9.5, 12, 13)), class = "data.frame", row.names = c(NA, -6L))

结果应该如下:

指数 分组 书数..匹配。 图书价格 新列
i1 一个 1 空白的 6
i2 一个 2 空白的 7
i3 2 12 12
i4 6 空白的 13
i5 C 4 10 10
i6 C 1 11.5 11.5
i7 D 3 8.5 8.5
i8 D 6 空白的 13
i9 4 空白的 9.5

提前致谢

一个可能的解决方案:

library(dplyr)

df %>% 
  inner_join(match, by = c("Count.of.Books..Match." = "Match")) %>% 
  mutate(Price.of.Books = ifelse(Price.of.Books.x == "BLANK", Price.of.Books.y, 
    Price.of.Books.x), .keep = "unused") 

#>   Index Grouping Count.of.Books..Match. Price.of.Books
#> 1    I1        A                      1            6.0
#> 2    I2        A                      2            7.0
#> 3    I3        B                      2            3.0
#> 4    I4        B                      6           13.0
#> 5    I5        C                      4            1.0
#> 6    I6        C                      1            2.0
#> 7    I7        D                      3            4.0
#> 8    I8        D                      6           13.0
#> 9    I9        E                      4            9.5

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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