嵌套ifelse以输出3个响应R
这是我的原始帖子中的一个相关问题:如何根据r
我从实验中有2个数据帧。第一个DF在40分钟内读取A(大致)连续信号。有5列,1:3是二进制的 - 说是否按下按钮。第四列是按下第2列或第3列的二进制。第五列是秒数的大约时间。 的示例:
启动 | 左右 | 下面 | l或r | 时间 |
---|---|---|---|---|
0 | 0 0 | 1 | 1 2 | 2.8225 |
0 | 0 | 1 1 | 1 | 1.82375 |
0 | 0 0 | 1 | 1 1 | 2.82500 |
0 | 0 | 1 | 1 | 2.82625 1 2.82625 |
1 | 0 0 | 0 0 | 0 0 0 0 | 16.8200 |
1 | 0 0 0 0 | 0 0 0 | 0 | 16.8212 |
等
DF 信息的信息是每行是一个试验,通常取决于当天的100-150行。我有一列标记试验开始时间和另一列的列,该列以秒为单位的试验结束时间。我有另一列说明试验是否有干预。 Example from df below (I omitted several irrelevant columns):
trial | control | t start | t end |
---|---|---|---|
1 | 0 | 16.64709 | 35.49431 |
2 | 0 | 41.81843 | 57.74304 |
3 | 0 | 65.54510 | 71.16612 |
4 | 0 | 82.65743 | 87.30914 |
11 | 3 | 187.0787 | 193.5898 |
12 | 0 | 200.0486 | 203.1883 |
30 | 3 | 415.1710 | 418.0405 |
etc.
For the第一个数据框架,我想创建一个列,以指示在试用中是否按下按钮。如果确实在试验中按下了按钮,则我需要根据干预措施对其进行标签。这是基于第二个DF中的开始和结束时间以及控制信息。在此表中,0 =干预和3 =控制。
我希望它看起来像这样(iti = trial,wt_int =在试验中,wt_control = in trial& control):
启动 | 左右 | 右 | l或r | time time | trip trial_type |
---|---|---|---|---|---|
0 | 0 | 1 | 1 | 1.8225 | ITI |
0 0 | 0 0 0 | 1 1 | 1 | 2.82375 | ITI |
0 | 0 | 1 | 1 1 | 2.82500 | 0 |
16.82125 | 1 | 2.82625 | 1 | 0 | WT_INT |
1 | 0 | 0 | 0 | 0 | 0 0 0 0 0 0 16.82000 WT_INT |
1 | 0 | 0 | 0 | 0 | 0 |
ITI | 0 | 0 | 0 | 187.0800 | WT_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:
initiate | left | right | l or r | time |
---|---|---|---|---|
0 | 0 | 1 | 1 | 2.8225 |
0 | 0 | 1 | 1 | 2.82375 |
0 | 0 | 1 | 1 | 2.82500 |
0 | 0 | 1 | 1 | 2.82625 |
1 | 0 | 0 | 0 | 16.8200 |
1 | 0 | 0 | 0 | 16.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):
trial | control | t start | t end |
---|---|---|---|
1 | 0 | 16.64709 | 35.49431 |
2 | 0 | 41.81843 | 57.74304 |
3 | 0 | 65.54510 | 71.16612 |
4 | 0 | 82.65743 | 87.30914 |
11 | 3 | 187.0787 | 193.5898 |
12 | 0 | 200.0486 | 203.1883 |
30 | 3 | 415.1710 | 418.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):
initiate | left | right | l or r | time | trial_type |
---|---|---|---|---|---|
0 | 0 | 1 | 1 | 2.8225 | iti |
0 | 0 | 1 | 1 | 2.82375 | iti |
0 | 0 | 1 | 1 | 2.82500 | iti |
0 | 0 | 1 | 1 | 2.82625 | iti |
1 | 0 | 0 | 0 | 16.82000 | wt_int |
1 | 0 | 0 | 0 | 16.82125 | wt_int |
1 | 0 | 0 | 0 | 187.0800 | wt_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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
剪切
来创建间隔并检查一个值是否落入其中:这将返回
数据
You could use
cut
to create intervals and check, if a values falls into them:This returns
Data