R:将存储在行中的不同测量值的副本转置为单列 [英] R: transposing replicates of different measurements stored in rows into single columns

查看:67
本文介绍了R:将存储在行中的不同测量值的副本转置为单列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果标题令人误解,我感到非常抱歉,因为我不知道该如何正确解释(不确定术语是什么)。

I am very sorry if the title is misleading, as I don't know how to explain this properly (not sure what the terminology would be).

我有一些基本看起来像这样的数据:

I have some data that basically looks like this:

a<-c('AA01','AA02','AB01')
b<-c('AA','AA','AB')
c<-c('Y','N','N')
d<-c(1,2,3)
e<-c(4,5,6)
f<-c(7,8,9)
g<-c(11,22,33)
h<-c(44,55,66)
i<-c(77,88,99)
cols<-c("SampID","Characteristic1","Characteristic2","Mes1Rep1","Mes1Rep2","Mes1Rep3","Mes2Rep1","Mes2Rep2","Mes2Rep3")


df<-data.frame(a,b,c,d,e,f,g,h,i)
colnames(df)<-cols
df

  SampID Characteristic1 Characteristic2 Mes1Rep1 Mes1Rep2 Mes1Rep3 Mes2Rep1 Mes2Rep2 Mes2Rep3
1   AA01              AA               Y        1        4        7       11       44       77
2   AA02              AA               N        2        5        8       22       55       88
3   AB01              AB               N        3        6        9       33       66       99

在此示例中,Mes1Rep1指的是Mes2Rep2是该测量的第二次重复,依此类推。我想将重复项安排在列中。像这样的东西:

In this example, Mes1Rep1 refers to the first replicate of a type of measurement, Mes2Rep2 the second replicate of that measurement, and so on. I would like to arrange the replicates in columns instead. Something more like this:

  SampID Characteristic1 Characteristic2 Replicates Measurement1 Measurement2
1   AA01              AA               Y       Rep1            1           11
2   AA01              AA               Y       Rep2            4           44
3   AA01              AA               Y       Rep3            7           77
4   AA02              AA               N       Rep1            2           22
5   AA02              AA               N       Rep2            5           55
6   AA02              AA               N       Rep3            8           88
7   AB01              AB               N       Rep1            3           33
8   AB01              AB               N       Rep2            6           66
9   AB01              AB               N       Rep3            9           99

我将如何在R中执行此操作?我想我可以自己对行进行转置,但是有没有办法自动复制冗余值(例如本示例中的 SampID)?

How would I go about doing this in R? I think I can just transpose the rows themselves, but is there some way to also copy the redundant values (such as "SampID" in this example) automatically?

或者别无选择,只能手动执行此操作?

Or is there no choice but to do this manually?

感谢您抽出宝贵的时间阅读本文,并提供您的帮助和/或指导!

Thank you for taking the time to read this, and for offering your help and/or guidance!

推荐答案

我们可以使用融化 data.table 中的$ c>可以采取多种措施 模式

We can use melt from data.table which can take multiple measure patterns

library(data.table)
melt(setDT(df), measure = patterns("Mes1", "Mes2"), 
   variable.name = "Replicates", 
    value.name = c("Measurement1", "Measurement2"))[
      order(SampID)][, Replicates := paste0("Rep", Replicates)][]
#   SampID Characteristic1 Characteristic2 Replicates Measurement1 Measurement2
#1:   AA01              AA               Y       Rep1            1           11
#2:   AA01              AA               Y       Rep2            4           44
#3:   AA01              AA               Y       Rep3            7           77
#4:   AA02              AA               N       Rep1            2           22
#5:   AA02              AA               N       Rep2            5           55
#6:   AA02              AA               N       Rep3            8           88
#7:   AB01              AB               N       Rep1            3           33
#8:   AB01              AB               N       Rep2            6           66
#9:   AB01              AB               N       Rep3            9           99






或在 base R

reshape(df, idvar = c("SampID", "Characteristic1", "Characteristic2"), 
       varying = list(4:6, 7:9), direction = "long")

这篇关于R:将存储在行中的不同测量值的副本转置为单列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆