简体   繁体   中英

How to prevent tidyr's separate function from pulling in row numbers and then dropping data

I am trying to write a line of code to separate a text string whenever a capital letter in encountered without removing the letter. The approach I have taken is as follows:

set.seed(1)

# create a dataframe of fused alpha numeric codes that I wish to separate
df1 <- as.data.frame(matrix(
    paste0(sample(LETTERS, 20, replace = TRUE), sample(seq(1, 7, 0.1), 20, replace = TRUE)), 
    nrow = 10)) %>% unite(col = "ab", sep = "")
df1

# Add a space (" ") before any captial letter encountered
df2 <- df1 %>% mutate(ab = gsub('([[:upper:]])', ' \\1', ab))
df2

# use separate to split the column based on the space
df3 <- df2 %>% separate(col=ab, into=c("a", "b"), sep = " ")
df3

When I run separate I get a warning and the output is not correct:

#Warning message:
#Expected 2 pieces. Additional pieces discarded in 10 rows [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. 
#> df3
#   a    b
#1      Y3
#2    D4.6
#3      G5
#4    A3.4
#5    B5.5
#6    W4.6
#7    K4.6
#8    N4.3
#9    R5.1
#10   S3.4

The contents intended for column "a" have been placed on column "b", whilst those intended for "b" appear to have been removed entirely.

Another option is to make a more precise regex from the very beginning.

Eg

df1 |>
 separate(col  = ab,
          into = c("a", "b"),
          sep  = "(?<=\\d)(?=[[:upper:]])")

Output:

      a    b
1  B1.8 Z4.3
2    M5 U6.7
3    N5 Q5.1
4  V4.9 B6.5
5    N4 V1.2
6  H2.8 J5.1
7  Q3.6 J1.3
8  J3.8 G2.9
9  B1.2 W4.7
10 L1.6 O3.5

This is because you create a white space before your first letter: to remove it, you can use trimws or str_trim :

df1 %>% 
  mutate(ab = trimws(gsub('([[:upper:]])', ' \\1', ab))) %>%
  separate(col=ab, into=c("a", "b"), sep = " ")
      a    b
1    Y3 A5.3
2  D4.6 U2.4
3    G5 U4.2
4  A3.4 J2.9
5  B5.5 V4.4
6  W4.6 N1.5
7  K4.6 J1.9
8  N4.3 G5.1
9  R5.1 I4.7
10 S3.4 O5.6

I later worked out that the row numbers are being included as a column and that I can get around this problem by acknowledging and deleting the "n" column:

df3 <- df2 %>% separate(col=ab, into=c("n", "a", "b"), sep = " ") %>% 
  select(-n)
df3

However, this is verbose, and further I can't see any previous literature or documentation describing this behaviour in separate . Am I missing something and is there a neater way of preventing this behaviour?

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