R-从列中的字符串中提取数字
我的数据以这种格式存储R-从列中的字符串中提取数字,r,R,我的数据以这种格式存储 District Andamans (01), Andaman & Nicobar Islands (35) 在单个列的所有行中。我想用R将它们添加到格式为01-35的新列中。我尝试使用 as.numeric(unlist(str_extract_all(abc$District, '\\d+'))) 但是它给我提供了单独的输出,我不能将其加载到与上面的数据列(590列)长度相同的一列(1180列)中。如果我们需要特定格式,请使用lappy在列表中循环(因为
District Andamans (01), Andaman & Nicobar Islands (35)
在单个列的所有行中。我想用R将它们添加到格式为01-35的新列中。我尝试使用
as.numeric(unlist(str_extract_all(abc$District, '\\d+')))
但是它给我提供了单独的输出,我不能将其加载到与上面的数据列(590列)长度相同的一列(1180列)中。如果我们需要特定格式,请使用
lappy
在列表
中循环(因为str\u extract\u all
返回列表
和粘贴
library(stringr)
sapply(str_extract_all(abc$District, "\\d+"), function(x) paste(x, collapse="-"))
#[1] "01-35"
数据
abc
abc <- structure(list(District = "District Andamans (01), Andaman & Nicobar Islands (35)"), .Names = "District", row.names = c(NA,
-1L), class = "data.frame")