'R: Creating a new column with existing values, conditional on group of another column

I am working on an eye-tracking dataset want to assign values from existing columns to new columns, conditional on a second existing column.

I have columns: "targetlocation" with factors left and right, "right_looks" with numerical values, and "left_looks" with numerical values.

What I want to do is:

  • Create new columns for "target_looks" and "distractor_looks" where:
  • If “targetlocation” is right, then values from “right_looks” are assigned to “target_looks” and values from “left_looks” are assigned to “distractor_looks”
  • If “targetlocation” is left, then values from “left_looks” are assigned to “target_looks” and values from “right_looks” are assigned to “distractor_looks”

I have tried creating first empty columns and then populating them, but maybe a mutate() or if_else() statement works better here. Are there any solutions for this?



Solution 1:[1]

Here is another approach with example data. For clarity, I created a simple 2 element vector with "left" and "right". Your targetlocation can be a factor with those levels, and a nontargetlocation would be the reversed levels of that factor. You can use get to extract the value for the appropriate column name based on "left" or "right".

set.seed(123)

df <- data.frame(
  targetlocation = c("left", "left", "right", "left"),
  left_looks = sample(4),
  right_looks = sample(4)
)

library(tidyverse)

my_levels <- c("left", "right")

df %>%
  rowwise() %>%
  mutate(
    targetlocation = factor(targetlocation, levels = my_levels),
    nontargetlocation = rev(my_levels)[targetlocation],
    target_looks = get(paste0(targetlocation, "_looks")),
    distractor_looks = get(paste0(nontargetlocation, "_looks"))
  )

Output

  targetlocation left_looks right_looks nontargetlocation target_looks distractor_looks
  <fct>               <int>       <int> <chr>                    <int>            <int>
1 left                    3           3 right                        3                3
2 left                    4           2 right                        4                2
3 right                   1           4 left                         4                1
4 left                    2           1 right                        2                1

Solution 2:[2]

Here is one option.

df$target_looks <- df$distractor_looks <- 0
df[df$targetlocation == 'right', c('target_looks', 'distractor_looks')] <- df[df$targetlocation == 'right', c('rightlooks', 'leftlooks')]
df[df$targetlocation == 'left', c('target_looks', 'distractor_looks')] <- df[df$targetlocation == 'left', c('leftlooks', 'rightlooks')]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ben
Solution 2 sashahafner