Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alternative to sapply over strsplit of character vector

Tags:

string

split

r

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?

like image 659
beginneR Avatar asked Dec 26 '22 22:12

beginneR


1 Answers

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.

like image 72
Ferdinand.kraft Avatar answered Jan 06 '23 20:01

Ferdinand.kraft