KLM117
KLM117

Reputation: 467

Replacing values in a column based on a condition in another column

I have a data frame where the "Threshold" column is based on the values in the first 4 columns. However, if there is a "Yes" In the Hotspot Column, I want to replace the Threshold value as being "High". So if the dataframe currently looks like this:

CNV.Gain Amplification Homozygous.Deletion.Frequency Heterozygous.Deletion.Frequency Hotspot Threshold
3 5 10 0 Yes Low
0 0 11 8 No Medium
7 16 25 0 No High

I would like it to look like this:

CNV.Gain Amplification Homozygous.Deletion.Frequency Heterozygous.Deletion.Frequency Hotspot Threshold
3 5 10 0 Yes High
0 0 11 8 No Medium
7 16 25 0 No High

Thank you as always for any help!

Here is the code to replicate the data frame if needed:

Data in dput format

long_fused <-
structure(list(CNV.Gain = c(3L, 0L, 7L), Amplification = c(5L, 
0L, 16L), Homozygous.Deletion.Frequency = c(10L, 11L, 25L), 
Heterozygous.Deletion.Frequency = c(0L, 8L, 0L), Hotspot = c("Yes","No","No",)Threshold = c("Low", "Medium", "High")), class ="data.frame", row.names = c(NA, -3L))

Upvotes: 2

Views: 15054

Answers (5)

akrun
akrun

Reputation: 887108

We could use data.table

library(data.table)
setDT(df)[Hotspot == 'Yes', Threshold := 'High']

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 388982

You can do a direct replacement -

df$Threshold[df$Hotspot == 'Yes'] <- 'High'
df

#  CNV.Gain Amplification Homozygous.Deletion.Frequency
#1        3             5                            10
#2        0             0                            11
#3        7            16                            25

#  Heterozygous.Deletion.Frequency Hotspot Threshold
#1                               0     Yes      High
#2                               8      No    Medium
#3                               0      No      High

Upvotes: 8

Serkan
Serkan

Reputation: 1945

You can use mutate and case_when to replace the values accordingly.

long_fused %>% mutate(
        Threshold = case_when(
                Hotspot == "Yes" ~ "High",
                Hotspot != "Yes" ~ Threshold
        )
)

Edit: A brief discussion with @Connor about conventions, and general approaches to such questions leads me to this edit and fixing a typo!

long_fused %>% mutate(
        Threshold = case_when(
                Hotspot == "Yes" ~ "High",
                TRUE ~ Threshold
        )
)

It is convention, and good practise, to use TRUE ~ Threshold.

Upvotes: 5

Connor Krenzer
Connor Krenzer

Reputation: 489

If you want to make sure that all thresholds are set to "High" when there is a hotspot, try:

long_fused <- mutate(long_fused, Threshold = if_else(Hotspot == "Yes", "High", Threshold))

Upvotes: 3

GuedesBF
GuedesBF

Reputation: 9858

We can use replace()

long_fused %>% mutate(Threshold = replace(Threshold, Hotspot==“yes”, “High”))

Upvotes: 2

Related Questions