I have subject codes where the last digit of the code is the session number. Using substring I can create a new session number variable, however the session number still needs to be removed from the original subject code. (AN11GR1 should become AN11GR)
Here is a reproducible example with a solution:
structure(list(subject = c("AN11GR1", "AN11GR2", "BR13ST1", "BR13ST2"
), target = c("m_ve", "k_ck", "m_ve", "k_ck"), corrAns = c("o",
"i", "o", "i"), RT = c(2.9254148, 1.7154148, 2.6454148, 1.9254148
)), class = "data.frame", row.names = c(NA, -4L)) -> myData
myData$session <- substring(myData$subject, nchar(myData$subject))
myData$subject <- substring(myData$subject, 1, nchar(myData$subject)-1)
myData <- subset(myData, select = c(subject, session, RT))
myData
Ideas on how to remove the last character in a concise and convenient way? (For the most concise solution see akrun's comment.)
CodePudding user response:
Add this line to your code. Using substring
and nchar
you could define to remove the last character:
myData$subject <- substring(myData$subject, 1, nchar(myData$subject)-1)
myData
subject session RT
1 AN11GR 1 2.925415
2 AN11GR 2 1.715415
3 BR13ST 1 2.645415
4 BR13ST 2 1.925415