У меня есть следующий набор данных с тремя переменными:

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. Первый шаг - определить, присутствует ли одна и та же буква в Y и Z.
  2. Второй шаг может быть выполнен с помощью ifelse для выбора минимального значения X, которое содержит букву как в Y, так и в Z, и максимального значения X, которое не содержит символа в столбце Y или Z.

Конечный результат должен быть:

6.7, 7.0
0
Garn_R 17 Сен 2021 в 23:48

2 ответа

Лучший ответ

Используя код из этого предыдущего ответа, я думаю, что это должно сработать:

library(stringr)

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)

df$YZ <- gsub(" ", "", paste(df$Y, df$Z))

df$unique <- !sapply(df$YZ, function(x) any(str_count(x, letters)>1))

print(max(df[df$unique == FALSE,]$X))
print(min(df[df$unique == TRUE,]$X))
[1] 6.7
[1] 7
0
Machetes0602 17 Сен 2021 в 21:07

Я думаю, что «минимальное значение X, которое содержит буквы как в Y, так и в Z» должно быть 0,1.

Head (df) X Y Z YZ уникальный 1 0,1 ac acac FALSE 2 0,3 bcd bcd bcdbcd FALSE 3 0,3 ac ab acab FALSE 4 0,4 ​​ab ac abac FALSE 5 0,8 ab ab abab FALSE 6 0,8 d cd dcd FALSE

Y = 'ac', Z = 'ac', 'a' как в Y, так и в Z

0
Liao Chen 18 Сен 2021 в 03:30