-2

Is there a function in R that I can cut a value in vector.

for example i got this vec:

40754831597
64278107602
64212163451

and each vale in the vec i want to cut so from the number pos 3 to 6 for example and get a new vector look like this

7548
2781
2121

and so on

zx8754
  • 52,746
  • 12
  • 114
  • 209
David_12
  • 17
  • 1
  • 4

2 Answers2

1

I don't really get why you would like to do this, but here you go:

# assuming it's a character vector
substring(vec,3,6)

# if it's numeric
substring(as.character(vec),3,6)

#output
#[1] "7548" "2781" "2121"
Val
  • 6,585
  • 5
  • 22
  • 52
0

We can use sub

sub(".{2}(.{4}).*", "\\1", v1)
#[1] "7548" "2781" "2121"

data

v1 <- c(40754831597, 64278107602, 64212163451)
akrun
  • 874,273
  • 37
  • 540
  • 662