简体   繁体   English

R 和 dplyr:创建一个新列,将值除以另一列的多个最大值

[英]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.

相关问题 R dplyr pivot(排名表) - 具有来自多列的唯一行值的新列,然后从另一列分配行值 - R dplyr pivot (rankings table) - new column with unique row values from multiple columns, then assign row values from another column “R:dplyr:如何添加一个将值除以第一组值的列(类似于vlookup)” - “R: dplyr: How to add a column that divides a value by the first group of values (kind of like a vlookup)” 用 R dplyr 中另一列的值替换一列的值 - Replace the values of one column with values of another column in R dplyr 如何基于R中的另一列创建具有多个值的新列 - How to create a new column with multiple values based on another column in R 使用另一列值创建新列 - Creating a new column by using another column values 使用dplyr根据另一列中的值添加新列 - adding a new column based upon values in another column using dplyr 使用 dplyr 根据来自另一列的值的总和创建一个新列 - Create a new column based on the sum of values from another column, with dplyr 使用 R 中另一列的分组值创建一个新的数据框 - creating a new data frame with the counts by the grouped values of another column in R 基于多个列中的值在R数据框中创建新列 - creating new column in an R dataframe based on values in multiple columns 有没有办法在 R 中使用 dplyr 根据另一个列的值创建一个新列? - Is there a way to create a new column based on the values of another one using dplyr in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM