I've currently been using code to split names of individual samples, change part of the sample name, and then rebind the strings together. The code works well when all names are the same length (ie: names are 8 characters long and it always splits after the first 4 characters), but when the names are different lengths, the code is no longer effective.
Essentially, individual names are 7 or 8 characters. The last 4 characters are what's important.
Example with 8 characters: Samp003A
Example with 7 characters: Sam003A
Is there a way to continue using strsplit to separate my names, but start from the end of the string rather the beginning, to keep the last 4 characters (003A
)?
Current code:
> RowList <- as.list(rownames(df1))
> RowListRes <- strsplit(as.character(RowList), "(?<=.{4})", perl = TRUE)
> RowListRes.df <- do.call(rbind, RowListRes)
> RowListRes.df[,1] <- "LY3D"
> dfnames <- apply(RowListRes.df, 1, paste, collapse="")
> rownames(df1) <- dfnames
It's line 2 that I'm trying hard to edit, so that I can split according to the last 4 characters.
Any help would be greatly appreciated!