2
我有以下列格式,尺寸串的柱:如何使用stringr從R中的維度字符串中提取高度和寬度?
41 1/4 X 29 3/8" (104.8 X74.6釐米)
7' 1" ×31" (216 X78.8釐米)
6' 6 3/4" ×6' 6 3/4" (200×200釐米)
如何提取釐米的高度和寬度,以一個單獨的列中的每個? 欲使用stringr
和dplyr
。
我有以下列格式,尺寸串的柱:如何使用stringr從R中的維度字符串中提取高度和寬度?
41 1/4 X 29 3/8" (104.8 X74.6釐米)
7' 1" ×31" (216 X78.8釐米)
6' 6 3/4" ×6' 6 3/4" (200×200釐米)
如何提取釐米的高度和寬度,以一個單獨的列中的每個? 欲使用stringr
和dplyr
。
我們可以嘗試
library(stringr)
do.call(rbind,
lapply(str_extract_all(df1$Col1,
"(?<=\\()[0-9.]+|[0-9.]+(?=\\scm)"), as.numeric))
# [,1] [,2]
#[1,] 104.8 74.6
#[2,] 216.0 78.8
#[3,] 200.0 200.0
如果我們需要使用dplyr
library(dplyr)
library(purrr)
str_extract_all(df1$Col1, "(?<=\\()[0-9.]+|[0-9.]+(?=\\scm)") %>%
map(~as.numeric(.)) %>%
do.call(rbind,.)
# [,1] [,2]
#[1,] 104.8 74.6
#[2,] 216.0 78.8
#[3,] 200.0 200.0
或者與extract
從tidyr
library(tidyr)
extract(df1, Col1, into=c("Col1", "Col2"),
"^[^(]+\\(([0-9.]+)\\D+([0-9.]+).*")
# Col1 Col2
#1 104.8 74.6
#2 216 78.8
#3 200 200
感謝您對不同的方式。這非常有幫助。 – MartinW