Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

What I want is to refer to another value in the same tibble and mutate the initial value into that. Let's say for instance I am having a tibble as follows:

tibble(x = 200:203, y = c("a","b","c", "d"), z = c(NA, 202,201,NA))

And from that I want to get the following as the result:

 tibble(x = 200:203, y = c("a","b","c", "d"), z = c(NA, "c","b",NA))

I have tried the following but it doesn't work.

mutate(tbl,z, ifelse(!is.na(z), tbl[[which(fixed_messages$id == z),2]], NA)) 

1 Answer

0 votes
by (107k points)

In R programming, you can simply use the mutate function:

tbl %>%

 mutate(z = y[match(z, x)])

      x y     z    

  <int> <chr> <chr>

1   200 a     <NA> 

2   201 b     c    

3   202 c     b    

4   203 d     <NA> 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...