Skip to content

check and verify

1 message · arun

#
HI,

If you wish to keep it as factors (A-D), then try this:

test<-read.table(text="
?A B C D E F
?a b c d 40 30
?a f a b 20 10
?x m y m 50 30
",sep="",header=TRUE)

str(test)
#'data.frame':??? 3 obs. of? 6 variables:
# $ A: Factor w/ 2 levels "a","x": 1 1 2
# $ B: Factor w/ 3 levels "b","f","m": 1 2 3
# $ C: Factor w/ 3 levels "a","c","y": 2 1 3
# $ D: Factor w/ 3 levels "b","d","m": 2 1 3
# $ E: int? 40 20 50
# $ F: int? 30 10 30
?test$new_column_1<-(as.character(test$A)==as.character(test$C))*test$E
?test$new_column_2<-(as.character(test$B)==as.character(test$D))*test$F
test
#? A B C D? E? F new_column_1 new_column_2
#1 a b c d 40 30??????????? 0??????????? 0
#2 a f a b 20 10?????????? 20??????????? 0
#3 x m y m 50 30??????????? 0?????????? 30


#or
within(test,{new_column_2<-(as.character(B)==as.character(D))*F;new_column_1<-(as.character(A)==as.character(C))*E})
? #A B C D? E? F new_column_1 new_column_2
#1 a b c d 40 30??????????? 0??????????? 0
#2 a f a b 20 10?????????? 20??????????? 0
#3 x m y m 50 30??????????? 0?????????? 30
A.K.