Noah Mba
Noah Mba

Reputation: 3

Extracting numeric values from square brackets in R

I have difficulties in extracting numeric values from within square brackets by using the stringr package.

I have a data frame with the following structure:

'data.frame':   18 obs. of  3 variables:
 $ participant: chr  "s07" "s08" "s10" "s11" ...
 $ position_1 : chr  "[-3.96002452]" "[-2.43317811]" "[0.57034622]" "[-2.93552563]" ...
 $ position_2 : chr  "[-6.40075519]" "[-2.26514695]" "[-0.98081968]" "[-2.91766826]" ...

Now, I would like to extract the numeric values that are within the square brackets, so that in the end I have a data frame which looks like this:

'data.frame':   18 obs. of  3 variables:
 $ participant: chr  "s07" "s08" "s10" "s11" ...
 $ position_1 : num -3.96002452 -2.43317811 0.57034622 -2.93552563 ...
 $ position_2 : num -6.40075519 -2.26514695 -0.98081968 -2.91766826 ...

I tried to use the stringr package but was not successful in getting the syntax to work.

Upvotes: 0

Views: 179

Answers (3)

jay.sf
jay.sf

Reputation: 72673

Using gsub with appropriate regex, no add. package needed.

df[2:3] <- lapply(df[2:3], \(x) as.numeric(gsub('\\[|\\]', '', x)))
df
#   participant position_1 position_2
# 1         s07 -3.9600245 -6.4007552
# 2         s08 -2.4331781 -2.2651470
# 3         s10  0.5703462 -0.9808197
# 4         s11 -2.9355256 -2.9176683

Data:

df <- structure(list(participant = c("s07", "s08", "s10", "s11"), position_1 = c("[-3.96002452]", 
"[-2.43317811]", "[0.57034622]", "[-2.93552563]"), position_2 = c("[-6.40075519]", 
"[-2.26514695]", "[-0.98081968]", "[-2.91766826]")), class = "data.frame", row.names = c(NA, 
-4L))

Upvotes: 2

RYann
RYann

Reputation: 683

example <- c("[-3.96002452]" ,"[-2.43317811]" ,"[0.57034622]", "[-2.93552563]")
readr::parse_number(example)

[1] -3.9600245 -2.4331781  0.5703462 -2.9355256

Use the readr package (install if you dont have it).

Upvotes: 1

TarJae
TarJae

Reputation: 78917

Use parse_number() from readr package:

library(tidyverse)

# the data
participant <-c("s07", "s08", "s10", "s11")
position_1 <- c("[-3.96002452]", "[-2.43317811]" ,"[0.57034622]", "[-2.93552563]")
position_2 <- c("[-6.40075519]", "[-2.26514695]", "[-0.98081968]" ,"[-2.91766826]" )

df <- tibble(participant, position_1, position_2)

# the code
df %>% 
  mutate(across(-participant, ~parse_number(.)))
  participant position_1 position_2
  <chr>            <dbl>      <dbl>
1 s07             -3.96      -6.40 
2 s08             -2.43      -2.27 
3 s10              0.570     -0.981
4 s11             -2.94      -2.92 

Upvotes: 2

Related Questions