Wong Wong - 2 months ago 14
R Question

How to apply ifelse statement for list of data in R

I want to create a new list of data comparing whether the predict will be equal actual in every row of the data in order to check for my correctly predicted outcome. sorry if my code is very messy and inefficient. I need some guideline for the problem stated below. thank you.

First i create a list for predict and actual

predict <-list(compare_result[,1])`

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
1 1 0 1 0 0 1 1 1 0 0 1 1 1 0 0 1 0 1 0 1 1 1 0 1 0 0

actual <-list(compare_result[,2])

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
0 1 1 1 1 1 0 1 0 1 1 0 1 1 0 1 1 0 0 0 1 0 1 1 0 0 0


Then i try to use the
ifelse
statement for both of the list but an error came out:

> comapare2 <- ifelse(predict=actual,"true","false")

Error in ifelse(predict = actual, "true", "false") :
unused argument (predict = actual)

Answer

You can compare two vectors directly, e.g.:

matches <- compare_results[, 1] == compare_results[,2]

and then if you need that to be a list (not sure why), just cast the final result:

matches_list <- as.list(matches)