[英]R and dplyr: Creating a new column that divides values by multiple maximal values of another column
I am editing a dataframe using dplyr where I have information on multiple reaction times(rt) for different individuals(id).我正在使用 dplyr 编辑数据框,其中我有关于不同个人(id)的多个反应时间(rt)的信息。 I now want to make a new column, where I divide each specific reaction time by the individual's maximum reaction time.
我现在想创建一个新列,在那里我将每个特定的反应时间除以个人的最大反应时间。 Currently, I have only managed to divide each specific reaction time by the maximum reaction time of the group, using the following code:
目前,我只能使用以下代码将每个特定的反应时间除以组的最大反应时间:
df <- mutate(df, spcRT=rt)
df <- group_by(df, id, rt) %>% summarise(
spcRT = max(df$rt, na.rm=TRUE) ) %>% as.data.frame()
which(is.na(df))
df <- mutate(df,IDspcRT = rt/spcRT)
If we need to create a column ('spcRT') by dividing the reaction time ('rt') with the maximum reaction time ( max(rt, na.rm=TRUE)
) for each 'id', then we need to group by 'id' and do the division.如果我们需要通过将每个 'id' 的反应时间 ('rt') 除以最大反应时间 (
max(rt, na.rm=TRUE)
) 来创建列 ('spcRT'),那么我们需要分组通过'id'并进行除法。
df %>%
group_by(id) %>%
mutate(spcRT = rt/max(rt, na.rm=TRUE))
It is not clear why the OP used 'rt' along with 'id' as grouping variable in the post.目前尚不清楚为什么 OP 在帖子中使用“rt”和“id”作为分组变量。 It would give only a single unique 'rt' value and there is no need for any
max
.它只会给出一个唯一的 'rt' 值,不需要任何
max
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.