Having 2 datasets. The cases dataset, I want to create a column (find) and set to 'Yes' if OTHRDX1 or OTHRDX2 or OTHRDX3 contains value that can be found in vector cd1121. For example:find in row2 and row5 are 'Yes', and rest are 'No'. How to do the columns searching? Thanks in advance!
apply() takes an object, in this case the data frame cases, a choice of rows (1) or columns (2) and then uses that as an argument to a function.
|> is the pipe like the OS pipe | or the R {magritter} package %>% operator. It passes on the output from the left to the next function on the right. It's just syntactic candy for
colSums(somefunction(someargument)
The left hand side (LHS) produces a truth table of TRUE FALSE values. These evaluate for 1,0 for purposes of colSums in the RHS, which ifelse() then treats as
TRUE/FALSE to make its classification.
Because most of us suffer to some degree from school algebra punctuation trauma the functional approach of {base} R can be daunting compared to the imperative approach of tidyverse—verb this then verb that. Once I got over the trauma, I found that the functional approach keeps me much more focused on the what I am trying to do, which avoids getting bogged down in figuring out how to do something, especially when I'm unclear on the concept of what it is that I am actually trying to accomplish.
Thanks! I always were amazed by the repeat operation (or loop vertically /horizontally) in r. Hardly for me to think how it goes through all columns and rows to searching or comparing. Also it is hard for me to find explicitly which 'item' in 'array' contains the value that I were looking for.
Yes, R's vectorization can be disturbing if you come from another programming language, but once you get used to it a bit, it makes things faster and easier to reason with.
Note that you could always write things in a more C-like way (not that it's a good idea), for example my solution