Spandyie
Spandyie

Reputation: 945

How to convert a vector of string into vector of integers in R?

I have a vector string which looks like this

A <- c("162&u", "139&u", "87&us", "175&u", "54&us", "25&us", "46&us","16650", "16776", "16689", "16844")

How do I convert it into a vector of numeric arrays that looks like this in R?

 A <- c(162,139,87, 175, 54,25,46,16650, 16776, 16689, 16844)

Upvotes: 0

Views: 155

Answers (2)

Henry Cyranka
Henry Cyranka

Reputation: 3060

B <- as.numeric(gsub("&.*","",A))

Upvotes: 0

Pierre L
Pierre L

Reputation: 28461

A generalized approach:

as.numeric(gsub("\\D+", "", A))
#[1]   162   139    87   175    54    25    46 16650 16776 16689 16844

Upvotes: 1

Related Questions