vendredi 17 septembre 2021

Compare elements in two strings for conditional selection in R

I have the following dataset with three variables:

X<-c(0.1,0.3,0.3,0.4,0.8,0.8,1.1,1.2,1.3,1.6,2.1,2.2,2.3,2.4,2.6,2.8,3.1,3.3,3.4,4.1,4.4,4.4,4.5,5.0,5.1,5.2,5.3,5.4,5.4,5.7,6.2,6.5,6.6,6.7,6.7,7.0,7.4,7.5,7.8,7.8,8.6,9.5,9.8,11.1,11.9)

Y<-c("ac","bcd","ac","ab","ab","d","ab","ab","cd","bcd","d","ad","ad","d","ad","ad","ad","ab","ad","a", "ad","ac","a", "bcd", "ac","d", "ac","ac","bcd","ab", "ab","ab","cd","ac","ad","ab","d","d", "ab","d", "d", "bcd","a", "a","d")

Z<-c("ac","bcd", "ab","ac","ab","cd","ac","ac","bcd" ,"cd","bcd" ,"ac","ac","bcd","ab","bcd", "bcd", "a", "ab","ab","cd","a", "ac","ac","bcd" ,"ad","bcd", "bcd" ,"ab","bcd",
"bcd", "bcd", "ac","cd","a", "cd","ac","ac","cd","ab","ab","a", "bcd", "cd","a")

df<-data.frame(X,Y,Z)
  1. The first step is to determine whether the same letter is present in Y and Z.
  2. The second step could be done using an ifelse to select the minimum X value which contains the letter in both Y and Z and the maximum X value which does not contain the character in either Y or Z column.

The final result should be:

6.7, 7.0

Aucun commentaire:

Enregistrer un commentaire