Normalizing selection of dataframe columns with dplyr

后端 未结 2 1546
说谎
说谎 2021-01-21 22:31

I have a data.frame with variables var1 var2 (both strings) and variables x, y, and z. I would like

相关标签:
2条回答
  • 2021-01-21 23:09

    It could be a problem with the attributes or grouping variable. We can reset the dataset without external attributes by converting to data.frame and then do the mutate_at

    df_ %>% 
       as.data.frame %>%
       mutate_at(vars(x, y, z), funs(norm = ./.[1]))
    
    0 讨论(0)
  • 2021-01-21 23:10

    Building on @akrun's answer, you can also use the first() function from dplyr:

    df_ %>%
            mutate_at(vars(c("x", "y", "z")), funs(norm = ./first(.)))
    
    0 讨论(0)
提交回复
热议问题