Merging multiple text files in R with a constraint

前端 未结 3 1468
小鲜肉
小鲜肉 2020-12-22 08:57

I have 10 text files containing 1000\'s of rows.

Example: First File:

V1  V2
1   2
2   3
10 20
1   4    
.....

Second file:

<
3条回答
  •  清歌不尽
    2020-12-22 09:34

    Here's one way to do it. I'll assume that you've already read your files into a list of data.frames, e.g. with L <- lapply(filenames, read.table). I'll simulate this L below.

    # Create dummy data
    L <- replicate(5, expand.grid(1:10, 1:10)[sample(100, 10), ], simplify=FALSE)
    
    # Add a column to each data.frame in L. 
    # This will indicate presence of the pair when we merge.
    L <- lapply(seq_along(L), function(i) { 
      L[[i]][, paste0('DF', i)] <-  1
      L[[i]] 
    })
    
    # Merge all the things 
    # (hat-tip to @Charles - https://stackoverflow.com/a/8097519/489704)
    L.merged <- Reduce(function(...) merge(..., all=T), L)
    
    head(L.merged)
    
    #   Var1 Var2 DF1 DF2 DF3 DF4 DF5
    # 1    1    2  NA  NA  NA   1  NA
    # 2    1    5   1  NA  NA  NA   1
    # 3    1    9  NA  NA  NA   1  NA
    # 4    1   10  NA  NA   1   1  NA
    # 5    2    5  NA  NA   1  NA  NA
    # 6    2    6  NA   1  NA   1  NA
    

    Easy enough to convert the NA to 0 if you want, e.g. with:

    L.merged[is.na(L.merged)] <- 0
    

    Relevant post: Merge multiple data frames in a list simultaneously

提交回复
热议问题