R:使用现有值创建新列,以另一列的组为条件

发布于 2025-01-18 04:40:02 字数 571 浏览 1 评论 0原文

我正在研究一个眼动追踪数据集,希望将现有列中的值分配给新列,以第二个现有列为条件。

我有列:带有因子 leftright 的“targetlocation”、带有数值的“right_looks”和带有数值的“left_looks”。

我想要做的是:

  • 为“target_looks”和“distractor_looks”创建新列,其中:
  • 如果“targetlocation”为right,则“right_looks”中的值将分配给“target_looks”,并且“ left_looks”被分配给“distractor_looks”
  • 如果“targetlocation”是left,那么“left_looks”中的值将被分配给“target_looks”,并且来自“target_looks”的值将被分配给“target_looks” “right_looks”被分配给“distractor_looks”

我尝试创建第一个空列,然后填充它们,但也许 mutate() 或 if_else() 语句在这里效果更好。有什么解决办法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独自←快乐 2025-01-25 04:40:02

这是示例数据的另一种方法。为了清楚起见,我创建了一个带有“左”和“右”的简单2个元素向量。您的TargetLocation可以是这些级别的因素,而nontargetLocation将是该因素的反向级别。您可以使用GET根据“左”或“右”提取适当的列名的值。

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"))
  )

输出

  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

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
自此以后,行同陌路 2025-01-25 04:40:02

这是一种选择。

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')]

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')]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文