【问题标题】:Merging two columns in R considering other corresponding columns?考虑其他相应的列合并R中的两列?
【发布时间】:2017-02-20 18:43:38
【问题描述】:

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

这是两个矩阵:

矩阵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

矩阵 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 是常见的。

【问题讨论】:

  • 您能举例说明一下输入是什么,输出是什么?
  • @discipulus 我已经更新了问题
  • 请使用提供的示例数据显示预期输出。描述不完全清楚。
  • “合并”是什么意思,因为第 1 列由数值组成。你认为 99.0061 和 99.0062 相等吗?
  • @UweBlock,不,99.0061 和 99.0062 应该在同一列中一个接一个。我将添加结果矩阵的样子

标签: r matrix merge multiple-columns


【解决方案1】:

这可以通过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

【讨论】:

  • 这很简单。完美运行:)
【解决方案2】:
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

【讨论】:

    【解决方案3】:

    我自己生成了一些数据,你可以用你的替换它们。在这里,您需要合并两个文件;先垂直后水平。最后,根据第一列对它们进行排序。

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-22
      • 1970-01-01
      • 2018-09-25
      • 1970-01-01
      • 2015-04-18
      • 2021-08-08
      相关资源
      最近更新 更多