Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a dataframe that looks like this:

A     B
ABC1  0
DEF2  4
DEG0  4

How do I transform this so that if a row contains the letter "D" then insert the value "yes" in a new column and "no" if not.

Final dataframe should look like this:

A     B    C
ABC1  0    no
DEF2  4    yes
DEG0  4    yes
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
968 views
Welcome To Ask or Share your Answers For Others

1 Answer

We can use grepl to return a logical index by matching the 'D' in the 'A' column, and then with ifelse, change the logical vector to 'yes' and 'no'

df$C <- ifelse(grepl("D", df$A), "yes", "no")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...