我正在使用R中的两个制表符分隔的表:
表1 :多个样本(列)的物种(行)计数数据
SpeciesID 16Snem1 nem2 16Snem3 16Snem4
K00368 452.0 0.0 470.0 0.0
K00376 469.5 15.0 332.0 0.0
K00387 401.5 10.0 302.0 0.0
表2 :与每个样本相关的元数据
SampleID OceanRegion Habitat Depth
16Snem1 GulfofMexico Seep 1000
16Snem2 GulfofMexico Shore 1425
16Snem3 SouthernCalifornia LopheliaSite 400
16Snem4 SouthernCalifornia LopheliaSite 495
我的问题是:
merge
或bind
这两个表的最佳方法是什么?答案 0 :(得分:1)
数据表方法:
首先将您的第一个表熔化为长格式,然后在表2上执行左连接。
library( data.table )
melt( dt1, id.vars = "SpeciesID",
measure.vars = patterns( "^16Snem" ),
variable.name = "SampleID" )[ dt2, `:=`( OceanRegion = i.OceanRegion,
Habitat = i.Habitat,
Depth = i.Depth),
on = "SampleID"][]
# SpeciesID SampleID value OceanRegion Habitat Depth
# 1: K00368 16Snem1 452.0 GulfofMexico Seep 1000
# 2: K00376 16Snem1 469.5 GulfofMexico Seep 1000
# 3: K00387 16Snem1 401.5 GulfofMexico Seep 1000
# 4: K00368 16Snem2 0.0 GulfofMexico Shore 1425
# 5: K00376 16Snem2 15.0 GulfofMexico Shore 1425
# 6: K00387 16Snem2 10.0 GulfofMexico Shore 1425
# 7: K00368 16Snem3 470.0 SouthernCalifornia LopheliaSite 400
# 8: K00376 16Snem3 332.0 SouthernCalifornia LopheliaSite 400
# 9: K00387 16Snem3 302.0 SouthernCalifornia LopheliaSite 400
# 10: K00368 16Snem4 0.0 SouthernCalifornia LopheliaSite 495
# 11: K00376 16Snem4 0.0 SouthernCalifornia LopheliaSite 495
# 12: K00387 16Snem4 0.0 SouthernCalifornia LopheliaSite 495
没有绘图应该很容易。
使用的示例数据
library( data.table )
dt1 <- fread("SpeciesID 16Snem1 16Snem2 16Snem3 16Snem4
K00368 452.0 0.0 470.0 0.0
K00376 469.5 15.0 332.0 0.0
K00387 401.5 10.0 302.0 0.0")
dt2 <- fread("SampleID OceanRegion Habitat Depth
16Snem1 GulfofMexico Seep 1000
16Snem2 GulfofMexico Shore 1425
16Snem3 SouthernCalifornia LopheliaSite 400
16Snem4 SouthernCalifornia LopheliaSite 495")