I am doing an analysis and I am not so advanced in R. I am stuck in this stage. I would appreciate your cordial help in this regard.
In the example data set (data1), I have 7 columns (a, b, c, d, e, f, g). First 3 columns (a, b, c) are from one group and the other 4 columns (d, e, f, g) are from different group.
In my data1, I applied a formula for TRUE/FALSE (1/0) output for each and every combination pair from the 2 groups [a with every column of d, e, f, g (ad, ae, af, ag); b with every column (bd, be, bf, bg); c with every column (cd, ce, cf, cg)]. In my example, I tried to do it for c and d column (cd). But, it is not showing me the correct output. In my desired output, the second output should be 1 (TRUE). And also, I have no idea how to automate the loop for the whole data set.
**N.B. Since any value of parental line can satisfy the conditions, I used | in the loop to get the result. But, I am not sure if it is the correct or efficient way.
a <- c(0, 0.501, 0.501, 0, 0.57, 20)
b <- c(0.108, 0.96, 0.110, 0.10, 4, 2)
c <- c(0.110, 1, 0.118, 0.107, 0.34, 0.019)
d <- c(0.115, 0.113, 0.98, 0.1, 13, 2)
e <- c(1, 3.113, 0.98, 0.560, 0.15, 1)
f <- c(2.45, 4.16, 0.045, 0.9, 0.12, 70)
g <- c(2, 0.6, 3, 7, 0.12, 29)
data1 <- data.frame(a, b, c, d, e, f, g)
rownames(data1) <- c(("Man2"), paste0('Man', 4:8))
data1
#> a b c d e f g
#> Man2 0.000 0.108 0.110 0.115 1.000 2.450 2.00
#> Man4 0.501 0.960 1.000 0.113 3.113 4.160 0.60
#> Man5 0.501 0.110 0.118 0.980 0.980 0.045 3.00
#> Man6 0.000 0.100 0.107 0.100 0.560 0.900 7.00
#> Man7 0.570 4.000 0.340 13.000 0.150 0.120 0.12
#> Man8 20.000 2.000 0.019 2.000 1.000 70.000 29.00
r <- c(1:6) #number of rows
c <- c(1:7) #number of cols
f <- c(1:3) #first group (a, b, c)
s <- c(4:7) #second group (d, e, f, g)
for (i in r) {
if ((data1[i,3] >= 0.5 & data1[i,4] >= data1[i, 3]*2) | (data1[i,4] >= 0.5 & data1[i,3] >= data1[i,4]*2)) {
print(1L)
} else if ((data1[i,3] < 0.5 & data1[i,4] >= 1.0) | (data1[i,4] < 0.5 & data1[1,3] >= 1.0)) {
print(1L)
} else {
print(0L)
}
}
#> [1] 0
#> [1] 0
#> [1] 0
#> [1] 0
#> [1] 1
#> [1] 1
Created on 2021-06-21 by the reprex package (v2.0.0)
For each combination,
- if the value in any one column is >= 0.5, the value in the other column should be >= 2 folds,
- and if the value is below 0.5 in any one column, the value in the other column need to be >= 1 for each row.
I look for an output like this df:
ad <- c(0L, 0L, 0L, 0L, 1L, 1L)
ae <- c(1L, 1L, 0L, 0L, 0L, 1L)
af <- c(1L, 1L, 0L, 0L, 0L, 1L)
ag <- c(1L, 0L, 1L, 1L, 0L, 0L)
bd <- c(0L, 0L, 0L, 0L, 1L, 0L)
be <- c(1L, 1L, 0L, 0L, 1L, 1L)
bf <- c(1L, 1L, 0L, 0L, 1L, 1L)
bg <- c(1L, 0L, 1L, 1L, 1L, 1L)
cd <- c(0L, 1L, 0L, 0L, 1L, 1L)
ce <- c(1L, 1L, 0L, 0L, 0L, 1L)
cf <- c(1L, 1L, 0L, 0L, 0L, 1L)
cg <- c(1L, 1L, 1L, 1L, 0L, 1L)
df <- data.frame(ad, ae, af, ag, bd, be, bf, bg, cd, ce, cf, cg)
rownames(df) <- c(("Man2"), paste0('Man', 4:8))
df
#> ad ae af ag bd be bf bg cd ce cf cg
#> Man2 0 1 1 1 0 1 1 1 0 1 1 1
#> Man4 0 1 1 0 0 1 1 0 1 1 1 1
#> Man5 0 0 0 1 0 0 0 1 0 0 0 1
#> Man6 0 0 0 1 0 0 0 1 0 0 0 1
#> Man7 1 0 0 0 1 1 1 1 1 0 0 0
#> Man8 1 1 1 0 0 1 1 1 1 1 1 1
Created on 2021-06-21 by the reprex package (v2.0.0)
I have two groups in the combination pairs as well. I want to count number of "1s" in one group c(ad, ae, bg, be, bf, cd) and also in the other group c(af, ag, bd, ce, cf, cg) for each row. Is it a good idea to save the 1/0 output in a new data frame like df or just putting the count of "1s" in two new columns (like in df2)? The working data set is a huge one. So, memory and efficient way are the issues here.
My desired output is something like this [counting 1s for the first combination group c(ad, ae, bg, be, bf, cd) and also counting 1s for the second combination group c(af, ag, bd, ce, cf, cg) for each row]:
ad <- c(0L, 0L, 0L, 0L, 1L, 1L)
ae <- c(1L, 1L, 0L, 0L, 0L, 1L)
af <- c(1L, 1L, 0L, 0L, 0L, 1L)
ag <- c(1L, 0L, 1L, 1L, 0L, 0L)
bd <- c(0L, 0L, 0L, 0L, 1L, 0L)
be <- c(1L, 1L, 0L, 0L, 1L, 1L)
bf <- c(1L, 1L, 0L, 0L, 1L, 1L)
bg <- c(1L, 0L, 1L, 1L, 1L, 1L)
cd <- c(0L, 1L, 0L, 0L, 1L, 1L)
ce <- c(1L, 1L, 0L, 0L, 0L, 1L)
cf <- c(1L, 1L, 0L, 0L, 0L, 1L)
cg <- c(1L, 0L, 1L, 1L, 0L, 1L)
#first_group <- c(ad, ae, bg, be, bf, cd)
#second_group <- c(af, ag, bd, ce, cf, cg)
first_combi <- c(4, 4, 1, 1, 5, 5)
second_combi <- c(5, 3, 2, 2, 1, 4)
df2 <- data.frame(ad, ae, af, ag, bd, be, bf, bg, cd, ce, cf, cg, first_combi, second_combi)
rownames(df2) <- c(("Man2"), paste0('Man', 4:8))
df2
#> ad ae af ag bd be bf bg cd ce cf cg first_combi second_combi
#> Man2 0 1 1 1 0 1 1 1 0 1 1 1 4 5
#> Man4 0 1 1 0 0 1 1 0 1 1 1 0 4 3
#> Man5 0 0 0 1 0 0 0 1 0 0 0 1 1 2
#> Man6 0 0 0 1 0 0 0 1 0 0 0 1 1 2
#> Man7 1 0 0 0 1 1 1 1 1 0 0 0 5 1
#> Man8 1 1 1 0 0 1 1 1 1 1 1 1 5 4
Created on 2021-06-21 by the reprex package (v2.0.0)
So, I need two suggestions:
- How to automate the loop for the whole data set
- How to store the 1/0 (TRUE/FALSE) for the two combination groups and counting "1s" for the groups.
Please, pave me on these two problems with the data set.