R How to use apply with ifelse function to search for string values over many columns?

user63230

I know there are many similar questions to this but just can't figure this out.

I want an ifelse function to go over many columns in a dataframe. I want to add two variables to the dataframe, "C03_only" and "only_c02_and_c09". I am only focused on entries that contain values: "C02 ","C03", "C09".

Example data:

mydf<- data.frame(id=1:4,
                  x1=c("A02", "C02", "C03", "M01"),
                  x2=c("B02", "", "C02", "C09"),
                  x3=c("C03", "C03", "C09", "C02") )

R>mydf
  id  x1  x2  x3
1  1 A02 B02 C03
2  2 C02     C03
3  3 C03 C02 C09
4  4 M01 C09 C02

The new dataset should look like:

R>mydf
  id  x1  x2  x3 C03_only only_c02_and_c09
1  1 A02 B02 C03        1                0
2  2 C02     C03        0                0
3  3 C03 C02 C09        0                0
4  4 M01 C09 C02        0                1

I first tried something like this

mydf$C03_only <- with(mydf,ifelse(x1 != "C02" | "C09" & x2 !="C02" | "C09" & x3== "C03",1,0))

which didnt work but the idea is terrible as I have many columns so is a no runner. Similarly I tired something with a for loop:

mydf$C03_only<-rep(0,nrow(mydf))
for (i in 2:nrow(mydf)){
  if (mydf$x1[i]!="C02" && mydf$x2[i]!="C09" && mydf$x3[i]=="C03"){
    mydf$C03_only[i]<-1}
}

This also didnt work but (only partially finished) with enough playing with it, it probably would.

I think the best approach is to use apply function but can't get it working:

mydf$C03_only<- apply(mydf[,-1], MARGIN=1, FUN=function(x){ 
  ifelse(any(x == "C03") & any(x != "C09" & x != "C02") , 1, 0)
}
)

mydf$only_c02_and_c09<- apply(mydf[,-1], MARGIN=1, FUN=function(x){ 
  ifelse(any(x == "C02" & x == "C09") & any(x != "C03") , 1, 0)
}
)

These are close but no cigar. I need to replace any with something but not sure what. Perhaps pass the variables of interest to a vector and run some conditional statement using %in% on this but I'm not sure how.

Any suggestions would be great, thanks.

candles_and_oranges

We can apply the conditions by row with. Note: the plus sign connected to paranthetical brackets coerces the from logical to numeric. Example: +(x) is the same as as.numeric(x):

mydf$C03_only <- apply(mydf, 1, function(x) +(any(x=="C03") & all(x != "C02" & x != "C09")))
mydf$only_c02_and_c09 <- apply(mydf, 1, function(x) +(!any(x=="C03") & sum(x == "C02" | x == "C09") >= 2L))
mydf
#   id  x1  x2  x3 C03_only only_c02_and_c09
# 1  1 A02 B02 C03        1                0
# 2  2 C02     C03        0                0
# 3  3 C03 C02 C09        0                0
# 4  4 M01 C09 C02        0                1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I loop or apply an ifelse statement with 2 conditions over many columns?

From Dev

How to loop a function over columns in many dataframes in r

From Dev

Loop over groupby columns in r and apply a function

From Dev

Apply a function to a subset of many columns in R

From Dev

Performing automated function over many columns in R

From Dev

How to use R ifelse function with && function?

From Dev

How to use multiple columns as inputs for apply function in R

From Dev

How to apply operation and sum over columns in R?

From Dev

How to apply custom function to multiple columns in R and store many dataframes using loops

From Dev

R: Apply a function over only character columns without type coercion

From Dev

R: apply function over multiple columns by condition (like with aggregate)

From Dev

How to use apply function to normalize columns pandas

From Dev

R function for looping over a string with unique values

From Dev

How to apply specific columns to sapply function in R?

From Dev

How to apply function to several columns in R?

From Dev

Apply a mutate over columns in R

From Dev

R apply function over consecutive values of two vectors

From Dev

How do I creathe then apply function over certain columns of a tibble?

From Dev

Python: How to iterate over rows and apply function to create new columns

From Dev

How to apply a function across multiple groups and loop over multiple columns

From Python

How to apply a function on a series of columns, based on the values in a corresponding series of columns?

From Dev

how to use function & ifelse() to sum up the total of negative values?

From Dev

How do I apply SUM() aggregation function to many columns at once?

From Dev

r apply multiple conditions to multiple columns (vectors of function argument values)

From Dev

R How can I use the apply function to a time series object and keep the dates attached to the specific columns?

From Dev

R: How to use Apply function taking multiple inputs across rows and columns

From Dev

How To Create a Use Apply (or create a function) To Turn Character Dates Into Dates In R Across Multiple Columns of Dates

From Dev

Run function over columns with numeric values only in data table in r

From Dev

How to apply a function to multiple columns to create multiple new columns in R?

Related Related

  1. 1

    How do I loop or apply an ifelse statement with 2 conditions over many columns?

  2. 2

    How to loop a function over columns in many dataframes in r

  3. 3

    Loop over groupby columns in r and apply a function

  4. 4

    Apply a function to a subset of many columns in R

  5. 5

    Performing automated function over many columns in R

  6. 6

    How to use R ifelse function with && function?

  7. 7

    How to use multiple columns as inputs for apply function in R

  8. 8

    How to apply operation and sum over columns in R?

  9. 9

    How to apply custom function to multiple columns in R and store many dataframes using loops

  10. 10

    R: Apply a function over only character columns without type coercion

  11. 11

    R: apply function over multiple columns by condition (like with aggregate)

  12. 12

    How to use apply function to normalize columns pandas

  13. 13

    R function for looping over a string with unique values

  14. 14

    How to apply specific columns to sapply function in R?

  15. 15

    How to apply function to several columns in R?

  16. 16

    Apply a mutate over columns in R

  17. 17

    R apply function over consecutive values of two vectors

  18. 18

    How do I creathe then apply function over certain columns of a tibble?

  19. 19

    Python: How to iterate over rows and apply function to create new columns

  20. 20

    How to apply a function across multiple groups and loop over multiple columns

  21. 21

    How to apply a function on a series of columns, based on the values in a corresponding series of columns?

  22. 22

    how to use function & ifelse() to sum up the total of negative values?

  23. 23

    How do I apply SUM() aggregation function to many columns at once?

  24. 24

    r apply multiple conditions to multiple columns (vectors of function argument values)

  25. 25

    R How can I use the apply function to a time series object and keep the dates attached to the specific columns?

  26. 26

    R: How to use Apply function taking multiple inputs across rows and columns

  27. 27

    How To Create a Use Apply (or create a function) To Turn Character Dates Into Dates In R Across Multiple Columns of Dates

  28. 28

    Run function over columns with numeric values only in data table in r

  29. 29

    How to apply a function to multiple columns to create multiple new columns in R?

HotTag

Archive