考虑其他相应列合并 R 中的两列?
Merging two columns in R considering other corresponding columns?
我正在使用 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 很常见。
我自己生成了一些数据,你可以用你的替换它们。在这里您需要合并两个文件;先垂直,然后水平。最后,根据第一列对它们进行排序。
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
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
这可以通过 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
我正在使用 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 很常见。
我自己生成了一些数据,你可以用你的替换它们。在这里您需要合并两个文件;先垂直,然后水平。最后,根据第一列对它们进行排序。
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
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
这可以通过 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