在 R 中的 spread() 函数中使用 put 两个值列 [英] Use put two value columns in spread() function in R

查看:37
本文介绍了在 R 中的 spread() 函数中使用 put 两个值列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发布了一个 问题 最近询问如何将数据从长表重塑为宽表.然后我发现 spread() 是一个非常方便的函数.所以现在我需要对我之前的帖子做一些进一步的开发.

I just posted a question recently asking how to reshape data from a long table to a wide table. Then I found spread() is a quite handy function for doing this. So now I need some further development on my previous post.

假设我们有一个这样的表:

Let's suppose we have a table like this:

id1   |  id2 |  info  | action_time | action_comment  |
 1    | a    |  info1 |    time1    |        comment1 |
 1    | a    |  info1 |    time2    |        comment2 |
 1    | a    |  info1 |    time3    |        comment3 |
 2    | b    |  info2 |    time4    |        comment4 |
 2    | b    |  info2 |    time5    |        comment5 |

我想把它改成这样:

id1   |  id2 |  info  |action_time 1|action_comment1 |action_time 2|action_comment2 |action_time 3|action_comment3  |
 1    | a    |  info1 |    time1    |      comment1  |    time2    |      comment2  |    time3    |      comment3   |
 2    | b    |  info2 |    time4    |      comment4  |    time5    |      comment5  |             |                 |

所以这个问题和我之前的问题之间的区别是我添加了另一列,我也需要重新调整它.

So the difference between this question and my previous question is I added another column and I need it to be reshaped as well.

我正在考虑使用

library(dplyr)
library(tidyr)

df %>% 
  group_by(id1) %>% 
  mutate(action_no = paste("action_time", row_number())) %>%
  spread(action_no, value = c(action_time, action_comment))

但是当我在 value 参数中放入两个值时它给了我一条错误消息说:无效的列规范.

But it gives me an error message when I put two values in value argument saying : Invalid column specification.

我真的很喜欢使用这样的 %>% 运算符来操作数据的想法,所以我很想知道如何更正我的代码以实现这一点.

I really like the idea of using such %>% operator to manipulate data, so I'm keen to know how to correct my code to make this happen.

非常感谢您的帮助

推荐答案

尝试:

library(dplyr)
library(tidyr)

df %>%
  group_by(id1) %>%
  mutate(id = row_number()) %>%
  gather(key, value, -(id1:info), -id) %>%
  unite(id_key, id, key) %>%
  spread(id_key, value)

给出:

#Source: local data frame [2 x 9]

#  id1 id2  info 1_action_comment 1_action_time 2_action_comment 2_action_time 3_action_comment 3_action_time
#1   1   a info1         comment1         time1         comment2         time2         comment3         time3
#2   2   b info2         comment4         time4         comment5         time5               NA            NA

这篇关于在 R 中的 spread() 函数中使用 put 两个值列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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