# How to find the sum of numbers stored in character vector separated with a special character in R?

R ProgrammingServer Side ProgrammingProgramming

To find the sum of numbers stored in character vector separated with a special character in R, we can follow the below steps −

• First of all, create a vector having numbers stored in character vector separated with a special character.
• Then, use sapply and strsplit function to find the sum of numbers.

## Example1

Create a vector with numbers stored in character vector separated with hyphen −

Live Demo

> x1<-sample(c("10-4-5-19-10","11-2-15-19-10","21-21-50-90-10","15-25-52-91-
10","17-27-55-91-100","13-12-75-29-18","14-29-57-92-13"),80,replace=TRUE)
> x1

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

 [1] "11-2-15-19-10"   "11-2-15-19-10"    "13-12-75-29-18"  "15-25-52-91-10"
[5] "15-25-52-91-10"  "17-27-55-91-100"  "13-12-75-29-18"  "15-25-52-91-10"
[9] "13-12-75-29-18"  "10-4-5-19-10"     "17-27-55-91-100" "14-29-57-92-13"
[13] "15-25-52-91-10"  "21-21-50-90-10"   "15-25-52-91-10"  "15-25-52-91-10"
[17] "13-12-75-29-18"  "15-25-52-91-10"   "17-27-55-91-100" "10-4-5-19-10"
[21] "15-25-52-91-10"  "17-27-55-91-100"  "15-25-52-91-10"  "21-21-50-90-10"
[25] "10-4-5-19-10"    "10-4-5-19-10"     "10-4-5-19-10"    "15-25-52-91-10"
[29] "11-2-15-19-10"   "10-4-5-19-10"     "17-27-55-91-100" "14-29-57-92-13"
[33] "17-27-55-91-100" "13-12-75-29-18"   "11-2-15-19-10"   "14-29-57-92-13"
[37] "17-27-55-91-100" "17-27-55-91-100"  "11-2-15-19-10"   "21-21-50-90-10"
[41] "13-12-75-29-18"  "15-25-52-91-10"   "10-4-5-19-10"    "14-29-57-92-13"
[45] "15-25-52-91-10"  "13-12-75-29-18"   "11-2-15-19-10"   "11-2-15-19-10"
[49] "11-2-15-19-10"   "13-12-75-29-18"   "14-29-57-92-13"  "21-21-50-90-10"
[53] "11-2-15-19-10"   "11-2-15-19-10"    "21-21-50-90-10"  "14-29-57-92-13"
[57] "21-21-50-90-10"  "21-21-50-90-10"   "11-2-15-19-10"   "15-25-52-91-10"
[61] "10-4-5-19-10"    "11-2-15-19-10"    "11-2-15-19-10"   "13-12-75-29-18"
[65] "21-21-50-90-10"  "11-2-15-19-10"    "17-27-55-91-100" "21-21-50-90-10"
[69] "15-25-52-91-10"  "21-21-50-90-10"   "15-25-52-91-10"  "21-21-50-90-10"
[73] "15-25-52-91-10"  "15-25-52-91-10"   "21-21-50-90-10"  "10-4-5-19-10"
[77] "13-12-75-29-18"  "11-2-15-19-10"    "11-2-15-19-10"   "14-29-57-92-13"

## Find the sum of numbers in the vector

Using strsplit and sapply function along with sum function to find the sum of numbers in vector x1 −

Live Demo

> x1<-sample(c("10-4-5-19-10","11-2-15-19-10","21-21-50-90-10","15-25-52-91-
10","17-27-55-91-100","13-12-75-29-18","14-29-57-92-13"),80,replace=TRUE)
> sapply(strsplit(x1,"-"), function(x) sum(as.numeric(x)))

#### Output

 [1] 57 57 147 193 193 290 147 193 147 48 290 205 193 192 193 193 147 193 290
[20] 48 193 290 193 192 48 48 48 193 57 48 290 205 290 147 57 205 290 290
[39] 57 192 147 193 48 205 193 147 57 57 57 147 205 192 57 57 192 205 192
[58] 192 57 193 48 57 57 147 192 57 290 192 193 192 193 192 193 193 192 48
[77] 147 57 57 205

## Example2

Create a vector with numbers stored in character vector separated with hyphen −

> x2<-sample(c(“10-4-5-19-10”,”11-2-15-19-10”,”21-21-50-90-10”,”15-25-52-91-
10”,”17-27-55-91-100”),80,replace=TRUE)
> x2

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

 [1] “17-27-55-91-100” “21-21-50-90-10” “15-25-52-91-10” “11-2-15-19-10”
[5] “17-27-55-91-100” “10-4-5-19-10” “21-21-50-90-10” “21-21-50-90-10”
[9] “21-21-50-90-10” “10-4-5-19-10” “11-2-15-19-10” “17-27-55-91-100”
[13] “21-21-50-90-10”  “21-21-50-90-10”  “10-4-5-19-10” “10-4-5-19-10”
[17] “11-2-15-19-10”   “10-4-5-19-10”    “15-25-52-91-10” “11-2-15-19-10”
[21] “21-21-50-90-10”  “10-4-5-19-10”    “17-27-55-91-100” “11-2-15-19-10”
[25] “15-25-52-91-10”  “15-25-52-91-10”  “17-27-55-91-100” “10-4-5-19-10”
[29] “15-25-52-91-10”  “11-2-15-19-10”   “15-25-52-91-10”  “10-4-5-19-10”
[33] “11-2-15-19-10”   “21-21-50-90-10”  “10-4-5-19-10”    “10-4-5-19-10”
[37] “21-21-50-90-10”  “11-2-15-19-10”   “21-21-50-90-10”  “17-27-55-91-100”
[41] “17-27-55-91-100” “17-27-55-91-100” “11-2-15-19-10”   “17-27-55-91-100”
[45] “15-25-52-91-10”  “15-25-52-91-10”  “21-21-50-90-10”  “10-4-5-19-10”
[49] “21-21-50-90-10”  “11-2-15-19-10”   “11-2-15-19-10”   “10-4-5-19-10”
[53] “11-2-15-19-10”   “11-2-15-19-10”   “21-21-50-90-10”  “17-27-55-91-100”
[57] “17-27-55-91-100” “11-2-15-19-10”   “10-4-5-19-10”    “15-25-52-91-10”
[61] “11-2-15-19-10”   “21-21-50-90-10”  “11-2-15-19-10”   “11-2-15-19-10”
[65] “11-2-15-19-10”   “21-21-50-90-10”  “15-25-52-91-10”  “17-27-55-91-100”
[69] “21-21-50-90-10”  “11-2-15-19-10”   “11-2-15-19-10”   “17-27-55-91-100”
[73] “11-2-15-19-10”   “21-21-50-90-10”  “17-27-55-91-100” “11-2-15-19-10”
[77] “11-2-15-19-10”   “21-21-50-90-10”  “11-2-15-19-10”   “11-2-15-19-10”

## Find the sum of numbers in the vector

Using strsplit and sapply function along with sum function to find the sum of numbers in vector x2 −

> x2<-sample(c(“10-4-5-19-10”,”11-2-15-19-10”,”21-21-50-90-10”,”15-25-52-91-
10”,”17-27-55-91-100”),80,replace=TRUE)
> sapply(strsplit(x2,”-“), function(x) sum(as.numeric(x)))

## Output

[1] 290 192 193 57 290 48 192 192 192 48 57 290 192 192 48 48 57 48 193
[20] 57 192 48 290 57 193 193 290 48 193 57 193 48 57 192 48 48 192 57
[39] 192 290 290 290 57 290 193 193 192 48 192 57 57 48 57 57 192 290 290
[58] 57 48 193 57 192 57 57 57 192 193 290 192 57 57 290 57 192 290 57
[77] 57 192 57 57

## Example3

Create a vector with numbers stored in character vector separated with hash −

Live Demo

> x3<-
sample(c("13#12#75#29#18","14#29#57#92#13","41#62#15#89#30","11#12#85#49#14"
,"1#40#50#19#10"),80,replace=TRUE)
> x3

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

 [1] "1#40#50#19#10"  "41#62#15#89#30" "11#12#85#49#14" "41#62#15#89#30"
[5] "1#40#50#19#10"  "14#29#57#92#13" "14#29#57#92#13" "14#29#57#92#13"
[9] "41#62#15#89#30" "13#12#75#29#18" "41#62#15#89#30" "41#62#15#89#30"
[13] "41#62#15#89#30" "1#40#50#19#10"  "14#29#57#92#13" "14#29#57#92#13"
[17] "41#62#15#89#30" "14#29#57#92#13" "14#29#57#92#13" "14#29#57#92#13"
[21] "13#12#75#29#18" "41#62#15#89#30" "14#29#57#92#13" "11#12#85#49#14"
[25] "13#12#75#29#18" "41#62#15#89#30" "1#40#50#19#10" "14#29#57#92#13"
[29] "14#29#57#92#13" "41#62#15#89#30" "41#62#15#89#30" "11#12#85#49#14"
[33] "1#40#50#19#10"  "1#40#50#19#10" "13#12#75#29#18" "13#12#75#29#18"
[37] "13#12#75#29#18" "13#12#75#29#18" "13#12#75#29#18" "41#62#15#89#30"
[41] "41#62#15#89#30" "14#29#57#92#13" "13#12#75#29#18" "11#12#85#49#14"
[45] "1#40#50#19#10"  "13#12#75#29#18" "13#12#75#29#18" "41#62#15#89#30"
[49] "13#12#75#29#18" "13#12#75#29#18" "13#12#75#29#18" "1#40#50#19#10"
[53] "14#29#57#92#13" "14#29#57#92#13" "11#12#85#49#14" "41#62#15#89#30"
[57] "13#12#75#29#18" "1#40#50#19#10" "11#12#85#49#14" "1#40#50#19#10"
[61] "13#12#75#29#18" "11#12#85#49#14" "1#40#50#19#10" "14#29#57#92#13"
[65] "41#62#15#89#30" "11#12#85#49#14" "11#12#85#49#14" "41#62#15#89#30"
[69] "11#12#85#49#14" "14#29#57#92#13" "11#12#85#49#14" "1#40#50#19#10"
[73] "1#40#50#19#10"  "13#12#75#29#18" "11#12#85#49#14" "1#40#50#19#10"
[77] "11#12#85#49#14" "13#12#75#29#18" "14#29#57#92#13" "13#12#75#29#18"

## Find the sum of numbers in the vector

Using strsplit and sapply function along with sum function to find the sum of numbers in vector x3 −

Live Demo

> x3<-
sample(c("13#12#75#29#18","14#29#57#92#13","41#62#15#89#30","11#12#85#49#14"
,"1#40#50#19#10"),80,replace=TRUE)
> sapply(strsplit(x3,"#"), function(x) sum(as.numeric(x)))

## Output

 [1] 120 237 171 237 120 205 205 205 237 147 237 237 237 120 205 205 237 205 205
[20] 205 147 237 205 171 147 237 120 205 205 237 237 171 120 120 147 147 147 147
[39] 147 237 237 205 147 171 120 147 147 237 147 147 147 120 205 205 171 237 147
[58] 120 171 120 147 171 120 205 237 171 171 237 171 205 171 120 120 147 171 120
[77] 171 147 205 147
Updated on 13-Aug-2021 10:38:59