1

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 )
    }
David Arenburg
  • 89,637
  • 17
  • 130
  • 188
dede
  • 1,079
  • 4
  • 14
  • 32

1 Answers1

0

An easy way to do this if you actually have numeric values is to use sprintf (in base R).

Example:

> x <- 1:15
> sprintf("sbj%02.f", x)
 [1] "sbj01" "sbj02" "sbj03" "sbj04" "sbj05" "sbj06" "sbj07" "sbj08" "sbj09"
[10] "sbj10" "sbj11" "sbj12" "sbj13" "sbj14" "sbj15"

Thus, you should try something like:

datafile$subject <- sprintf("sbj%02.f", datafile$subject)
A5C1D2H2I1M1N2O1R2T1
  • 184,536
  • 28
  • 389
  • 466