使用更宽的枢轴 (R) 将多条线合并为一条线 [英] Using pivot wider (R) to make multiple lines into a single line

查看:35
本文介绍了使用更宽的枢轴 (R) 将多条线合并为一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一如既往地为简单的 Q 道歉.我正在努力扩大支点以做我想做的事.

Apologies for the simple Q as always. I'm struggling to get pivot wider to do what I want.

我有一个这样的制表符分隔数据集:

I have a tab delimited dataset like this:

ID  filepath
ID1 /tmp/ID1_1.txt
ID1 /tmp/ID1_2.txt
ID2 /tmp/ID2_1.txt
ID2 /tmp/ID2_2.txt
ID3 /tmp/ID3_1.txt
ID3 /tmp/ID3_2.txt

我想把它改成这样:

ID  filepath_1  filepath_2
ID1 /tmp/ID1_1.txt  /tmp/ID1_2.txt
ID2 /tmp/ID2_1.txt  /tmp/ID2_2.txt
ID3 /tmp/ID3_1.txt  /tmp/ID3_2.txt

我认为更宽的支点是可行的方法,但它表现不佳..

I figure pivot wider is the way to go, but it's not behaving..

希望得到一些指导!

推荐答案

你需要一个标识符来表示哪一行进入哪一列:

you need an identifier to signalize which row goes into which column:

# dummy data read from plain text
df <- data.table::fread("ID  filepath
ID1 /tmp/ID1_1.txt
ID1 /tmp/ID1_2.txt
ID2 /tmp/ID2_1.txt
ID2 /tmp/ID2_2.txt
ID3 /tmp/ID3_1.txt
ID3 /tmp/ID3_2.txt") 

df %>%
    dplyr::group_by(ID) %>%
    dplyr::mutate(rn = paste0("filepath_",dplyr::row_number())) %>%
    dplyr::ungroup() %>%
    tidyr::pivot_wider(names_from = rn, values_from = filepath)

# A tibble: 3 x 3
  ID    filepath_1     filepath_2    
  <chr> <chr>          <chr>         
1 ID1   /tmp/ID1_1.txt /tmp/ID1_2.txt
2 ID2   /tmp/ID2_1.txt /tmp/ID2_2.txt
3 ID3   /tmp/ID3_1.txt /tmp/ID3_2.txt

这篇关于使用更宽的枢轴 (R) 将多条线合并为一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆