user18260355
user18260355

Reputation: 21

How to add certain values from one column to another?

I have a column with values "Yes" and "No". I want to place all of the "No" values into another column. How can I do that?

Example:

Column1 Column2
No Apples
No Oranges
No Bananas
Yes Apples
Yes Peaches

I want:

Column2
No
No
Apples
Oranges
Bananas
Apples
Peaches

Just the "No"s and none of the "Yes" to be places into column2. No idea why my table looks weird when I publish the question/

Upvotes: 1

Views: 70

Answers (1)

TarJae
TarJae

Reputation: 78917

Update:

library(dplyr)

df %>% 
  mutate(Column2 = ifelse(Column1 == "No", "No", Column2))

We could use mutate with an ifelse statement:

library(dplyr)

data.frame(col1 = rep(c("yes", "no"),5)) %>% 
  mutate(col2 = ifelse(col1=="no", "no", NA_character_))
   col1 col2
1   yes <NA>
2    no   no
3   yes <NA>
4    no   no
5   yes <NA>
6    no   no
7   yes <NA>
8    no   no
9   yes <NA>
10   no   no

Upvotes: 4

Related Questions