简体   繁体   English

按行号R组合和追加不同长度的列

[英]Combining and appending columns of different lengths, by row number, R

I'm working with biochemical data from subjects, analysing the results by sex. 我正在处理受试者的生化数据,按性别分析结果。 I have 19 biochemical tests to analyse for each sex, for each of two drugs (haematology and anatomy tests coming later). 我有19种生化测试可对两种药物的每种性别进行分析(稍后将进行血液学和解剖学测试)。

For reasons of reproducibility of results and for preventing transcription errors, I am trying to summarise each test into one table. 出于结果可重复性和防止转录错误的原因,我尝试将每个测试汇总到一个表中。 Included in the table output, I need a column for the Dunnett post hoc comparison p-values. 在表格输出中,我需要一列用于Dunnett事后比较p值。 Because the Dunnett test compares to the control results, with a control and 3 drug levels I only get 3 p-values. 因为Dunnett检验与对照结果比较,在对照和3种药物水平下,我只得到3个p值。 However, I have 4 mean and sd values. 但是,我有4个均值和sd值。

Using ddply to get the mean and sd results (having limited the number of significant figures, I get a dataset that looks like this: 使用ddply来获取均值和sd结果(由于有效数字的数量有限,我得到了一个数据集,如下所示:

 Sex<- c(rep("F",4), rep("M",4))
 Druglevel <- c(rep(0:3,2))
 Sample <- c(rep(10,8))
 Mean <- c(0.44, 0.50, 0.46, 0.49, 0.48, 0.55, 0.47, 0.57)
 sd <- c(0.07, 0.07, 0.09, 0.12, 0.18, 0.19, 0.13, 0.41)
 Drug1Biochem1 <- data.frame(Sex, Druglevel, Sample, Mean, sd)

I have used glht in the package multcomp to perform the Dunnett tests on the aov object I constructed from undertaking a normal aov . 我已经在软件包multcomp使用glht ,对通过执行普通aov构造的aov对象执行Dunnett测试。 I've extracted the p-values from the glht summary (I've rounded these to three decimal places). 我从glht摘要中提取了p值(将这些值四舍五入到小数点后三位)。 The male and female analyses have been run using separate ANOVA so I have one set of output for each sex. 男性和女性分析是使用独立的ANOVA因此对于每种性别,我都有一组输出。 The female results are: 女性的结果是:

femaleR <- c(0.371, 0.973, 0.490) 

and the male results are: 男性结果为:

 maleR <- c(0.862, 0.999, 0.738)

How can I append a column for the p-values to my original dataframe (Drug1Biochem1) so that both femaleR and maleR are in that final column, with row 1 and row 5 of that column empty (ie no p-values for the control)? 如何将一列p值附加到原始数据框(Drug1Biochem1),以便femaleR和maleR都在该最后一列中,而该列的第1行和第5行为空(即,控件没有p值) ?

I wish to output the resulting combination to html, which can be inserted into a Word document so no transcription errors occur. 我希望将结果组合输出到html,可以将其插入到Word文档中,因此不会发生转录错误。 I have set a seed value so that the results of the program are reproducible (when I finally stop debugging). 我设置了一个种子值,以便程序的结果可再现(当我最终停止调试时)。

In summary, I would like a data frame (or table, or whatever I can output to html) that has the following format: 总而言之,我想要一个具有以下格式的数据框(或表,或任何我可以输出到html的东西):

 Sex       Druglevel       Sample     Mean     sd     p-value
 F         0               10         0.44     0.07   
 F         1               10         0.50     0.07   0.371
 F         2               10         0.46     0.09   0.973
 F         3               10         0.49     0.12   0.480
 M         0               10         0.48     0.18   
 M         1               10         0.55     0.19   0.862
 M         2               10         0.47     0.13   0.999
 M         3               10         0.57     0.41   0.738

For each test, I wish to reproduce this exact table. 对于每个测试,我希望重现此精确表。 There will always be 4 groups per sex, and there will never be a p-value for the control, which will always be summarised in row 1 (F) and row 5 (M). 每种性别始终有4组,并且控件永远没有p值,该值将始终在第1行(F)和第5行(M)中进行汇总。

You could try merge 您可以尝试merge

dN <- data.frame(Sex=rep(c('M', 'F'), each=3), Druglevel=1:3, 
                 pval=c(maleR, femaleR))

merge(Drug1Biochem1, dN, by=c('Sex', 'Druglevel'), all=TRUE)
#   Sex Druglevel Sample Mean   sd  pval
#1   F         0     10 0.44 0.07    NA
#2   F         1     10 0.50 0.07 0.371
#3   F         2     10 0.46 0.09 0.973
#4   F         3     10 0.49 0.12 0.490
#5   M         0     10 0.48 0.18    NA
#6   M         1     10 0.55 0.19 0.862
#7   M         2     10 0.47 0.13 0.999
#8   M         3     10 0.57 0.41 0.738

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

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