Здравствуйте, у меня есть фрейм данных, например:
Groups Names COL1 COL2 COL3
G1 SP1 1 0.400 0.500
G1 SP1 1 0.004 0.005
G1 SP1 0 0.004 0.005
G1 SP2 0 0.400 0.005
G1 SP2 0 0.004 0.500
G1 SP3 0 0.005 0.006
G1 SP5 1 0.400 0.006
G1 SP6 1 0.008 0.002
G2 Sp12 1 0.004 0.005
G2 SP1 0 0.050 0.600
G2 SP1 0 0.400 0.600
G2 SP2 0 0.004 0.005
G2 SP2 0 0.004 0.005
G2 SP5 0 0.004 0.005
G2 SP6 0 0.003 0.002
G2 SP7 0 0.560 0.760
G2 SP12 0 0.004 0.003
G3 SP5 0 0.87 0.767
, и я хотел бы добавить столбец, в котором я заполняю буквы (L,M
или P
) для каждого Groups
и каждого Names
(так что внутри групп имена идентификаторов должны иметь одну и ту же букву) и вот идея (и мы должны соблюдать эту спецификацию c порядок:
if
COL1 >= 1 & COL2 >= 0.05 & COL3 >= 0.05 -> COL4 = L
else
COL1 == 0 & COL2 >= 0.05 & COL3 >= 0.05 -> COL4 = M
else
COL1 >= 1 & COL2 >= 0.05 & COL3 < 0.05 -> COL4 = M
else
COL1 >= 1 & COL2 < 0.05 & COL3 < 0.05 -> COL4 = M
else
COL1 == 0 & COL2 < 0.05 & COL3 < 0.05 -> COL4 = P
else
COL1 == 0 & COL2 >= 0.05 & COL3 < 0.05 -> COL4 = P
else
COL1 == 0 & COL2 < 0.05 & COL3 >= 0.05 -> COL4 = P
else
COL1 == 0 & COL2 < 0.05 & COL3 >= 0.05 -> COL4 = P
else
COL4 = P
Вот пример того, что я должен получить:
Groups Name COL1 COL2 COL3 COL4
G1 SP1 1 0.400 0.500 L this one had COL1 >= 1 & COL2 >= 0.05 & COL3 >= 0.05 -> COL4 = L
G1 SP1 1 0.004 0.005 L
G1 SP1 0 0.004 0.005 L
G1 SP2 0 0.400 0.005 P this one had COL1 == 0 & COL2 >= 0.05 & COL3 < 0.05 -> COL4 = P
G1 SP2 0 0.004 0.500 P
G1 SP3 0 0.005 0.006 P this one had COL1 == 0 & COL2 < 0.05 & COL3 < 0.05 -> COL4 = P
G1 SP5 1 0.400 0.006 M this one had COL1 >= 1 & COL2 >= 0.05 & COL3 < 0.05 -> COL4 = M
G1 SP6 1 0.008 0.002 M this one had COL1 >= 1 & COL2 < 0.05 & COL3 < 0.05 -> COL4 = M
G2 Sp12 1 0.004 0.005 M this one had COL1 >= 1 & COL2 < 0.05 & COL3 < 0.05 -> COL4 = M
G2 SP1 0 0.050 0.600 M
G2 SP1 0 0.400 0.600 M this one had COL1 == 0 & COL2 >= 0.05 & COL3 >= 0.05 -> COL4 = M
G2 SP2 0 0.004 0.005 P this one had COL1 == 0 & COL2 < 0.05 & COL3 < 0.05 -> COL4 = P
G2 SP2 0 0.004 0.005 P
G2 SP5 0 0.004 0.005 P this one had COL1 == 0 & COL2 < 0.05 & COL3 < 0.05 -> COL4 = P
G2 SP6 0 0.003 0.002 P this one had COL1 == 0 & COL2 < 0.05 & COL3 < 0.05 -> COL4 = P
G2 SP7 0 0.560 0.760 M this one had COL1 == 0 & COL2 >= 0.05 & COL3 >= 0.05 -> COL4 = M
G2 SP12 0 0.004 0.003 M
G3 SP5 0 0.87 0.767 M this one had COL1 == 0 & COL2 >= 0.05 & COL3 >= 0.05 -> COL4 = M
вот данные
> dput(test)
structure(list(Groups = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L), .Label = c("G1",
"G2", "G3"), class = "factor"), Names = structure(c(1L, 1L, 1L,
4L, 4L, 5L, 6L, 7L, 2L, 1L, 1L, 4L, 4L, 6L, 7L, 8L, 3L, 6L), .Label = c("SP1",
"Sp12", "SP12", "SP2", "SP3", "SP5", "SP6", "SP7"), class = "factor"),
COL1 = c(1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L), COL2 = c(0.4, 0.004, 0.004, 0.4,
0.004, 0.005, 0.4, 0.008, 0.004, 0.05, 0.4, 0.004, 0.004,
0.004, 0.003, 0.56, 0.004, 0.87), COL3 = c(0.5, 0.005, 0.005,
0.005, 0.5, 0.006, 0.006, 0.002, 0.005, 0.6, 0.6, 0.005,
0.005, 0.005, 0.002, 0.76, 0.003, 0.767)), class = "data.frame", row.names = c(NA,
-18L))