简体   繁体   English

在特定条件下,用表(或矩阵)中的值填充列

[英]Fill a column with values from a table (or matrix) given certain conditions

I'm not sure exactly what this would be called, so I'm struggling to search for it. 我不确定这将被称为什么,所以我正在努力寻找它。 Sorry if this is a repeated question, that would be why. 抱歉,如果这是一个重复的问题,那就是为什么。

I have a data 43x17 data matrix,lets call it A, with four levels in the "site" column. 我有一个数据43x17数据矩阵,我们称其为A,在“站点”列中具有四个级别。 I also have another 4x3 matrix containing coordinates for each site, lets call it B. I'd like to create a column in A where each site is assigned the coordinate listed in B. 我还有另一个4x3矩阵,其中包含每个站点的坐标,我们称其为B。我想在A中创建一列,为每个站点分配B中列出的坐标。

Using the data below as an example, I'd like to make a new column in A and populate it with values from Coordinate1 (in B) with the associated "Site" value. 以下面的数据为例,我想在A中创建一个新列,并使用来自Coordinate1(在B中)的值和关联的“站点”值填充它。

A <- matrix(c(1:4), nrow = 12, ncol = 3, byrow = TRUE,
               dimnames = list(c(1:12),
                               c("Site", "D.2", "D.3")))
A
B<- matrix(c(1:4), nrow = 4, ncol = 3, byrow = TRUE,
           dimnames = list(c(1:4),
                           c("Site", "Coordinate1", "Coordinate2")))
B

I'm sure this is simple, but I'm blanking on what to call this and have apparently forgotten how to do it! 我敢肯定这很简单,但是我在称呼它什么方面空白了,显然已经忘记了怎么做!

There are several options. 有几种选择。

Without conversion to data.frame and using merge() as suggested in most of the comments, you may try to use match() and cbind() : 如果不转换为data.frame并按照大多数注释中的建议使用merge() ,则可以尝试使用match()cbind()

cbind(A, B[match(A[, "Site"], B[, "Site"]), "Coordinate1", drop = FALSE])
  Site D.2 D.3 Coordinate1 1 1 2 3 2 2 4 1 2 1 3 3 4 1 4 4 2 3 4 3 5 1 2 3 2 6 4 1 2 1 7 3 4 1 4 8 2 3 4 3 9 1 2 3 2 10 4 1 2 1 11 3 4 1 4 12 2 3 4 3 

or, to cbind all columns of B 或者,绑定B所有列

cbind(A, B[match(A[, "Site"], B[, "Site"]), ])
  Site D.2 D.3 Site Coordinate1 Coordinate2 1 1 2 3 1 2 3 2 4 1 2 4 1 2 3 3 4 1 3 4 1 4 2 3 4 2 3 4 5 1 2 3 1 2 3 6 4 1 2 4 1 2 7 3 4 1 3 4 1 8 2 3 4 2 3 4 9 1 2 3 1 2 3 10 4 1 2 4 1 2 11 3 4 1 3 4 1 12 2 3 4 2 3 4 

Conversion to data.frame and merge() 转换为data.frame和merge()

 as.matrix(merge(data.frame(A), data.frame(B), by = "Site", sort = FALSE))

distorts the original order of rows: 扭曲行的原始顺序:

  Site D.2 D.3 Coordinate1 Coordinate2 [1,] 1 2 3 2 3 [2,] 1 2 3 2 3 [3,] 1 2 3 2 3 [4,] 4 1 2 1 2 [5,] 4 1 2 1 2 [6,] 4 1 2 1 2 [7,] 3 4 1 4 1 [8,] 3 4 1 4 1 [9,] 3 4 1 4 1 [10,] 2 3 4 3 4 [11,] 2 3 4 3 4 [12,] 2 3 4 3 4 

Using right join from the data.table package maintains the order of rows 使用data.table包中的右连接可维护行的顺序

library(data.table)
as.matrix(data.table(B)[data.table(A), on = "Site"])
  Site Coordinate1 Coordinate2 D.2 D.3 [1,] 1 2 3 2 3 [2,] 4 1 2 1 2 [3,] 3 4 1 4 1 [4,] 2 3 4 3 4 [5,] 1 2 3 2 3 [6,] 4 1 2 1 2 [7,] 3 4 1 4 1 [8,] 2 3 4 3 4 [9,] 1 2 3 2 3 [10,] 4 1 2 1 2 [11,] 3 4 1 4 1 [12,] 2 3 4 3 4 

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

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