I'm trying to create a new column which just contains certain numeric data from an expression.
Here's my data: https://pastebin.com/hYg3zqYz
I just need the numbers that come after Bipolar in column 12.
Here's what works
p <- df %>%
select(where(~ any(stringr::str_detect(.x, "Bipolar")))) #returns correct column
Where I try then try to make a new column that pulls just the text, it only ever returns the first row, not sure what I'm doing wrong.
p %>%
mutate(group = "sr_bipol",
sr_bipol = as.numeric(stringr::str_extract(., "[0-9].[0-9]+"))) %>%
select(group, sr_bipol)
# A tibble: 20 × 2
group sr_bipol
<chr> <dbl>
1 sr_bipol 7.83
2 sr_bipol 7.83
3 sr_bipol 7.83
4 sr_bipol 7.83
5 sr_bipol 7.83
.....................
I also get the error code:
argument is not an atomic vector; coercing
After loading in a new case, I tried to use the following solutio but ran into a new error
new data: https://paste.kodi.tv/azuramoguh
df %>%
transmute(across(where(~ any(stringr::str_detect(.x, "Bipolar"))),
~ as.numeric(str_extract(.x, "(?<=Bipolar\\s)[0-9]\\.[0-9]+")),
.names = "sr_bipol{str_remove(.col, '[.]+')}"))
Error in `$<-.data.frame`(`*tmp*`, "call_text", value = c("df %>% ...", :
replacement has 13 rows, data has 12
Also tried:
df %>%
select(where(~ any(stringr::str_detect(.x, "Bipolar")))) %>% #this finds the column with the bipolar ##voltage data
transmute(group = "sr_bipol",
sr_bipol = as.numeric(
stringr::str_extract(pull(.,1), "[0-9].[0-9]+") #str_extract expects a vector, so "pull" to #change from df to vector
)
) %>%
bind_cols(carto_lv_sr_volt %>% select(x:z))
Error in `select()`:
! `where()` must be used with functions that return `TRUE` or `FALSE`.