Skip to content

Instantly share code, notes, and snippets.

@rhilfi
Last active January 25, 2022 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhilfi/2494b8af3b0e9d931944ca31001bbc1f to your computer and use it in GitHub Desktop.
Save rhilfi/2494b8af3b0e9d931944ca31001bbc1f to your computer and use it in GitHub Desktop.
mutate_across_str_replace #R
library(dplyr)
# see https://www.tidyverse.org/blog/2020/04/dplyr-1-0-0-colwise/
# see https://vbaliga.github.io/replace-text-in-specific-column/
ID<-1:3
Names<-c("Peter", "Paul", "Marhy")
FamilyNames<-c("Lennon", "McCartney", "Jagger")
Age<-rnorm(3, 80, 12)
data<-data.frame(Names, FamilyNames, ID, Age)
data<-data %>%
mutate(across(where(is.character), str_replace, "Marhy", "Marry"))
# Example c_across
library(dplyr) # library(tidyverse)
data<-rio::import("https://ndownloader.figshare.com/files/22063455", format="xlsx")
data<-data %>%
rowwise() %>%
mutate(DASS_Anxiety=sum(c_across(contains("DASSA")))) %>%
mutate(DASS_Depression=sum(c_across(contains("DASSD")))) %>%
mutate(DASS_Stress=sum(c_across(contains("DASSS"))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment