I have data in a data frame in the form of:
structure(list(O2Range = c("112 MAX", "16/19", "16/190", "12 MAX",
NA, NA, NA, NA, NA, NA, NA, "16/20", "18/22", NA, "16/20", NA,
"11/13", NA, "16/190", NA)), row.names = c(NA, -20L), class = c("tbl_df",
"tbl", "data.frame"))
As may be apparent, low and high O2 readings are separated by a '/' in the column, but occasionally it is listed as a number and then 'MAX' (ie: 112 MAX).
I am attempting to separate this column into two new columns via:
library(tidyverse)
data$O2High <- if (str_detect(data$O2Range, "/")) {str_split_fixed(data$O2Range, fixed("/"), 2)[, 2]
} else {str_split_fixed(data$O2Range, fixed(" "), 2)[, 2]}
data$O2Low <- if (str_detect(data$O2Range, "/")) {str_split_fixed(data$O2Range, fixed("/"), 2)[, 1]
} else {str_split_fixed(data$O2Range, fixed(" "), 2)[, 1]}
However, the result doesn't turn out as expected:
structure(list(O2High = c("MAX", "", "", "MAX", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", ""), O2Low = c("112",
"16/19", "16/190", "12", "", "", "", "", "", "", "", "16/20",
"18/22", "", "16/20", "", "11/13", "", "16/190", "")), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame"))
There seems to be something going on with my if/else statement, but I can't work out the issue. Any ideas?
Thank you, Kris
Aucun commentaire:
Enregistrer un commentaire