Reputation: 194
I have a df with column named 'error (range)' containing the values with error range like below,
error (range) |
---|
25 (20 - 30) |
42 (39 - 48) |
35 (32 - 38) |
But I want to extract the values and my df columns should look like,
error | error_min | error_max |
---|---|---|
25 | 20 | 30 |
42 | 39 | 48 |
35 | 32 | 38 |
Can anyone help me with the coding in R to achieve this ?
Upvotes: 0
Views: 146
Reputation: 887028
In base R
, read with read.table
after removing the brackets and -
with gsub
read.table(text=gsub("\\(|\\)|-", "", df[["error (range)"]]),
header = FALSE, strip.white = TRUE, fill = TRUE,
col.names = c("error", "error_min", "error_max"))
error error_min error_max
1 25 20 30
2 42 39 48
3 35 32 38
df <- structure(list(`error (range)` = c("25 (20 - 30)", "42 (39 - 48)",
"35 (32 - 38)")), class = "data.frame", row.names = c(NA, -3L
))
Upvotes: 0
Reputation: 79208
library(tidyverse)
df %>%
set_names('error')%>%
separate(error, c('error', 'error_min', 'error_max'),
convert = TRUE, extra = 'drop')
error error_min error_max
1 25 20 30
2 42 39 48
3 35 32 38
Upvotes: 2