简体   繁体   English

在 R 中按用户名和时间合并以获取长格式数据

[英]merge by username and time in R for long format data

I have a long format data frame.我有一个长格式数据框。 A username may have 1 to multiple observations in time (1,2,3),一个用户名在时间上可能有 1 到多个观察值 (1,2,3),


username score time 

a         10     1

a         20     2

a         10     2

a         30     3

b         10     2

b         20     3



I hope to merge the dataset by username and time and get a mean (score)我希望通过用户名和时间合并数据集并得到一个平均值(分数)

For each time (1,2,3), a username will have only one score for one time.对于每一次 (1,2,3),用户名一次只有一个分数。

This means that for each username, he/she will have 3 observations in time (1,2,3)这意味着对于每个用户名,他/她将在时间上有 3 个观察值 (1,2,3)

Something like this:像这样的东西:


username score time 

a         10     1

a         15     2

a         30     3

b         10     2

b         20     3

b         na     1

If we need a summarised output如果我们需要总结output

library(dplyr)
library(hablar)
library(tidyr)
df2 <- df1 %>%
          group_by(username, time) %>%
          summarise(score = mean_(score), .groups = 'drop') %>%
          complete(username, time)

-output -输出

# A tibble: 6 x 3
#  username  time score
#  <chr>    <int> <dbl>
#1 a            1    10
#2 a            2    15
#3 a            3    30
#4 b            1    NA
#5 b            2    10
#6 b            3    20

data数据

df1 <- structure(list(username = c("a", "a", "a", "a", "b", "b"), score = c(10L, 
20L, 10L, 30L, 10L, 20L), time = c(1L, 2L, 2L, 3L, 2L, 3L)), 
      class = "data.frame", row.names = c(NA, 
-6L))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM