简体   繁体   English

向量加法和向量索引

[英]Vector addition with vector indexing

This may well have an answer elsewhere but I'm having trouble formulating the words of the question to find what I need. 这很可能在其他地方有一个答案,但是我在表达问题的措词以找到我需要的东西时遇到了麻烦。

I have two dataframes, A and B, with A having many more rows than B. I want to look up a value from B based on a column of A, and add it to another column of A. Something like: 我有两个数据框A和B,A的行比B多。我想根据A的一列从B查找一个值,并将其添加到A的另一列。

A$ColumnToAdd + B[ColumnToMatch == A$ColumnToMatch,]$ColumnToAdd

But I get, with a load of NAs: 但是我得到了很多NA:

Warning in `==.default`: longer object length is not a multiple of shorter object length

I could do it with a messy for-loop but I'm looking for something faster & elegant. 我可以使用凌乱的for循环来做到这一点,但我正在寻找更快,更优雅的产品。

Thanks 谢谢

If I understood your question correctly, you're looking for a merge or a join , as suggested in the comments. 如果我正确理解了您的问题,那么您正在寻找合并联接 ,如注释中所建议。

Here's a simple example for both using dummy data that should fit what you described. 这是一个简单的示例,说明两者都应使用您所描述的虚拟数据。

library(tidyverse)

# Some dummy data
ColumnToAdd <- c(1,1,1,1,1,1,1,1)
ColumnToMatch  <- c('a','b','b','b','c','a','c','d')
A <- data.frame(ColumnToAdd, ColumnToMatch)
ColumnToAdd <- c(1,2,3,4)
ColumnToMatch <- c('a','b','c','d')
B <- data.frame(ColumnToAdd, ColumnToMatch)

# Example using merge
A %>% 
  merge(B, by = c("ColumnToMatch")) %>%  
  mutate(sum = ColumnToAdd.x + ColumnToAdd.y)                    

# Example using join
A %>% 
  inner_join(B, by = c("ColumnToMatch")) %>% 
  mutate(sum = ColumnToAdd.x + ColumnToAdd.y)    

The advantages of the dplyr versions over merge are : dplyr版本优于合并的优点是

  • rows are kept in existing order 行按现有顺序保留
  • much faster 快多了
  • tells you what keys you're merging by (if you don't supply) 告诉您要合并的键(如果不提供)
  • also work with database tables. 也可以使用数据库表。

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

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