Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get first n characters from a string in R

I would like to extract three letters of each string for each row in df as below

Exampe:

df <- data.frame(name = c('Jame Bond', "Maria Taylor", "Micheal Balack"))
df
            name
1      Jame Bond
2   Maria Taylor
3 Micheal Balack

desired out

df_new 
        name
1      Jam_Bon
2      Mar_Tay
3      Mic_Bal

Any sugesstions for this using tidyverse?

like image 593
Anh Avatar asked Oct 26 '25 15:10

Anh


2 Answers

library(stringr)
library(dplyr)

df$name %>% 
  str_extract_all("(?<=(^|[:space:]))[:alpha:]{3}") %>% 
  map_chr(~ str_c(.x, collapse = "_"))

The stringr cheatsheet is very useful for working through these types of problems. https://www.rstudio.com/resources/cheatsheets/

Created on 2022-03-26 by the reprex package (v2.0.1)

like image 192
acammack1234 Avatar answered Oct 29 '25 05:10

acammack1234


You can try this with dplyr::rowwise(), stringr::str_split() and stringr::str_sub():

df_new <- df %>% 
  rowwise() %>% 
  mutate(name = paste(
    unlist(
      lapply(str_split(name, ' '), function(x){
        str_sub(x, 1, 3)
      })
    ), 
    collapse = "_"
  ))

I got the same result as you expected :

> df_new
# A tibble: 3 x 1
# Rowwise: 
  name   
  <chr>  
1 Jam_Bon
2 Mar_Tay
3 Mic_Bal
like image 38
Ao Sun Avatar answered Oct 29 '25 06:10

Ao Sun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!