這是我在此處找到的原始帖子中的一個相關問題:How to create a new variable based on condition from different dataframe in R
我有 2 個來自實驗的資料框。第一個 df 在 40 分鐘內讀取(大致)連續信號。有 5 列,1:3 是二進制的 - 表示是否按下按鈕。第 4 列是第 2 列或第 3 列是否被推送的二進制。第 5 列是以秒為單位的近似時間。下面 df 的示例:
| 發起 | 剩下 | 對 | l 或 r | 時間 |
|---|---|---|---|---|
| 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 |
等等
第二個資料框是會話資訊,其中每一行都是一個試驗,通常根據日期不同為 100-150 行。我有一列標記試用開始時間,另一列標記試用結束時間(以秒為單位)。我有另一個專欄說明試驗是否有干預。下面的 df 示例(我省略了幾個不相關的列):
| 審判 | 控制 | 開始 | 結束 |
|---|---|---|---|
| 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 |
等等
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.
放棄以前的建議,我嘗試了嵌套 ifelse 陳述句,但沒有成功。我可以讓它將所有試驗標記為具有不同失敗嘗試的“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")
謝謝!
uj5u.com熱心網友回復:
您可以使用cut創建間隔并檢查值是否屬于它們:
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"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/457692.html
上一篇:使用ifelse替換和洗掉重復行
下一篇:while回圈和if陳述句的問題
