Home > other >  Flip/switch values in two columns if value in a third column is positive or negative
Flip/switch values in two columns if value in a third column is positive or negative

Time:06-16

I would like to swap/switch the values in two columns if a value in a third column is negative.

id <- c("1", "2", "3", "4", "5")
effect <- c("A", "C", "G", "A", "G")
non_effect <- c("C", "A", "T", "C", "C")
variable1 <- c("X", "X", "X", "X", "X")
variable2 <- c("Y", "Y", "Y", "Y", "Y")
value <-c("0.2", "-0.1", "-0.3", "0.5", "0.8")

df <- data.frame(id,effect,non_effect,variable1,variable2,value) 

I would get this as a data.frame

id effect non_effect variable1 variable2 value
1  A      C          X         Y         0.2
2  C      A          X         Y        -0.1
3  G      T          X         Y        -0.3
4  A      C          X         Y         0.5
5  G      C          X         Y         0.8

what I want is:

id effect non_effect variable1 variable2 value
1  A      C          X         Y         0.2
2  A      C          X         Y         0.1
3  T      G          X         Y         0.3
4  A      C          X         Y         0.5
5  G      C          X         Y         0.8

CodePudding user response:

df %>%
  transform(effect = ifelse(value < 0, non_effect, effect),
            non_effect = ifelse(value < 0, effect, non_effect), 
            value = abs(as.numeric(value)))

# A tibble: 5 x 6
  id    effect non_effect variable1 variable2 value
  <chr> <chr>  <chr>      <chr>     <chr>     <dbl>
1 1     A      C          X         Y           0.2
2 2     A      C          X         Y           0.1
3 3     T      G          X         Y           0.3
4 4     A      C          X         Y           0.5
5 5     G      C          X         Y           0.8

CodePudding user response:

library(tidyverse)
id <- c("1", "2", "3", "4", "5")
effect <- c("A", "C", "G", "A", "G")
non_effect <- c("C", "A", "T", "C", "C")
variable1 <- c("X", "X", "X", "X", "X")
variable2 <- c("Y", "Y", "Y", "Y", "Y")
value <-c("0.2", "-0.1", "-0.3", "0.5", "0.8")

df <- data.frame(id,effect,non_effect,variable1,variable2,value) 

df %>% 
  mutate(
    value = as.numeric(value),
    effect1 = ifelse(value < 0, non_effect, effect),
    non_effect1 = ifelse(value < 0, effect, non_effect
    ),
    value = abs(value)
  ) %>% 
  select(id, effect = effect1, non_effect = non_effect1, variable1, variable2, value)

Output:

id effect non_effect variable1 variable2 value
1      A          C         X         Y   0.2
2      A          C         X         Y   0.1
3      T          G         X         Y   0.3
4      A          C         X         Y   0.5
5      G          C         X         Y   0.8

CodePudding user response:

Set to data.table, and change the columns in-place, when value<0

library(data.table)

setDT(df)[,value:=as.numeric(value)]
df[value<0, c("effect", "non_effect", "value"):=list(non_effect,effect,abs(value))]

Output:

   id effect non_effect variable1 variable2 value
1:  1      A          C         X         Y   0.2
2:  2      A          C         X         Y   0.1
3:  3      T          G         X         Y   0.3
4:  4      A          C         X         Y   0.5
5:  5      G          C         X         Y   0.8

Similar approach in base R

df$value = as.numeric(df$value)
df[df$value<0,c("effect","non_effect")] <- df[df$value<0, c("non_effect","effect")] 
df$value = abs(df$value)

CodePudding user response:

Just make a copy of one of the columns, switch the values you want in that column, then use the copy to switch the values in the other column.

df$value <- as.numeric(df$value)
is.neg <- df$value < 0
df$value <- abs(df$value)

eff <- df$effect
df$effect[is.neg] <- df$non_effect[is.neg]
df$non_effect[is.neg] <- eff[is.neg]

  • Related