I am not sure how to make this work?
I wish to make a new column in a dataframe dependent on whether or not two other columns meet the following criteria:
if df$Cn_LCIS and df$Cn_ILC are both greater than '2' print '0'
if df$CN_LCIS and df$Cn_ILC are both smaller than '2' print '0'
and if they are the same also print '0' else print '1'
df
Chromosome Start End Cn_ILC mCn_ILC Cn_LCIS mCn_LCIS both
chr1.1 chr1 0 11194349 2 1 2 1 0
chr1.2 chr1 102809740 104579163 2 1 3 1 1
chr1.3 chr1 104579163 121311799 2 1 2 1 0
chr1.4 chr1 11194349 11492125 2 1 3 1 1
chr1.5 chr1 11492125 71442329 2 1 2 1 0
chr1.6 chr1 144009053 157140292 1 1 1 1 0
chr1.7 chr1 157140292 243709339 2 1 1 1 0
chr1.8 chr1 243709339 244112662 3 1 3 1 0
chr1.9 chr1 244112662 249250621 3 1 3 1 0
chr1.10 chr1 71442329 72624878 2 1 3 1 1
chr1.11 chr1 72624878 102809740 2 1 4 1 1
Not working:
df$both <- ifelse(df$Cn_LCIS > 2 & df$Cn_ILC > 2, 0, ifelse (df$Cn_LCIS < 2 & df$Cn_ILC < 2, 0, ifelse (df$Cn_LCIS == 2 & df$Cn_LCIS == 2, 0, ifelse(df$Cn_ILC!=df$Cn_LCIS,1))))
compareTo2 <- function(LCIS, ILC) {
as.numeric(!((LCIS > 2 & ILC > 2) | (LCIS < 2 & ILC < 2) | (LCIS == ILC) ))
}
compareTo2(df$Cn_LCIS, df$Cn_ILC)
# [1] 0 1 0 1 0 0 1 0 0 1 1
If your data is large, you might want to try the following
library(data.table)
DT <- as.data.table(df)
## different syntax for data.table than data.frame
DT[, as.numeric(!((Cn_LCIS > 2 & Cn_ILC > 2) | (Cn_LCIS < 2 & Cn_ILC < 2) | (Cn_LCIS == Cn_ILC) )) ]
## ... or you can assign it as a column
DT[, compare_LCIS_ILC := as.numeric(!((Cn_LCIS > 2 & Cn_ILC > 2) | (Cn_LCIS < 2 & Cn_ILC < 2) | (Cn_LCIS == Cn_ILC) )) ]
DT
# Chromosome Start End Cn_ILC mCn_ILC Cn_LCIS mCn_LCIS both compare_LCIS_ILC
# 1: chr1 0 11194349 2 1 2 1 0 0
# 2: chr1 102809740 104579163 2 1 3 1 1 1
# 3: chr1 104579163 121311799 2 1 2 1 0 0
# 4: chr1 11194349 11492125 2 1 3 1 1 1
# 5: chr1 11492125 71442329 2 1 2 1 0 0
# 6: chr1 144009053 157140292 1 1 1 1 0 0
# 7: chr1 157140292 243709339 2 1 1 1 0 1
# 8: chr1 243709339 244112662 3 1 3 1 0 0
# 9: chr1 244112662 249250621 3 1 3 1 0 0
# 10: chr1 71442329 72624878 2 1 3 1 1 1
# 11: chr1 72624878 102809740 2 1 4 1 1 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With