Daniele
Daniele

Reputation: 31

Replace a value when is >0 in a column with a character of another column in R

In a data frame, I would like to replace the values greater than 1 in a column with the character present in another column.

Description dataframe and output

Upvotes: 0

Views: 52

Answers (1)

Daniele
Daniele

Reputation: 31

I found the solution:

library(dplyr)

new_df <-df %>% mutate(Sample_1 = ifelse(as.numeric(Sample_1==0), m.Sample_1,as.character(knr)))%>%
  mutate(Sample_2 = ifelse(as.numeric(Sample_2==0), Sample_2,as.character(knr)))%>%
  mutate(Sample_3 = ifelse(as.numeric(Sample_3==0), Sample_3,as.character(knr)))
  mutate(m.Surface = ifelse(as.numeric(m.Surface==0), m.Surface,as.character(knr))

Upvotes: 1

Related Questions