Christian
Christian

Reputation: 26417

Turning a list of characters that contain numbers into integers in R

Is there a faster way in R to turn a list of characters like c("12313","21323") into an integer list like c(12313, 21323) other than writing a for loop myself?

Upvotes: 0

Views: 1213

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368389

The as.* functions are already vectorized:

R> txt <- c("123456", "7890123")
R> as.numeric(txt)
[1]  123456 7890123
R> sum(as.numeric(txt))
[1] 8013579
R> 

In general, 'thinking vectorized' is the way to go with R, but it takes some practice.

Upvotes: 3

Related Questions