开发者

Combining multiple data.frames using R

I have several txt files in which each txt file contains 3 columns(A,B,C). Column A will be common to all txt files. Now I want to combine txt files with coulmn A appearing only once while the other columns (B and C) of respective files. I used cbind but it creates a data frame with repeats of column A, which I dont want. The column A must be repeated only once. Here is 开发者_StackOverflow中文版the R code I tried:

data <- read.delim(file.choose(),header=T)   
data2 <- read.delim(file.choose(),header=T)
data3 <- cbind(data1,data2)
write.table(data3,file="sample.txt",sep="\t",col.names=NA)


Unless your files are all sorted precisely the same, you'll need to use merge:

dat <- merge(data,data2,by="A")
dat <- merge(dat,data3,by="A")

This should automatically prevent you from having multiple A's, since merge knows they're all a key/index column. You'll likely want to rename the duplicate B's and C's before merging.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜