Сложное преобразование фрейма данных из длинного в широкое с использованием значений в нескольких «ключевых» столбцах в R

Может быть более чистый способ (с меньшим количеством кастингов), но я так делал это раньше в XQuery:

xquery version "1.0-ml"; 

import module namespace sem = "http://marklogic.com/semantics" 
      at "/MarkLogic/semantics.xqy";

let $triples := sem:sparql('
SELECT *
WHERE
{ ?subject ?predicate ?object }
')

return sem:database-nodes($triples ! sem:triple(map:get(., "subject"), map:get(., "predicate"), map:get(., "object"))) ! fn:base-uri(.)
0
задан mkpcr 18 January 2019 в 19:04
поделиться

1 ответ

Разделите ваши данные на два кадра данных, один для элементов управления и один для вмешательств, а затем объедините их вместе.

df
        study group_allocation    outcome bl_mean fu_timepoint fu_mean mean_diff
1 Smith, 1999   intervention_1 depression     6.5            6     5.2      -2.3
2 Smith, 1999          control depression     4.5            6     7.5        NA
3 Jones, 1996   intervention_1    anxiety     3.7           12     2.5      -3.8
4 Jones, 1996   intervention_2    anxiety     4.2           12     2.7      -3.6
5 Jones, 1996          control    anxiety     5.3           12     6.3        NA

 interventions<-df[grep("intervention", df$group_allocation),]

 interventions
        study group_allocation    outcome bl_mean fu_timepoint fu_mean mean_diff
1 Smith, 1999   intervention_1 depression     6.5            6     5.2      -2.3
3 Jones, 1996   intervention_1    anxiety     3.7           12     2.5      -3.8
4 Jones, 1996   intervention_2    anxiety     4.2           12     2.7      -3.6


 controls<-df[grep("control", df$group_allocation),]

 controls
        study group_allocation    outcome bl_mean fu_timepoint fu_mean mean_diff
2 Smith, 1999          control depression     4.5            6     7.5        NA
5 Jones, 1996          control    anxiety     5.3           12     6.3        NA

 names(controls)<-paste0("cg_", names(controls)) #add cg prefix to colnames

 new_df<-merge(interventions, controls, by.x="study", by.y="cg_study", all.x=TRUE)

 new_df
        study group_allocation    outcome bl_mean fu_timepoint fu_mean mean_diff cg_group_allocation cg_outcome cg_bl_mean cg_fu_timepoint cg_fu_mean cg_mean_diff
1 Jones, 1996   intervention_1    anxiety     3.7           12     2.5      -3.8             control    anxiety        5.3              12        6.3           NA
2 Jones, 1996   intervention_2    anxiety     4.2           12     2.7      -3.6             control    anxiety        5.3              12        6.3           NA
3 Smith, 1999   intervention_1 depression     6.5            6     5.2      -2.3             control depression        4.5               6        7.5           NA
0
ответ дан Esther 18 January 2019 в 19:04
поделиться
Другие вопросы по тегам:

Похожие вопросы: