-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
  • 46,390
  • 10
  • 104
  • 180
David_12
  • 17
  • 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,006
  • 3
  • 21
  • 44
0

We can use sub

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

data

v1 <- c(40754831597, 64278107602, 64212163451)
akrun
  • 789,025
  • 32
  • 460
  • 575