Last active
January 25, 2022 11:10
-
-
Save rhilfi/2494b8af3b0e9d931944ca31001bbc1f to your computer and use it in GitHub Desktop.
mutate_across_str_replace #R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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