Reputation: 723
From this string border-color:#002449;left:74.4%top;37%;
I would like to make the first percentage 74.4%
a variable called X
and the second percentage 37%
a variable called Y
.
I can compelete this with a str_extract and regex but I would rather create the variables X
and Y
by doing a strsplit.
What I am currently doing:
coord <- str_extract_all(s, "\\d+(\\.\\d+){0,1}%")[[1]]
X <- coord[1] ; Y <- coord[2]
This works fine, but my data.frame currently has 54 rows in it not just 1. I believe doing a strsplit will be better.
Any help will be appreciated. Please let me know if any further information is needed.
Upvotes: 2
Views: 127
Reputation: 887541
We can use rbind
the coord
library(stringr)
coord <- str_extract_all(s, "\\d+(\\.\\d+){0,1}%")
d1 <- as.data.frame(do.call(rbind, coord), stringsAsFactors=FALSE)
Another option with str_extract
would be
coord <- str_extract_all(s, "\\d+(?:%)")
d2 <- as.data.frame(do.call(rbind, coord), stringsAsFactors=FALSE)
With strsplit
, we could try
coord <- lapply(strsplit(s, '[0-9.]+%(*SKIP)(*F)|.',
perl=TRUE), setdiff, "")
d3 <- as.data.frame(do.call(rbind, coord), stringsAsFactors=FALSE)
s <- "border-color:#002449;left:74.4%top;37%;"
Upvotes: 2