R函数用于将单元格中的逗号分隔值转换为具有相同行名称的多行数据。

huangapple go评论77阅读模式
英文:

R function for transforming comma-separated values in a cell into multiple rows with same row name?

问题

Orthogroup Sequence
0 Seq1
0 Seq2
0 Seq3
1 Seq4
英文:

I have a two-column dataframe in R: the first column is a broad category, and the second column contains comma-separated items within the broad category. This is what it looks like:

Orthogroup Sequences
0 Seq1, Seq2, Seq3
1 Seq4

And this is what I would like it to look like:

Orthogroup Sequence
0 Seq1
0 Seq2
0 Seq3
1 Seq4

To be honest I'm not even really sure where to start... any help is much appreciated!

答案1

得分: 2

你可以使用 tidyr 包中的 separate_rows() 函数来实现这个。

library(tidyverse)
Orthogroup <- c(0, 1)
Sequences <- c("Seq1, Seq2, Seq3", "Seq4")
df <- data.frame(Orthogroup, Sequences)
df %>%
  separate_rows(Sequences, sep = ", ")
#> # A tibble: 4 × 2
#>   Orthogroup Sequences
#>        <dbl> <chr>    
#> 1          0 Seq1     
#> 2          0 Seq2     
#> 3          0 Seq3     
#> 4          1 Seq4
英文:

You can accomplish this with separate_rows() from the package tidyr.

library(tidyverse)
Orthogroup &lt;- c(0, 1)
Sequences &lt;- c(&quot;Seq1, Seq2, Seq3&quot;, &quot;Seq4&quot;)
df &lt;- data.frame(Orthogroup, Sequences)
df %&gt;%
  separate_rows(Sequences, sep = &quot;, &quot;)
#&gt; # A tibble: 4 &#215; 2
#&gt;   Orthogroup Sequences
#&gt;        &lt;dbl&gt; &lt;chr&gt;    
#&gt; 1          0 Seq1     
#&gt; 2          0 Seq2     
#&gt; 3          0 Seq3     
#&gt; 4          1 Seq4

huangapple
  • 本文由 发表于 2023年2月16日 08:08:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466613.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定