Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

r - merging two files into a new file

I have 2 files with say 3 columns and a few rows.

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

and

1    8    10
2    3    100
3    4    45
4    5    78
5    2    99
6    80   60

Now i want to create a third file having all the values of first two files and also if first and second column of both the files are same then in third file the values corresponding to them should like say,value in third column of first file must be in third column of newly created file and value in third column of second file must be in fourth column of newly created file. According to above example answer should be

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
 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

data

 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))

Convert the data columns to numeric class before you try the above code

dat1[] <- lapply(dat1, function(x) as.numeric(as.character(x))) 
dat2[] <- lapply(dat2, function(x) as.numeric(as.character(x))) 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...