How can I count the number of times a string appears in column X, based on column Y ?

How would you extend the snippet below to create additional sets for bone, central, skin, soft?

nb <- c(42,90,53,1,65,68)

Given those objects how would you compare a pair of testset and trueset for

  • either
  • neither
  • both
  • some number 1..n

Hint: All are booleans, the last is the sum of booleans.

Flow: mutate with case_when

DF %>% mutate(the_result = case_when{
              # integer vectors of name_case
              name_case == #logical test
                                      ~ #VALUE for in either
              ...
              name_case == #logical test
                                      ~ #VALUE for sum(...)

See intersect in help() for set operations

1 Like

I wonder if there's some way to automate this for each row.....none-the-less, you've given me a different way of thinking about it. I'm still trying to find the solution - you are helping greatly! .

Regards, teatv

Thanks again for your patience and guidance with this. I'm learning a lot from your example! I've spent some time looking over your suggestion, and learning about case_when. I saw this tidyverse.org article and followed some of those examples about how to replace characters matching a given criterion.

If my workflow should be to define each non-[lineage] group first, I can see how that would work in theory, but I'm not sure how to make it work in practice using case_when.

Something like: for all unique rows in DF$lineage, define the non-[lineage] groups which would be all the groups present in DF$clusters_testset except non-[lineage].
If I did that, then (According to above example), there should be non-[lineage] group for every [lineage] group. Non-blood, non-bone, non-central, non-skin, non-soft.

Then I could maybe write a function including case_when, calling the respective non-[lineage] group when appropriate, and mutate that to the DF column as a/b/c/d_result?

I'm still trying to learn about all this.
Thanks!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.