Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R-从列中的字符串中提取数字_R - Fatal编程技术网

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")