Reputation: 559
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))))
Upvotes: 4
Views: 224
Reputation: 146224
The problem with your code is that your innermost ifelse
doesn't have an "else" argument. If you just add , 0
inside the innermost ifelse
, your code will run. Thad said, some of the other answers are cleaner.
Upvotes: 1
Reputation: 44340
More tersely:
with(df, sign((Cn_LCIS-2)*(Cn_ILC-2))-(Cn_ILC != Cn_LCIS) < 0)*1
# [1] 0 1 0 1 0 0 1 0 0 1 1
Upvotes: 1
Reputation: 99391
And another possibility is to use with
or within
with(df, {
cb <- cbind(Cn_LCIS, Cn_ILC)
aa <- (cb > 2) | (cb < 2) | (cb[1] == cb[2])
(!(aa[,1] == aa[,2]))+0
})
# [1] 0 1 0 1 0 0 1 0 0 1 1
Upvotes: 1
Reputation: 92300
Or
transform(df, both = ifelse((Cn_LCIS > 2 & Cn_ILC > 2) |
(Cn_LCIS < 2 & Cn_ILC < 2) |
(Cn_LCIS == 2 & Cn_LCIS == 2), 0, 1))
# 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 1
# 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
Upvotes: 2
Reputation: 55420
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
Upvotes: 2