My data set is stored in a single column table named "Formula" which looks like this:
row.identity..main.ID.
C5H6O2N3
C10H12N
C5H6O2N3S
I want to extend the current table, where in each column the letters are written and in the line below the coresponding number is shown. Basically I want to have something like this:
row.identity..main.ID. C H O N S X
C5H6O2N3 5 6 2 3 0 0
C10H12N 10 12 0 1 0 0
C5H6O2N3S 5 6 2 3 1 0
It would be great when the code is flexible for even longer data set with variating letters. So far, I tried to implement the solution from Onyambu.
library(tidyverse)
library(stringr)
Formula%>%mutate(row.identity..main.ID.=gsub("\\b([A-Za-z]+)\\b","\\30",row.identity..main.ID.),
elements=str_extract_all(row.identity..main.ID.,"[A-Za-z]+"),
value=str_extract_all(row.identity..main.ID.,"\\d+"))%>%
unnest()%>%pivot_wider(elements,value,fill=0)
However this is resulting in several errors like "Incompatible lengths: 4, 3." and/or cols
is now required when using unnest().