考虑到其他相应的列,在R中合并两列?

时间:2016-10-12 04:30:09

标签: r matrix merge multiple-columns

我正在使用R并从2个excel表中导入数据,每个表包含3列。第一个矩阵包含3列(1-3)和380行,第二个矩阵包含3列和365行。第2列和第3列始终是与第一列对应的值。我想将两个矩阵的第一列合并到一个列中,这样在合并后,两列中相同的值只是被替换(它们不应该一个接一个地在各个行中)并且列被排列在一个列中。升序。此外,主要条件应该是每个矩阵的第2,3列(即第1列的值)应相应地重新排列,但不应合并。如果第一列中有一些值(合并后生成),其值在相应列中不存在,则应将其替换为零。我已经完成了第一列的合并和重新排列,但我无法在其他列中进行相应的更改。我该怎么回事?

以下是两个矩阵:

Matrix A

92.6691     1076.5      0.48
93.324      1110.1      0.5
96.9597     1123.3      0.5
97.7539     968.4       0.43
98.992      1006.1      0.45
99.0061     5584.6      2.49
101.0243    1555.7      0.69
101.0606    12821.2     5.72
102.1221    972         0.43

Matrix B

95.4466     974.2       0.43
99.0062     4721.9      2.06
100.0321    1040.1      0.45
101.0241    2115.8      0.92
101.0606    15202.8     6.64
102.2736    945.3       0.41
108.4273    1059.7      0.46
115.0397    25106.3     10.96
115.0761    54740       23.9

合并后,结果应为单个矩阵:

Column 1 - Merged 1st columns of matrices A and B (ascending order)
Column 2 - Rearranged based on change in row positions of column 1 in matrix A
Column 3 - Rearranged based on change in row positions of column 1 in matrix A
Column 4 - Rearranged based on change in row positions of column 1 in matrix B
Column 5 - Rearranged based on change in row positions of column 1 in matrix B

以下是结果矩阵:

92.6691     1076.5      0.48      0        0
93.324      1110.1      0.5       0        0
95.4466     0           0         974.2    0.43
96.9597     1123.3      0.5       0        0
97.7539     968.4       0.43      0        0
98.992      1006.1      0.45      0        0
99.0061     5584.6      2.49      0        0
99.0062     0           0         4721.9   2.06
100.0321    0           0         1040.1   0.45
101.0241    0           0         2115.8   0.92
101.0243    1555.7      0.69      0        0
101.0606    12821.2     5.72      15202.8  6.64
102.1221    972         0.43      0        0
102.2736    0           0         945.3    0.41
108.4273    0           0         1059.7   0.46
115.0397    0           0         25106.3  10.96
115.0761    0           0         54740    23.9

请注意,在矩阵A和B中,值101.0606很常见。

3 个答案:

答案 0 :(得分:2)

这可以通过merge()轻松完成。

# read your data:
read.table(
         t="92.6691     1076.5      0.48
            93.324      1110.1      0.5
            96.9597     1123.3      0.5
            97.7539     968.4       0.43
            98.992      1006.1      0.45
            99.0061     5584.6      2.49
            101.0243    1555.7      0.69
            101.0606    12821.2     5.72
            102.1221    972         0.43") -> M1
read.table(
         t="95.4466     974.2       0.43
            99.0062     4721.9      2.06
            100.0321    1040.1      0.45
            101.0241    2115.8      0.92
            101.0606    15202.8     6.64
            102.2736    945.3       0.41
            108.4273    1059.7      0.46
            115.0397    25106.3     10.96
            115.0761    54740       23.90") -> M2

# merge data -- note `all = TRUE` 
result <- merge(M1,M2,by = "V1", all = TRUE)

# replace na with 0
result[is.na(result)] <- 0

result
#        V1    V2.x V3.x    V2.y  V3.y
# 1   92.67  1076.5 0.48     0.0  0.00
# 2   93.32  1110.1 0.50     0.0  0.00
# 3   95.45     0.0 0.00   974.2  0.43
# 4   96.96  1123.3 0.50     0.0  0.00
# 5   97.75   968.4 0.43     0.0  0.00
# 6   98.99  1006.1 0.45     0.0  0.00
# 7   99.01  5584.6 2.49     0.0  0.00
# 8   99.01     0.0 0.00  4721.9  2.06
# 9  100.03     0.0 0.00  1040.1  0.45
# 10 101.02     0.0 0.00  2115.8  0.92
# 11 101.02  1555.7 0.69     0.0  0.00
# 12 101.06 12821.2 5.72 15202.8  6.64
# 13 102.12   972.0 0.43     0.0  0.00
# 14 102.27     0.0 0.00   945.3  0.41
# 15 108.43     0.0 0.00  1059.7  0.46
# 16 115.04     0.0 0.00 25106.3 10.96
# 17 115.08     0.0 0.00 54740.0 23.90

答案 1 :(得分:1)

df3 <- merge(df1,df2,all.x=T,all.y=T)
df3[is.na(df3)] <- 0

          x       a    b       c     d
1   92.6691  1076.5 0.48     0.0  0.00
2   93.3240  1110.1 0.50     0.0  0.00
3   95.4466     0.0 0.00   974.2  0.43
4   96.9597  1123.3 0.50     0.0  0.00
5   97.7539   968.4 0.43     0.0  0.00
6   98.9920  1006.1 0.45     0.0  0.00
7   99.0061  5584.6 2.49     0.0  0.00
8   99.0062     0.0 0.00  4721.9  2.06
9  100.0321     0.0 0.00  1040.1  0.45
10 101.0241     0.0 0.00  2115.8  0.92
11 101.0243  1555.7 0.69     0.0  0.00
12 101.0606 12821.2 5.72 15202.8  6.64
13 102.1221   972.0 0.43     0.0  0.00
14 102.2736     0.0 0.00   945.3  0.41
15 108.4273     0.0 0.00  1059.7  0.46
16 115.0397     0.0 0.00 25106.3 10.96
17 115.0761     0.0 0.00 54740.0 23.90

数据

df1

x          a    b
92.6691   1076.5    0.48
93.324    1110.1    0.5
96.9597   1123.3    0.5
97.7539    968.4    0.43
98.992    1006.1    0.45
99.0061   5584.6    2.49
101.0243    1555.7  0.69
101.0606    12821.2 5.72
102.1221    972     0.43
df2
x              c    d
95.4466    974.2    0.43
99.0062   4721.9    2.06
100.0321    1040.1  0.45
101.0241    2115.8  0.92
101.0606    15202.8 6.64
102.2736    945.3   0.41
108.4273    1059.7  0.46
115.0397    25106.3 10.96
115.0761    54740   23.9

答案 2 :(得分:0)

我自己生成了一些数据,你可以用你的数据替换它们。在这里,您需要合并两个文件;首先垂直然后水平。最后,根据第一栏订购。

set.seed(42)
# Load data 1
dat1<- as.data.frame(matrix(rexp(30), 10))

# Inly keep unique rows
dat1 <- unique(dat1)

set.seed(24)
# Load data 2
dat2 <-as.data.frame(matrix(rexp(30), 10))

# Inly keep unique rows
dat2 <- unique(dat2)

# Copy it in temp 
dat2n <-dat2

# sed second and third column to 0s
dat2n[,2:3] <- 0    
# Concatenate them and keep only unique
dat <- rbind(dat1,dat2n)

# Merge dat and dat2 with respect to column 1 and keep everything in dat

fin.dat <- merge(dat, dat2, by="V1", all.x = TRUE)

# Finally order the dataframe 
fin.dat <- fin.dat[order(fin.dat[,1], decreasing = FALSE),]
# Replace NA with zeros
fin.dat[is.na(fin.dat)] <- 0