嵌套ifelse以输出3个响应R

发布于 2025-01-19 16:10:06 字数 3609 浏览 2 评论 0原文

这是我的原始帖子中的一个相关问题:如何根据r

我从实验中有2个数据帧。第一个DF在40分钟内读取A(大致)连续信号。有5列,1:3是二进制的 - 说是否按下按钮。第四列是按下第2列或第3列的二进制。第五列是秒数的大约时间。 的示例:

启动左右下面l或r时间
00 011 22.8225
001 111.82375
00 011 12.82500
00112.82625 1 2.82625
10 00 00 0 0 016.8200
10 0 0 00 0 0016.8212

DF 信息的信息是每行是一个试验,通常取决于当天的100-150行。我有一列标记试验开始时间和另一列的列,该列以秒为单位的试验结束时间。我有另一列说明试验是否有干预。 Example from df below (I omitted several irrelevant columns):

trialcontrolt startt end
1016.6470935.49431
2041.8184357.74304
3065.5451071.16612
4082.6574387.30914
113187.0787193.5898
120200.0486203.1883
303415.1710418.0405

etc.

For the第一个数据框架,我想创建一个列,以指示在试用中是否按下按钮。如果确实在试验中按下了按钮,则我需要根据干预措施对其进行标签。这是基于第二个DF中的开始和结束时间以及控制信息。在此表中,0 =干预和3 =控制。

我希望它看起来像这样(iti = trial,wt_int =在试验中,wt_control = in trial& control):

启动左右l或rtime timetrip trial_type
00111.8225ITI
0 00 0 01 112.82375ITI
0011 12.825000
16.8212512.8262510WT_INT
100000 0 0 0 0 0 16.82000 WT_INT
100000
ITI000187.0800WT_CONTROL

ITI 0 0 1 1 。我可以将其标记为所有试验,为“ ITI”或“ WT_INT”,以不同的失败尝试或第1037行的错误(当它从ITI更改为WT时)。从我最初的问题中,我现在在我的第一df中有一个“试用”列,我将用于以下代码。也许有一种更简单的方法结合了原始代码?

错误的一部分:

df %>% 
  rowwise() %>% 
  mutate(trial_type = ifelse(any(trial == "wt" & df2$control == 0,
                                 ifelse(trial == "wt" & df2$control == 3,
                                        "wt_omission", "iti"), "wt_odor")))

还尝试了此功能,它标记为WT_INT:

df$trial_type <- ifelse(df$trial == 'wt' && df2$control == 0,
                        ifelse(df$trial == 'wt' && df2$control == 3, 
                               "wt_control", "iti"), "wt_int")

谢谢!

This is a related question from my original post found here: How to create a new variable based on condition from different dataframe in R

I have 2 data frames from an experiment. The 1st df reads a (roughly) continuous signal over 40 mins. There are 5 columns, 1:3 are binary - saying whether a button was pushed. The 4th column is a binary of if either from column 2 or 3 was pushed. The 5th column is an approximate time in seconds. Example from df below:

initiateleftrightl or rtime
00112.8225
00112.82375
00112.82500
00112.82625
100016.8200
100016.8212

etc.

The 2nd data frame is session info where each row is a trial, usually 100-150 rows depending on the day. I have a column that marks trial start time and another column that marks trial end time in seconds. I have another column that states whether or not the trial had an intervention. Example from df below (I omitted several irrelevant columns):

trialcontrolt startt end
1016.6470935.49431
2041.8184357.74304
3065.5451071.16612
4082.6574387.30914
113187.0787193.5898
120200.0486203.1883
303415.1710418.0405

etc.

For the 1st data frame, I want to create a column that indicates whether or not the button was pushed within a trial. If the button was indeed pushed within a trial, I need to label it based on intervention. This is based on those start and end times in the 2nd df, along with the control info. In this table, 0 = intervention and 3 = control.

I would like it to look something like this (iti = inter-trial, wt_int = within trial & intervention, wt_control = within trial & control):

initiateleftrightl or rtimetrial_type
00112.8225iti
00112.82375iti
00112.82500iti
00112.82625iti
100016.82000wt_int
100016.82125wt_int
1000187.0800wt_control

etc.

Going off previous recommendations, I've tried nested ifelse statements with no success. I can get it to label all of the trials as either "iti" or "wt_int" with different failed attempts, or an error at row 1037 (when it changes from iti to wt). From my original question I have a "trial" column now in my 1st df which I'm using for the following code. Perhaps there is a more straightforward approach that combines the original code?

Errors out part way through:

df %>% 
  rowwise() %>% 
  mutate(trial_type = ifelse(any(trial == "wt" & df2$control == 0,
                                 ifelse(trial == "wt" & df2$control == 3,
                                        "wt_omission", "iti"), "wt_odor")))

Also tried this, which labels all as wt_int:

df$trial_type <- ifelse(df$trial == 'wt' && df2$control == 0,
                        ifelse(df$trial == 'wt' && df2$control == 3, 
                               "wt_control", "iti"), "wt_int")

Thank you!

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

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

发布评论

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

评论(1

就是爱搞怪 2025-01-26 16:10:06

您可以使用剪切来创建间隔并检查一个值是否落入其中:

library(dplyr)

df1 %>% 
  mutate(
    check_1 = cut(time, breaks = df2$t_start, labels = FALSE),
    check_2 = coalesce(cut(time, breaks = df2$t_end, labels = FALSE), 0),
    check_3 = df2$control[check_1],
    trial_type = case_when(
      check_1 - check_2 == 1 & check_3 == 0 ~ "wt_int",
      check_1 - check_2 == 1 & check_3 == 3 ~ "wt_control",
      TRUE ~ "iti"
      )
    ) %>% 
  select(-starts_with("check_"))

这将返回

# A tibble: 7 x 6
  initiate  left right l_or_r   time trial_type
     <dbl> <dbl> <dbl>  <dbl>  <dbl> <chr>     
1        0     0     1      1   2.82 iti       
2        0     0     1      1   2.82 iti       
3        0     0     1      1   2.82 iti       
4        0     0     1      1   2.83 iti       
5        1     0     0      0  16.8  wt_int    
6        1     0     0      0  16.8  wt_int    
7        1     0     0      0 187.   wt_control

数据

df1 <- structure(list(initiate = c(0, 0, 0, 0, 1, 1, 1), left = c(0, 
0, 0, 0, 0, 0, 0), right = c(1, 1, 1, 1, 0, 0, 0), l_or_r = c(1, 
1, 1, 1, 0, 0, 0), time = c(2.8225, 2.82375, 2.825, 2.82625, 
16.82, 16.8212, 187.08)), class = c("spec_tbl_df", "tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -7L), spec = structure(list(
    cols = list(initiate = structure(list(), class = c("collector_double", 
    "collector")), left = structure(list(), class = c("collector_double", 
    "collector")), right = structure(list(), class = c("collector_double", 
    "collector")), l_or_r = structure(list(), class = c("collector_double", 
    "collector")), time = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1L), class = "col_spec"))

df2 <- structure(list(trial = c(1, 2, 3, 4, 11, 12, 30), control = c(0, 
0, 0, 0, 3, 0, 3), t_start = c(16.64709, 41.81843, 65.5451, 82.65743, 
187.0787, 200.0486, 415.171), t_end = c(35.49431, 57.74304, 71.16612, 
87.30914, 193.5898, 203.1883, 418.0405)), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -7L), spec = structure(list(
    cols = list(trial = structure(list(), class = c("collector_double", 
    "collector")), control = structure(list(), class = c("collector_double", 
    "collector")), t_start = structure(list(), class = c("collector_double", 
    "collector")), t_end = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1L), class = "col_spec"))

You could use cut to create intervals and check, if a values falls into them:

library(dplyr)

df1 %>% 
  mutate(
    check_1 = cut(time, breaks = df2$t_start, labels = FALSE),
    check_2 = coalesce(cut(time, breaks = df2$t_end, labels = FALSE), 0),
    check_3 = df2$control[check_1],
    trial_type = case_when(
      check_1 - check_2 == 1 & check_3 == 0 ~ "wt_int",
      check_1 - check_2 == 1 & check_3 == 3 ~ "wt_control",
      TRUE ~ "iti"
      )
    ) %>% 
  select(-starts_with("check_"))

This returns

# A tibble: 7 x 6
  initiate  left right l_or_r   time trial_type
     <dbl> <dbl> <dbl>  <dbl>  <dbl> <chr>     
1        0     0     1      1   2.82 iti       
2        0     0     1      1   2.82 iti       
3        0     0     1      1   2.82 iti       
4        0     0     1      1   2.83 iti       
5        1     0     0      0  16.8  wt_int    
6        1     0     0      0  16.8  wt_int    
7        1     0     0      0 187.   wt_control

Data

df1 <- structure(list(initiate = c(0, 0, 0, 0, 1, 1, 1), left = c(0, 
0, 0, 0, 0, 0, 0), right = c(1, 1, 1, 1, 0, 0, 0), l_or_r = c(1, 
1, 1, 1, 0, 0, 0), time = c(2.8225, 2.82375, 2.825, 2.82625, 
16.82, 16.8212, 187.08)), class = c("spec_tbl_df", "tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -7L), spec = structure(list(
    cols = list(initiate = structure(list(), class = c("collector_double", 
    "collector")), left = structure(list(), class = c("collector_double", 
    "collector")), right = structure(list(), class = c("collector_double", 
    "collector")), l_or_r = structure(list(), class = c("collector_double", 
    "collector")), time = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1L), class = "col_spec"))

df2 <- structure(list(trial = c(1, 2, 3, 4, 11, 12, 30), control = c(0, 
0, 0, 0, 3, 0, 3), t_start = c(16.64709, 41.81843, 65.5451, 82.65743, 
187.0787, 200.0486, 415.171), t_end = c(35.49431, 57.74304, 71.16612, 
87.30914, 193.5898, 203.1883, 418.0405)), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -7L), spec = structure(list(
    cols = list(trial = structure(list(), class = c("collector_double", 
    "collector")), control = structure(list(), class = c("collector_double", 
    "collector")), t_start = structure(list(), class = c("collector_double", 
    "collector")), t_end = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1L), class = "col_spec"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文