Home > Back-end >  How to create permutations conditionally from an existing data frame in R
How to create permutations conditionally from an existing data frame in R

Time:05-08

I would like to create all possible 5-value permutations from a character vector. After permutating, I would like to create a new column that determines if each row is "compatible" or not. I have an existing data frame that determines if a combination of values is compatible or not. I would like to take the information from the existing data frame and extend it into the permutated data.

To explain further, here is an example my existing data frame that has information about if values are compatible or not:

Ind1. Ind2. Ind3. Ind4. Ind5. Compatible
  A     B     C     D     E      NO
  A     B     C     D     F      NO
  A     B     C     D     G      NO
  A     B     C     E     F      YES
  A     B     D     F     G      YES
  A     D     E     F     H      YES

And here is the permutated data frame:

 library(RcppAlgos)
 
 df <- as.data.frame(permuteGeneral(c(A, B, C, D, E, F, G, H), m = 5)

And I would like to create a new column in the "df" data frame that takes from the existing data frame to determine if any permutations are compatible or not.

I am fairly new to R and have no idea of going about this or if this is even possible. Any help is appreciated!!

CodePudding user response:

Your new data, df, is a fairly big object after making the necessary assumption that you want the items to be character values rather than R symbols.

The merge function can be used to see which of the 6K permutations are matchable to the items in the first dataset.

bigM <- merge(read.table(text="Ind1. Ind2. Ind3. Ind4. Ind5. Compatible
  A     B     C     D     E      NO
  A     B     C     D     F      NO
  A     B     C     D     G      NO
  A     B     C     E     F      YES
  A     B     D     F     G      YES
  A     D     E     F     H      YES", head=TRUE), df, by.x=1:5, by.y=1:5)

Turns out there are exactly 6 lines that match on all five values (in the proper order) and the result looks just like the original. If you wanted bigM to have all the lines of the df object you could add an all.y=TRUE parameter to the merge call.

str(bigM)
'data.frame':   6 obs. of  6 variables:
 $ Ind1.     : chr  "A" "A" "A" "A" ...
 $ Ind2.     : chr  "B" "B" "B" "B" ...
 $ Ind3.     : chr  "C" "C" "C" "C" ...
 $ Ind4.     : chr  "D" "D" "D" "E" ...
 $ Ind5.     : chr  "E" "F" "G" "F" ...
 $ Compatible: chr  "NO" "NO" "NO" "YES" ...

CodePudding user response:

Your new data, df, is a fairly big object after making the necessary assumption that you want the items to be character values rather than R symbols.

> tail(df)
     V1 V2 V3 V4 V5
6715  H  G  F  D  C
6716  H  G  F  D  E
6717  H  G  F  E  A
6718  H  G  F  E  B
6719  H  G  F  E  C
6720  H  G  F  E  D

The merge function can be used to see which of the 6K permutations are matchable to the items in the first dataset.

bigM <- merge(read.table(text="Ind1. Ind2. Ind3. Ind4. Ind5. Compatible
  A     B     C     D     E      NO
  A     B     C     D     F      NO
  A     B     C     D     G      NO
  A     B     C     E     F      YES
  A     B     D     F     G      YES
  A     D     E     F     H      YES", head=TRUE), df, by.x=1:5, by.y=1:5)

Turns out there are exactly 6 lines that match on all five values and the result looks just like the original. If you wanted bigM to have all the lines of the df object you could all an all.y=TRUE parameter to the merge call.

I'm guessing that you had not actually run this code and were under the impression that permuteGeneral was giving you a random set of permutations rather than all possible unique permutations. That guess is based on the error I got as well as the fact that the answer should have been immediately apparent if you knew what df became. The error came from not quoting the letters "A"-"H". That meant the R interpreter went out looking for an object named A and didn't find anything. R names or 'symbols' are not quoted on input while R character values need to be.

  • Related