I have a data frame, df
that looks something like this:
date sample
1 29-Apr 1,000 (1/4)
2 29-Apr 1,000 (1/4)
3 28-Apr 1,970
4 27-Apr 1,000 (1/4)
5 25-Apr 1,000 (1/4)
...
How can I extract the value in parenthesis and create a new column from it?
I can extract the values in parenthesis:
matches <- regexpr("\\(.*?\\)", df$Sample_Size)
fractions_with_parens <- regmatches(df$Sample_Size, matches)
fractions <- gsub("[\\(\\)]", "", more)
But this will remove the non-matches, so the vector does match the length of the dataframe's rows. So in this example row 3 will be missing.