Sorry for the basic question, but I cannot figure out how to change a string of characters and numbers. I have a dataframe datafile with a column subject with 28 different numbers/subject (4, 5, 8, 9, 10, 11, 12, etc.). I need to change these numbers into strings like 'sbj04', 'sbj05', 'sbj08', 'sbj09','sbj10', 'sbj11','sbj12', etc. I've tried different things, but they don't work.
datafile$subject = as.factor(datafile$subject) #it works
datafile$subject <- sub("^", "sbj", datafile$subject ) #it works, but all numbers become 'sbj4', 'sbj5', 'sbj8', 'sbj9', sbj10', etc.
the following code doesn't return what I need
datafile[datafile$subject == "sbj4"] <- "sbj04"
datafile[datafile$subject == "sbj5"] <- "sbj05"
datafile[datafile$subject == "sbj8"] <- "sbj08"
datafile[datafile$subject == "sbj9"] <- "sbj09"
the following code doesn't return what I need
datafile[datafile$subject == "sbj4",] <- datafile[datafile$subject == "sbj04",]
datafile[datafile$subject == "sbj5",] <- datafile[datafile$subject == "sbj05",]
datafile[datafile$subject == "sbj8",] <- datafile[datafile$subject == "sbj08",]
datafile[datafile$subject == "sbj9",] <- datafile[datafile$subject == "sbj09",]
the following code doesn't return what I need
if (datafile$subject < 10) {
datafile$subject <- sub("^", "sbj0", datafile$subject )
} else{
datafile$subject <- sub("^", "sbj", datafile$subject )
}