在R中创建“元数据”字段

时间:2018-10-22 20:18:56

标签: r

我有一个与此相似的数据框:

id <- c(123,234,123,234)
task <- c(54,23,12,58)
a <- c(23,67,45,89)
b <- c(78,45,65,45)

df <- data.frame(id,task,a,b)
> df
   id task  a  b
1 123   54 23 78
2 234   23 67 45
3 123   12 45 65
4 234   58 89 45

我为每个ID分别为a和b:

df$score <- rowMeans(subset(df, select = c(3:4)), na.rm = TRUE)
> df
   id task  a  b score
1 123   54 23 78  50.5
2 234   23 67 45  56.0
3 123   12 45 65  55.0
4 234   58 89 45  67.0

对于每个ID,我的总得分如下:

out <- ddply(df, 1, summarise,
                    overall = mean(score, na.rm = TRUE))
> out
   id overall
1 123   52.75
2 234   61.50

但是我想要最终输出的是一个新列,其中包含进入总分的分数及其任务ID,如下所示:

   id overall                                meta
1 123   52.75 "task_scores":[{"54":50.5,"12":55}]
2 234   61.50   "task_scores":[{"23":56,"58":67}]

我将如何使用R做到这一点?

3 个答案:

答案 0 :(得分:2)

我们可以利用jsonlite来创建结构

library(jsonlite)
library(plyr)
ddply(df, "id", summarise, overall = mean(score, na.rm = TRUE),
    meta = paste0('"task_scores":', 
              toJSON(setNames(as.data.frame.list(score), task))))
#   id overall                                meta
#1 123   52.75 "task_scores":[{"54":50.5,"12":55}]
#2 234   61.50   "task_scores":[{"23":56,"58":67}]

答案 1 :(得分:0)

我不知道如何使该元数据字典成为副手,但是您可以执行以下操作:

library(dplyr)
library(magrittr)
out <- df %>% group_by(id) %>%  mutate(overall = mean(score))

> out
# A tibble: 4 x 6
# Groups:   id [2]
     id  task     a     b score overall
  <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl>
1   123    54    23    78  50.5    52.8
2   234    23    67    45  56      61.5
3   123    12    45    65  55      52.8
4   234    58    89    45  67      61.5

因此df既具有汇总分数,又将数据保留在原始行中。

答案 2 :(得分:0)

您可以进行一些突变。粘贴您的统计信息,获取平均行数,然后获取分组平均数。

library(dplyr)
df %>%
  mutate(score = rowMeans(subset(., select = c(3:4)), na.rm = TRUE)) %>% 
  group_by(id) %>% 
  mutate(overall = mean(score)) %>% 
  mutate(tally = paste(task, score, sep = ":", collapse = ","))

  # A tibble: 4 x 7
# Groups:   id [2]
     id  task     a     b score overall tally        
  <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl> <chr>        
1   123    54    23    78  50.5    52.8 54:50.5,12:55
2   234    23    67    45  56      61.5 23:56,58:67  
3   123    12    45    65  55      52.8 54:50.5,12:55
4   234    58    89    45  67      61.5 23:56,58:67 

要获得所需的最终输出,只需选择并切片即可。

    df %>%
  mutate(score = rowMeans(subset(., select = c(3:4)), na.rm = TRUE)) %>% 
  group_by(id) %>% 
  mutate(overall = mean(score)) %>% 
  mutate(tally = paste(task, score, sep = ":", collapse = ",")) %>% 
  select(id, overall, tally) %>% 
  slice(1)

  # A tibble: 1 x 3
     id overall tally        
  <dbl>   <dbl> <chr>        
1   123    52.8 54:50.5,12:55
2   234    61.5 23:56,58:67