Imagine the following dataset (column vector):
df <- data.frame(a=c("AB3474","AB3482","AB3458","AB3487","AB3471","AB3452"))
df
a
1 AB3474
2 AB3482
3 AB3458
4 AB3487
5 AB3471
6 AB3452
Now I want to build a new vector which gets values, that "a" has on the fifth position. So the resulting df should look like this:
df_new
a new
1 AB3474 7
2 AB3482 8
3 AB3458 5
4 AB3487 8
5 AB3471 7
6 AB3452 5
I "sapplied" over the splitted string (using sapply
and strsplit
), but I guess there are simpler and hopefully faster ways for solving this.
Any suggestions?
Use this:
df_new <- within(df, new <- substr(a, 5, 5))
Result:
a new
1 AB3474 7
2 AB3482 8
3 AB3458 5
4 AB3487 8
5 AB3471 7
6 AB3452 5
EDIT: to answer the comment below:
within(df, new <- paste0(substr(a, 5, 5), ifelse(as.numeric(substr(a, 6, 6))>5, "b", "a")))
Result:
a new
1 AB3474 7a
2 AB3482 8a
3 AB3458 5b
4 AB3487 8b
5 AB3471 7a
6 AB3452 5a
Note that as.numeric
is preffered to avoid lexical comparisons.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With