简体   繁体   中英

Splitting a single column in r into 4 separate columns

I am in need of splitting a single data frame column (ID) into five separate columns (A, B, C, D, E). The columns need to be split by:

  • A - First Letter
  • B - All numbers until the second letter
  • C - All letters until the last letter
  • D - Last number
  • E - Last letter

Heres an example:

Before

ID Conc
1 A01HGF1a  132
2 D02SDV2b  453

After

A B C D E Conc
1 A 01 HGF 1 a  132
2 D 02 SDV 2 b  453

I have attempted to use separate() from tidyr but cannot figure out how to utilize regex properly. Any help is much appreciated!

Here is what I have attempted thus far

`separate(df, ID, into = c("A", "B", "C", "D","E"), sep = "(^.)(\\d+)(\\S+)(\\d+)(\\S+)")`

You could use sub here for a base R option:

df$A <- sub("^(\\w).*", "\\1", df$ID)
df$B <- sub("^\\w(\\d+).*", "\\1", df$ID)
df$C <- sub("^\\w\\d+(\\D+).*", "\\1", df$ID)
df$D <- sub(".*?(\\d+)\\D+$", "\\1", df$ID)
df$E <- sub(".*?(\\D+)$", "\\1", df$ID)
df

        ID Conc A  B   C D E
1 A01HGF1a  132 A 01 HGF 1 a
2 D02SDV2b  453 D 02 SDV 2 b

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM