Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R: Extracting charcters from string vectors to data frame rows

I have a data frame which has one colomn, a list of words. I’d like to extract the charcters from each word and have it stored as a position colomn in the data frame. For example, if the dataframe is defined like this:

words <- c('which', 'there', 'their', 'would') 
words <- as.data.frame(words)  

I’d like it to look like this at the end:

words first_pos second_pos third_pos fourth_pos fifth_pos
which w h i c h
there t h e r e
their t h e i r
would w o u l d

What I have so far is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

position <- c("first_pos", "second_pos", "third_pos", "fourth_pos", "fifth_pos")
words[position] <- NA
dismantled <- str_split(words$words,"")

This dismantles the words and creates the colomns I need. However, I could use some help filling the rows of the colomns with the letters.

>Solution :

We could use separate after a space between each character in words:

library(tidyverse)
words %>%
  mutate(words1 =  sub("\\s+$", "", gsub('(.{1})', '\\1 ', words))) %>% 
  separate(words1, into = paste0(1:5, "_pos"))
  words 1_pos 2_pos 3_pos 4_pos 5_pos
1 which     w     h     i     c     h
2 there     t     h     e     r     e
3 their     t     h     e     i     r
4 would     w     o     u     l     d
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading