R:如何将一行旋转到列中?

发布于 2025-02-03 03:41:15 字数 879 浏览 1 评论 0原文

我想我了解dplyr pivot_longer()和pivot_wider()函数的基础知识,但是现在我有一个混乱的数据表(最初在excel中):

reprex <- tibble(
  Sample = c("Colour","A","B"),
  cells1green = c("green","3917","4370"),
  cells1red = c("red","267","299"),
  cells2green = c("green","4443","7082"),
  cells2red = c("red","1438","1694"))

在excel,第2列&amp; 3均为单元格1和第4列&amp; 5均为命名细胞2。我在名称中添加了“绿色”和“红色”,因为R无论如何都会更改重复列名的名称。我想知道是否有一种方法可以将第一个数据行(“绿色”,“红色”,“绿色”,“红色”)转换为列(“颜色”)并融合Cells1 ...列将其融合到一列和单元格2 ...列成一个列。最后一步是pivot_longer最终拥有这张桌子:

reprex_final <- tibble(
    Sample = c("A","A","A","A","B","B","B","B"),
    Celltype = c("Cells1","Cells1","Cells2","Cells2","Cells1","Cells1","Cells2","Cells2"),
    Colour = c("green","red","green","red","green","red","green","red"),
    Signal = c(3917,267,4443,1438,4370,299,7082,1694))

也许有人对如何进行有一个想法。那将非常有帮助。我希望我提供足够的信息。

I think I understand the basics of the dplyr pivot_longer() and pivot_wider() functions, but now I have a messy data table (originally in excel):

reprex <- tibble(
  Sample = c("Colour","A","B"),
  cells1green = c("green","3917","4370"),
  cells1red = c("red","267","299"),
  cells2green = c("green","4443","7082"),
  cells2red = c("red","1438","1694"))

In excel, Columns 2 & 3 are both named cells1 and Columns 4 & 5 are both named cells2. I added "green" and "red" to the names as R changes the name of a duplicate column name anyway. I'm wondering if there is a way to turn just the first data row ("green","red","green","red") into a column ("Colour") and fuse the cells1... columns into one column and the cells2... columns into one column. The last step would be to pivot_longer to eventually have this table:

reprex_final <- tibble(
    Sample = c("A","A","A","A","B","B","B","B"),
    Celltype = c("Cells1","Cells1","Cells2","Cells2","Cells1","Cells1","Cells2","Cells2"),
    Colour = c("green","red","green","red","green","red","green","red"),
    Signal = c(3917,267,4443,1438,4370,299,7082,1694))

Maybe somebody has an idea about how to proceed. That would be very helpful. I hope I provided enough information.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹤舞 2025-02-10 03:41:15

一个选项是首先摆脱第一行,然后使用pivot_longer喜欢:

library(tidyr)
library(dplyr)

reprex |> 
  slice(-1) |> 
  pivot_longer(-Sample, names_to = c("Celltype", "Colour"), 
               names_pattern = "^(.*\\d)(.*)$", values_to = "Signal")
#> # A tibble: 8 × 4
#>   Sample Celltype Colour Signal
#>   <chr>  <chr>    <chr>  <chr> 
#> 1 A      cells1   green  3917  
#> 2 A      cells1   red    267   
#> 3 A      cells2   green  4443  
#> 4 A      cells2   red    1438  
#> 5 B      cells1   green  4370  
#> 6 B      cells1   red    299   
#> 7 B      cells2   green  7082  
#> 8 B      cells2   red    1694

One option would be to first get rid of the first row and use pivot_longer like so:

library(tidyr)
library(dplyr)

reprex |> 
  slice(-1) |> 
  pivot_longer(-Sample, names_to = c("Celltype", "Colour"), 
               names_pattern = "^(.*\\d)(.*)
quot;, values_to = "Signal")
#> # A tibble: 8 × 4
#>   Sample Celltype Colour Signal
#>   <chr>  <chr>    <chr>  <chr> 
#> 1 A      cells1   green  3917  
#> 2 A      cells1   red    267   
#> 3 A      cells2   green  4443  
#> 4 A      cells2   red    1438  
#> 5 B      cells1   green  4370  
#> 6 B      cells1   red    299   
#> 7 B      cells2   green  7082  
#> 8 B      cells2   red    1694
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文