简体   繁体   English

如何为每个用户 ID 的问题创建一个包含一系列答案(在不同时间)的新数据框

[英]How to create a new dataframe with the serie of the answer ( at different times ) to a question for each user id

I'm working on a dataframe with a lot of questions and some people answered several times to the inquiry.我正在研究一个有很多问题的数据框,有些人多次回答了这个问题。 I would like to study the evolution of their answer.我想研究他们答案的演变。

I have a database that looks like:我有一个看起来像这样的数据库:

User ID用户身份 Time时间 Answer回答
User A用户A 2012-01 2012-01 5 5个
User B用户乙 2012-02 2012-02 6 6个
User B用户乙 2012-01 2012-01 5 5个
User B用户乙 2012-03 2012-03 6 6个
User A用户A 2012-02 2012-02 5 5个
User C用户C 2012-03 2012-03 6 6个

And I would like to have a dataframe with the answer of each user classed by time like that:我想要一个数据框,其中包含按时间分类的每个用户的答案:

User ID用户身份 2012-01 2012-01 2012-02 2012-02 2012-03 2012-03
User A用户A 5 5个 6 6个 X X
User B用户乙 5 5个 5 5个 X X
User C用户C X X X X 6 6个

Do you know how I could do that?你知道我该怎么做吗?

I've tried to use group by user ID but it didn't work.我试过按用户 ID 使用组,但没有用。

library(tidyr)

# Your data
df<-read.table(text="
User ID Time    Answer
User A  2012-01 5
User B  2012-02 6
User B  2012-01 5
User B  2012-03 6
User A  2012-02 5
User C  2012-03 6",
header=TRUE) 

df %>% 
  pivot_wider(names_from = Time,
              values_from=Answer)

# A tibble: 3 × 5
  User  ID    `2012-01` `2012-02` `2012-03`
  <chr> <chr>     <int>     <int>     <int>
1 User  A             5         5        NA
2 User  B             5         6         6
3 User  C            NA        NA         6

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用一组时间序列值的导数创建新列 - How to create a new column with the derivative of a set of time serie values 如何在 R 中创建一个新的 dataframe 组合每个 ID 可用的第一个日期和最后一个日期? - How can I create a new dataframe in R that combines the first date and last date available for each ID? 如何在 R 数据框对象的 Stack Overflow 问题/答案上创建表格? - How to create a table on a Stack Overflow question/answer from a R dataframe object? 如何拆分数据框,在每个列表数据框中创建新变量并取消拆分? - How to split dataframe, create new variable in each list dataframe, and unsplit? 如何为循环的 R 中的每次迭代创建一个新的 dataframe? - How to create a new dataframe for each iteration in R for loop? 在R中,向数据框的每个ID添加新行 - In R, adding a new row to dataframe, to each id 180个嵌套条件在一个单独的文件中,为我的数据框中的每一行创建一个新的id变量 - 180 nested conditions in a separate file to create a new id variable for each row in the my dataframe 如何创建一个新列,将值与来自不同 dataframe 的标题匹配 - How to create a new column, matching values with headers from a different dataframe 在 R 中创建一个 dataframe 列,指示每个行值已被采样的次数 - Create in R a dataframe column that indicates how many times each row value has been sampled 创建一个新的 dataframe 显示每列的总和 - Create a new dataframe showing the sum of each column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM