简体   繁体   English

如何在R中将字符串向量转换为整数向量?

[英]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? 如何将其转换为R中看起来像这样的数字数组向量?

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

A generalized approach: 通用方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM