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
  • 91,361
  • 17
  • 137
  • 196
dede
  • 1,129
  • 5
  • 15
  • 35
  • In your description you say you have just integers while in your code your actual values aren't. – David Arenburg Dec 10 '15 at 10:26
  • @DavidArenburg, because when I run the code `datafile$subject <- sub("^", "sbj", datafile$subject )` they become 'sbj4', 'sbj5', etc.. – dede Dec 10 '15 at 10:28
  • Try `paste0("sbj", stringi::stri_pad_left(1:12, 2, 0))`. You can replace `1:12` with your original `subject` column. Or just `sprintf("%02d", 1:12)` – David Arenburg Dec 10 '15 at 10:30

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
  • 190,393
  • 28
  • 405
  • 485