claire_123
claire_123

Reputation: 85

Paste suffix to certain columns

I am attempting to paste the suffix .m onto the names of certain columns.

Attached is a screenshot of my data at the moment. Here is the code I have tried:

dat %>% rename_at(vars(self.report, teacher.report, parent.report, peer.report, observation, standardized.assessments, administrative.records, qed, primary, secondary,mixed, Targeted, academic, social_relationships, emo_and_problem), ~ paste0(., '.m'))

Here is my data:

structure(list(Drop = c("0", "0", "0", "0", "0", "0"), `First Reviewer` = c("LK", 
"LK", "LK", "LK", "LK", "LK"), `Second Reviewer` = c("SB", "SB", 
"SB", "SB", "SB", "SB"), APA = c("August et al. (2001)", "August et al. (2001)", 
"August et al. (2001)", "August et al. (2001)", "August et al. (2001)", 
"August et al. (2001)"), Intervention = c("Early Risers", "Early Risers", 
"Early Risers", "Early Risers", "Early Risers", "Early Risers"
), Study.Number = c("1", "1", "1", "1", "1", "1"), `Quasi-Experimental` = c("0", 
"0", "0", "0", "0", "0"), Targeted = c(1, 1, 1, 1, 1, 1), Tx.Cluster = c(10, 
10, 10, 10, 10, 10), Control.Cluster = c(10, 10, 10, 10, 10, 
10), `Unit of Cluster` = c("schools", "schools", "schools", "schools", 
"schools", "schools"), Tx.N = c(102, 102, 102, 102, 102, 102), 
    Control.N = c(99, 99, 99, 99, 99, 99), Total_N = c(201, 201, 
    201, 201, 201, 201), `Included in RAND` = c("No", "No", "No", 
    "No", "No", "No"), `Outcome (category): 
Cognitive
Emotional
Social/interpersonal
Academic` = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_), `Website Category` = c("Academic", "Problem Behaviors", 
    "Problem Behaviors", "Problem Behaviors", "Social Relationships", 
    "Social Relationships"), `Website Subcategory` = c("Academic Engagement", 
    "Aggression/misconduct", "Disruptive Behavior", "Disruptive Behavior", 
    "Social Skills", "Social Skills"), Composite = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_), Order = c(NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_), `Outcome (measure)` = c("Woodcock Johnson", 
    "CFA composite of multiple scales", "CFA composite of multiple scales", 
    "CFA composite of multiple scales", "CFA composite of multiple scales", 
    "CFA composite of multiple scales"), `MOOSES Rating` = c("5", 
    "4", "4", "4", "4", "4"), MOOSES_rough = c("5", "4", "4", 
    "4", "4", "4"), `Description of measure` = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_), `Reverse-coded?
(N: pos/higher is good)
(Y: neg/lower is good)` = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_), `Type of Measure` = c("standardized assessment", 
    "Parent-report", "Parent-report", "Parent-report", "Parent-report", 
    "Parent-report"), `Self- Report` = c("No", "No", "No", "No", 
    "No", "No"), Notes = c(NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_), Grade.Band = c("Primary",
    "Primary", "Primary", "Primary", "Primary", "Primary"), Grade = c(NA, 
    "K-1", "K-1", "K-1", NA, "K-1"), `Outcome (description)` = c("Academic competence", 
    "Aggression", "Hyperactivity", "Impulsivity", "Adaptability", 
    "Social skills"), Outcome_desc = c("Academic competence", 
    "Aggression", "Hyperactivity", "Impulsivity", "Adaptability", 
    "Social skills"), `For R: Prettest Treatment Mean` = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_), `For R: Pretest Control Mean` = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_), `For R: Pretest Control SD` = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_), baseline = c(NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_
    ), `For R: Posttest Treatment Mean` = c(NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_
    ), `For R: Posttest Control Mean` = c(NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_
    ), `For R: Posttest Control SD` = c(NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_
    ), ES2 = c("0.26", "-0.02", "0.07", "0.07", "-0.28", "0.03"
    ), ES = c(0.26, -0.02, 0.07, 0.07, -0.28, 0.03), `LEAVE EMPTY Glass's D` = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_), `Outcome (descriptoin)` = c("Academic competence", 
    "Aggression", "Hyperactivity", "Impulsivity", "Adaptability", 
    "Social skills"), Significant = c("No", "No", "No", "No", 
    "No", "No"), primary = c(1, 1, 1, 1, 1, 1), secondary = c(0, 
    0, 0, 0, 0, 0), mixed = c(0, 0, 0, 0, 0, 0), teacher.report = c(0, 
    0, 0, 0, 0, 0), peer.report = c(0, 0, 0, 0, 0, 0), parent.report = c(0, 
    1, 1, 1, 1, 1), self.report = c(0, 0, 0, 0, 0, 0), observation = c(0, 
    0, 0, 0, 0, 0), standardized.assessments = c(1, 0, 0, 0, 
    0, 0), administrative.records = c(0, 0, 0, 0, 0, 0), form_of_assessment = structure(c(6L, 
    3L, 3L, 3L, 3L, 3L), .Label = c("Administrative Records", 
    "Observation", "Parent-Report", "Peer Report", "Self-Report", 
    "Standardized Records", "Teacher-Report"), class = "factor"), 
    emotional_wellbeing = c(0, 0, 0, 0, 0, 0), problem_behaviors = c(0, 
    1, 1, 1, 0, 0), emo_and_problem = c(0, 1, 1, 1, 0, 0), social_relationships = c(0, 
    0, 0, 0, 1, 1), academic = c(1, 0, 0, 0, 0, 0), outcome = c("Academic", 
    "Emotional Well-Being and Problem Behaviors", "Emotional Well-Being and Problem Behaviors", 
    "Emotional Well-Being and Problem Behaviors", "Social Relationships", 
    "Social Relationships"), qed = c(0, 0, 0, 0, 0, 0)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame")) 

Upvotes: 0

Views: 32

Answers (1)

akrun
akrun

Reputation: 887158

Use rename_with as rename_at is deprecated and also assign the output back

library(dplyr)
library(stringr)
dat <- dat %>% 
    rename_with(~ str_c(., ".m"),
     c(self.report, teacher.report, parent.report, peer.report, 
     observation, standardized.assessments, administrative.records, 
     qed, primary, secondary,mixed, Targeted, academic, 
       social_relationships, emo_and_problem))

Now check the column names that ends with .m

> grep("\\.m$", names(dat), value = TRUE)
 [1] "Targeted.m"                 "primary.m"                  "secondary.m"                "mixed.m"                    "teacher.report.m"          
 [6] "peer.report.m"              "parent.report.m"            "self.report.m"              "observation.m"              "standardized.assessments.m"
[11] "administrative.records.m"   "emo_and_problem.m"          "social_relationships.m"     "academic.m"                 "qed.m"     

Upvotes: 2

Related Questions