通过保留第一列来融化数据框 [英] Melt the dataframe by keeping the first column

查看:65
本文介绍了通过保留第一列来融化数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的示例中,如何使data.frame看起来像输出:

How can I have the data.frame look like an output in the example below:

  created_at  negative  positive
1 2020-03-06 0.8897419 0.7564513
2 2020-03-07 0.9041667 0.7989865

输出需要:

  created_at  x         y
1 2020-03-06 negative   0.8897419 
2 2020-03-07 negative   0.9041667 
1 2020-03-06 positive   0.7564513
2 2020-03-07 positive   0.7989865

我尝试了melt函数,但是没有用。

I tried the melt function, but it didn't work.

推荐答案

一种流行的方法是 pivot_longer from tidyr

One popular approach is pivot_longer from tidyr.

library(tidyr)
data %>% 
  pivot_longer(-created_at,names_to = "x",values_to = "y")
# A tibble: 4 x 3
#  created_at x            y
#* <fct>      <chr>    <dbl>
#1 2020-03-06 negative 0.890
#2 2020-03-06 positive 0.756
#3 2020-03-07 negative 0.904
#4 2020-03-07 positive 0.799

这篇关于通过保留第一列来融化数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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