Philip John
Philip John

Reputation: 5565

R issue with adding a new column while working on Confusion Matrix

I am working on Confusion matrix in R.

I have the following data

row.names    A    B    C    Actual
d1           1    1    1    1
d2           0    0    1    0
d3           1    1    0    0
d4           0    0    0    0
d5           0    1    1    1
d6           1    0    1    1

I need to create a new column Classifier which has value 1 if rowsums(data[,1:3]) greater than or equal to 2, and 0 if the rowsum is less than 2.

Can anyone provide me a hint how to do this.

Upvotes: 1

Views: 100

Answers (1)

akrun
akrun

Reputation: 887691

You can try

data$Classifier <- (rowSums(data[,1:3]) >=2)+0L
data
#   A B C Actual Classifier
#d1 1 1 1      1          1
#d2 0 0 1      0          0
#d3 1 1 0      0          1
#d4 0 0 0      0          0
#d5 0 1 1      1          1
#d6 1 0 1      1          1

data

data <- structure(list(A = c(1L, 0L, 1L, 0L, 0L, 1L), B = c(1L, 0L, 
1L, 
0L, 1L, 0L), C = c(1L, 1L, 0L, 0L, 1L, 1L), Actual = c(1L, 0L, 
0L, 0L, 1L, 1L)), .Names = c("A", "B", "C", "Actual"), 
row.names = c("d1", "d2", "d3", "d4", "d5", "d6"),
class = "data.frame")

Upvotes: 1

Related Questions