tidyr :: gather找不到我的值变量

时间:2019-07-20 10:29:07

标签: r tidyr

我有一个非常大的data.frame。我复制了一部分。

     RECORDING_SESSION_LABEL condition TRIAL_INDEX IA_LABEL IA_DWELL_TIME
1                         23     match           1     eyes          3580
2                         23     match           1     nose          2410
3                         23     match           1    mouth          1442
4                         23     match           1     face           841
5                         23  mismatch           3     eyes          1817
6                         23  mismatch           3     nose          1724
7                         23  mismatch           3    mouth          1600
8                         23  mismatch           3     face          1136
9                         23  mismatch           4     eyes          4812
10                        23  mismatch           4     nose          3710
11                        23  mismatch           4    mouth          4684
12                        23  mismatch           4     face          1557
13                        23  mismatch           6     eyes          4645
14                        23  mismatch           6     nose          2321
15                        23  mismatch           6    mouth           674
16                        23  mismatch           6     face           684
17                        23     match           7     eyes          1062
18                        23     match           7     nose          1359
19                        23     match           7    mouth           215
20                        23     match           7     face             0

我需要计算每个试验索引中每个IA_LABEL的IA_DWELL_TIME的百分比。为此,我首先将IA_label放在不同的列中

data_IA_DWELL_TIME <- tidyr::spread(data_IA_DWELL_TIME, key = IA_LABEL, value = IA_DWELL_TIME) 

为了计算百分比,我创建了一个新的数据框:

data_IA_DWELL_TIME_percentage <-data_IA_DWELL_TIME
data_IA_DWELL_TIME_percentage$eyes <- 100*(data_IA_DWELL_TIME$eyes/(rowSums(data_IA_DWELL_TIME[,c("eyes","nose","mouth","face")])))
data_IA_DWELL_TIME_percentage$nose <- 100*(data_IA_DWELL_TIME$nose/(rowSums(data_IA_DWELL_TIME[,c("eyes","nose","mouth","face")])))
data_IA_DWELL_TIME_percentage$mouth <- 100*(data_IA_DWELL_TIME$mouth/(rowSums(data_IA_DWELL_TIME[,c("eyes","nose","mouth","face")])))
data_IA_DWELL_TIME_percentage$face <- 100*(data_IA_DWELL_TIME$face/(rowSums(data_IA_DWELL_TIME[,c("eyes","nose","mouth","face")])))

所以一切都很好,我得到了想要的输出。问题是当我想将列放回行中时

data_IA_DWELL_TIME_percentage <- tidyr::gather(key = IA_LABEL, value = IA_DWELL_TIME,-RECORDING_SESSION_LABEL,-condition, -TRIAL_INDEX)

我收到此错误:

  

tidyr :: gather中的错误(键= IA_LABEL,值= IA_DWELL_TIME,   -RECORDING_SESSION_LABEL ,:找不到对象“ RECORDING_SESSION_LABEL”   >

对这里发生的事情有任何想法吗?谢谢!

1 个答案:

答案 0 :(得分:1)

如前所述,您不是在gather语句中引用数据框。

但是,您可以避免完全引用它,而将第二部分放在dplyr管道中,如下所示:

library(dplyr)
library(tidyr)

data_IA_DWELL_TIME <- spread(data_IA_DWELL_TIME, key = IA_LABEL, value = IA_DWELL_TIME) 

data_IA_DWELL_TIME %>%
  mutate_at(
    vars(eyes, nose, mouth, face),
    ~ 100 * (. / (rowSums(data_IA_DWELL_TIME[, c("eyes", "nose", "mouth", "face")])))
  ) %>%
  gather(key = IA_LABEL, value = IA_DWELL_TIME,-RECORDING_SESSION_LABEL,-condition, -TRIAL_INDEX)