您可以使用str_extract_all;要在小数点前捕获. 或,,请使用字符类[.,]:
library(stringr)
str_extract_all(x, "\\d+[.,]\\d+")
[[1]]
[1] "799.88" "966.01" "1634.17" "4714.35" "2992.45" "3200.66"
[[2]]
[1] "226.9" "312.29" "452.16" "1037.67" "1145.13" "1178.85"
[[3]]
[1] "375.99" "293.48" "749.61" "1832.05" "980.07" "1224.46"
[[4]]
[1] "4.10" "10.63" "8.91" "14.40" "22.52" "18.12"
[[5]]
[1] "6.88" "20.87" "17.30" "27.44" "27.68" "24.47"
[[6]]
[1] "727,671" "142,936" "630,353" "413,807" "86,717" "248,179"
要将它们全部放在一个向量中(而不是列表),请使用unlist:
unlist(str_extract_all(x, "\\d+[.,]\\d+"))
[1] "799.88" "966.01" "1634.17" "4714.35" "2992.45" "3200.66" "226.9" "312.29" "452.16" "1037.67" "1145.13"
[12] "1178.85" "375.99" "293.48" "749.61" "1832.05" "980.07" "1224.46" "4.10" "10.63" "8.91" "14.40"
[23] "22.52" "18.12" "6.88" "20.87" "17.30" "27.44" "27.68" "24.47" "727,671" "142,936" "630,353"
[34] "413,807" "86,717" "248,179"
数据:
x <- c("(799.88) (966.01) (1634.17) (4714.35) (2992.45) (3200.66)",
"Per capita monthly income 226.9 312.29 452.16 1037.67 1145.13 1178.85",
"(375.99) (293.48) (749.61) (1832.05) (980.07) (1224.46)", "Per capita income / Hour of work 4.10 10.63 8.91 14.40 22.52 18.12 ",
"(6.88) (20.87) (17.30) (27.44) (27.68) (24.47)", "Number of observations (with weight) 727,671 142,936 630,353 413,807 86,717 248,179"
)