Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将两个文件合并为一个新文件_R - Fatal编程技术网

R 将两个文件合并为一个新文件

R 将两个文件合并为一个新文件,r,R,我有2个文件,比如说3列和几行 1 2 10 2 3 20 3 4 30 4 5 40 5 1 50 6 1 60 及 现在我想创建一个第三个文件,其中包含前两个文件的所有值,如果两个文件的第一列和第二列相同,那么在第三个文件中,对应于它们的值应该是,第一个文件第三列中的值必须在新创建文件的第三列中,第二个文件第三列中的值必须在新创建文件的第四列中。 根据上面的例子,答案应该是 1 2 10 0 2 3 20

我有2个文件,比如说3列和几行

1    2    10
2    3    20
3    4    30
4    5    40
5    1    50
6    1    60

现在我想创建一个第三个文件,其中包含前两个文件的所有值,如果两个文件的第一列和第二列相同,那么在第三个文件中,对应于它们的值应该是,第一个文件第三列中的值必须在新创建文件的第三列中,第二个文件第三列中的值必须在新创建文件的第四列中。 根据上面的例子,答案应该是

1  2  10  0
2  3  20  100
3  4  30  45
4  5  40  78
1  8  10   0
5  1  50   0
6  1  60   0
5  2  99   0
6  80 60   0

如果您使用
dput()
发布一个示例,会更容易。我会检查
?merge
是否有帮助或
rbind.fill
(软件包
plyr
)。 希望这有帮助
赫尔曼

d3@AmbikaGupta,您需要添加
all=T
,但我仍在努力理解您想要的输出。例如,
V1=1
V2=8
组合在哪里?请参阅all=TRUE、all.x和all.y。。。“这些行在通常由‘y’中的值填充的列中具有‘NA’。默认值为‘FALSE’,因此只有同时包含‘x’和‘y’中的数据的行才是。”类似于merge(d1、d2、by=…,all=TRUE)的功能应该可以工作。如果需要,您只需将NA替换为0即可。谢谢,但在尝试将NA替换为0时会显示警告。查看您的预期结果后,我认为我给您的代码不正确。关于警告消息,请检查是否通过
str(dat1)
检查列是否为
factors
为什么在预期结果中没有行
1 8 10 0
。输出为“data.frame”:7个obs。共有三个变量:$V1:Factor w/7级别“1”、“2”、“3”、“4”…:7123456$V2:Factor w/6级别“1”、“2”、“3”、“4”…:62345$V3:Factor w/7级别“10”、“20”、“30”…:7123456抱歉,忘了提及。最好将列更改为
numeric
class<代码>dat1[]
1  2  10  0
2  3  20  100
3  4  30  45
4  5  40  78
1  8  10   0
5  1  50   0
6  1  60   0
5  2  99   0
6  80 60   0
 res <- merge(dat1,dat2, by=c("V1", "V2"),all=TRUE)
 indx <- is.na(res[,3]) 
 res[indx,3] <- res[indx,4]
 res[indx,4] <- NA
 res[is.na(res)] <- 0
 #    V1 V2 V3.x V3.y
 #1  1  2   10    0
 #2  1  8   10    0
 #3  2  3   20  100
 #4  3  4   30   45
 #5  4  5   40   78
 #6  5  1   50    0
 #7  5  2   99    0
 #8  6  1   60    0
 #9  6 80   60    0
 dat1 <- structure(list(V1 = structure(1:6, .Label = c("1", "2", "3", 
 "4", "5", "6"), class = "factor"), V2 = structure(c(2L, 3L, 4L, 
 5L, 1L, 1L), .Label = c("1", "2", "3", "4", "5"), class = "factor"), 
V3 = structure(1:6, .Label = c("10", "20", "30", "40", "50", 
"60"), class = "factor")), .Names = c("V1", "V2", "V3"), class = "data.frame", row.names = c(NA, 
-6L))

 dat2 <- structure(list(V1 = structure(1:6, .Label = c("1", "2", "3", 
 "4", "5", "6"), class = "factor"), V2 = structure(c(5L, 2L, 3L, 
 4L, 1L, 6L), .Label = c("2", "3", "4", "5", "8", "80"), class = "factor"), 
V3 = structure(c(1L, 2L, 3L, 5L, 6L, 4L), .Label = c("10", 
"100", "45", "60", "78", "99"), class = "factor")), .Names = c("V1", 
"V2", "V3"), class = "data.frame", row.names = c(NA, -6L))
dat1[] <- lapply(dat1, function(x) as.numeric(as.character(x))) 
dat2[] <- lapply(dat2, function(x) as.numeric(as.character(x)))