I want to take the numbers after "narrow" (e.g. 20) and make another vector. Any idea how I can do that?
Asked
Active
Viewed 41 times
1
-
1It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Do not post data as images -- this is not easily reproducible. – MrFlick Feb 18 '20 at 20:13
-
You could try something along the lines of strsplit(data[,1],","). You'll have better luck getting an answer with reproducible data. – Daniel O Feb 18 '20 at 20:14
-
when working with data.table-format: `data.table::tstrsplit( Stimulus, ",")` will do the trick – Wimpel Feb 18 '20 at 20:20
-
readr ::parse_number(data$Stimulus) – AndS. Feb 18 '20 at 20:24
4 Answers
0
We can use sub
to remove the substring "Narrow", followed by a ,
and zero or more spaces (\\s+
), replace with blank (""
) and convert to numeric
df1$New <- as.numeric(sub("Narrow,\\s*", "", df1$Stimulus))

akrun
- 874,273
- 37
- 540
- 662
0
You could use separate
to separate the stimulus
column into two vectors.
library(tidyr)
df %>%
separate(col = stimulus,
sep = ", ",
into = c("Text","Number"))

Jonathan V. Solórzano
- 4,720
- 10
- 22
0
Maybe you can try the code below, using regmatches
df$new <- with(df, as.numeric(unlist(regmatches(stimulus,gregexpr("\\d+",stimulus)))))

ThomasIsCoding
- 96,636
- 9
- 24
- 81