Reputation: 467
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:
dput
formatlong_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
Reputation: 887108
We could use data.table
library(data.table)
setDT(df)[Hotspot == 'Yes', Threshold := 'High']
Upvotes: 1
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
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
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
Reputation: 9858
We can use replace()
long_fused %>% mutate(Threshold = replace(Threshold, Hotspot==“yes”, “High”))
Upvotes: 2