0

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:

V1  V2
1   2
8   10
.....

What I want is a final file which will contain 12 columns. The first two columns representing the relationship and the next 10 columns telling about different files ( represented by 1 if that pair is present and 0 if not present ) For example:

Final File:

V1  V2  V3  V4  V5  V6  V7  V8  V9  V10  V11  V12
1   2   1   1   0   0   1    0   1   1    0     1
2   3   1   0   1   1   0    0   0   1    1     0

Now, after searching, I found,

# 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 - http://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

However, the problem is that, this is not showing those rows which have all NA's. For eg:

# 7    4    5  NA  NA  NA  NA  NA
  • You probably need to add an `init` to the `Reduce` which has all possible combinations pre-populated. – asb Jun 19 '14 at 07:27
  • And will it be like this: L.merged <- Reduce(function(...) init() merge(..., all=T), L) – user3730341 Jun 19 '14 at 07:31
  • Please look at `?Reduce` to see how init works. I would have tried to work it out but without the files your example is a little difficult to follow. – asb Jun 19 '14 at 07:41
  • init basically creates an R object of the same kind as the elements of input file. The code that I have added for the reduce function, first creates 5 dummy files. Can you tell me in the given code, what changes should be added to the reduce function? – user3730341 Jun 19 '14 at 07:45
  • @user3730341 Is this your question? http://stackoverflow.com/questions/24280263/merging-multiple-text-files-in-r-with-a-constraint – zx8754 Jun 19 '14 at 08:50
  • The answer that has been given there. I have already included that. However, it is incomplete. And I have included in my post, what more I want. – user3730341 Jun 19 '14 at 08:54
  • @user3730341 This question is still 99.99% same as above mentioned question. I updated my answer see, if it works for you: [Merging multiple text files in R with a constraint](http://stackoverflow.com/a/24280659/680068) – zx8754 Jun 19 '14 at 10:11

0 Answers0