0

I'm currently doing annotation on sound and I want to work on this.

However, it happens that in one annotation, there are several species. So I have a dataframe with one column for the annotations, one for the time interval and others but no matter.

I'm working on R

Ex: A_B_C and I want to transform this line A_B_C in 3 lines A, B and C and I want to keep the other information of the first line in the other lines that have been created.

(Sorry for my english if there are any mistakes)

I have seen that it is possible to split thanks to the underscore. But in this code:

df$new <- substr(df$x, regexpr("_", df$x)+1, length(df$x))

It only creates a new column. And I don't want to do this.

It's my first post, sorry if it's not perfectly clear

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • Welcome to SO. To improve your post please make your example input clearer (is it a data.frame?) and give an example of the exact output. – s_baldur Mar 13 '23 at 11:02

1 Answers1

0

The {tidyr} package has a function separate_rows() which will split a column and insert the result as rows keeping the information from the other columns.

library(dplyr)
library(tidyr)

mydat <- tibble(x = "A_B_C", y = 1)

mydat %>% 
  separate_rows(x, sep = "_")
#> # A tibble: 3 × 2
#>   x         y
#>   <chr> <dbl>
#> 1 A         1
#> 2 B         1
#> 3 C         1

Created on 2023-03-13 with reprex v2.0.2

TimTeaFan
  • 17,549
  • 4
  • 18
  • 39