I have this dataframe, it looks like this:
I need to take the first character from the column at, the whole value in an, then put a counter on the end that increments for repeats in column an. This counter has to be always length of three. The end result is this:
So nothing here that dramatic, I was able to do this with the following code (prepare to be impressed):
library(stringr)
tk <- ""
for (i in 1:nrow(df)){
if (tk == df$an[i]){
counter <- counter + 1
} else {
tk <- df$an[i]
counter <- 1
}
df$ap[i] <- counter
}
df$ap <- paste0(substr(df$at, 1, 1), df$an, str_pad(df$ap, 3, pad="0"))
I'm so not satisfied with this debacle. It seems not very "R" and I'd like very much never to allow this to see the light of day. How can I make this more "R"?
I appreciate the advice.