- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试从中 reshape 数据集(mydata - 片段)
sample species cell_nr biovol
1 41442bay_1 Mytilus sp. 6.22 1243.04
2 41502elba_1 Mytilus sp. 1.35 260.64
3 41502bay_3 Mytilus sp. 2.74 548.21
4 41443bay_2 M. edulis 599.14 114028.15
5 41411elba_2 M. edulis 5107.51 1021502.16
对此(结果)
sample variable Mytilus sp. M. edulis
1 41442bay_1 cell_nr 6.22 0
2 41442bay_1 biovol 1243.04 0
3 41443bay_2 cell_nr 0 599.14
4 41443bay_2 biovol 0 114028.15
到目前为止,我在 R 中使用了 reshape2
mymelt <- melt(mydata, id=c("species", "sample"))
result <- dcast(mymelt, sample+variable~species)
但它聚合了我的变量
Aggregation function missing: defaulting to length
我需要我的一对变量的唯一 ID,以便在不聚合的情况下 reshape - 正如我通过阅读这两个线程所理解的那样:how-to-use-cast-in-reshape-without-aggregation & reshaping-data-frame-with-duplicates
然而,我卡在了这一点上。欢迎任何帮助,并在此先感谢您。
//已编辑
//编辑2
这是“mydata”的一个子集——起始表——两个采样点和两个变量 & 和分类单元
structure(list(sample = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("41411bay_1",
"41411elba_1"), class = "factor"), genspec = structure(c(1L,
2L, 5L, 6L, 8L, 9L, 10L, 11L, 13L, 18L, 14L, 15L, 16L, 17L, 19L,
20L, 21L, 22L, 23L, 12L, 24L, 25L, 26L, 27L, 3L, 4L, 5L, 6L,
7L, 7L, 8L, 9L, 10L, 11L, 14L), .Label = c("Achnanthes_taeniata",
"Asterionella_formosa", "Chaetoceros_ceratosporus", "Chaetoceros_compressus",
"Chaetoceros_simplex", "Chaetoceros_socialis", "Chaetoceros_sp.",
"Chaetoceros_wighamii", "Chroococcus_minimus", "Chrysophyte_",
"Cryptophyte_", "decaying_dino", "decaying_dino_", "Gymnodinium_sp.",
"Melosira_nummuloides", "Monoraphidium_contortum", "Mougeotia_sp.",
"Mytilus_sp.", "Navicula_sp.", "Protoperidinium_pellucidum",
"Protoperidinium_sp.", "Quadrigula_sp.", "Rhabdoderma_lineare",
"Skeletonema_costatum", "Surirella_sp.", "Thalassionema_nitzschioides",
"Thalassiosira_sp."), class = "factor"), total_cell_nr = c(570.14,
142.54, 30.54, 95.02, 213.8, 6246.1, 1924.23, 71.27, 47.51, 23.76,
71.27, 23.76, 35.63, 11.88, 35.63, 59.39, 47.51, 35.63, 95.02,
59.39, 6235.91, 11.88, 35.63, 11.88, 487.34, 314.42, 15.72, 110.05,
408.74, 31.44, 267.25, 35471.82, 13119.72, 534.51, 15.72), total_biovol = c(114028.15,
74830.97, 25900.68, 23850.89, 500084.7, 51217.98, 769690, 15465.07,
342702.1, 11877.93, 5485537.87, 102340.26, 1460.99, 64200.22,
74830.97, 1640342.42, 656754.62, 7483.1, 2375.59, 428377.62,
860556.18, 950234.57, 37059.15, 35633.8, 207121.44, 107530.22,
13331.23, 27621.43, 163904.98, 12608.08, 625105.87, 290868.96,
5247886.36, 115988.01, 1210045.12)), .Names = c("sample", "genspec",
"total_cell_nr", "total_biovol"), class = "data.frame", row.names = c(NA,
-35L))
我正在做这个
mymelt <- melt(mydata, id.vars=c("genspec", "sample"))
mymelt$indx <- with(mymelt, ave(seq_along(genspec), genspec, sample, FUN=seq_along))
result <- dcast(mymelt, sample+variable+indx~genspec, value.var='value', fill=0)
我希望结果是 4 个 obs。 (两个站点和两个变量),但我得到了 7 个观测值。 bay_1 有重复的样本,但 elba_1 没有——这发生在原始数据集中的整个结果中。我想这是一个非常基本的问题,答案很简单,但我看不到。
//编辑3
好的,我知道这里发生了什么——我的样本中有重复的 genspec(即物种)。这给 akrun 的整体工作答案带来了困惑。为了明白我的意思,请对上面粘贴的 df 使用以下命令 - 我删除了重复的样本,一切正常:
mydata <- mydata[-30,]
mymelt <- melt(mydata, id.vars=c("genspec", "sample"))
mymelt$indx <- with(mymelt, ave(seq_along(genspec), genspec, sample, FUN=seq_along))
result <- dcast(mymelt, sample+variable+indx~genspec, value.var='value', fill=0)
最佳答案
您可能需要创建一个序列列
mymelt$indx <- with(mymelt, ave(seq_along(species), species, FUN=seq_along))
dcast(mymelt, sample+variable+indx~species, value.var='value', fill=0)
# sample variable indx M. edulis Mytilus sp.
#1 41411elba_2 cell_nr 2 5107.51 0.00
#2 41411elba_2 biovol 4 1021502.16 0.00
#3 41442bay_1 cell_nr 1 0.00 6.22
#4 41442bay_1 biovol 4 0.00 1243.04
#5 41443bay_2 cell_nr 1 599.14 0.00
#6 41443bay_2 biovol 3 114028.15 0.00
#7 41502bay_3 cell_nr 3 0.00 2.74
#8 41502bay_3 biovol 6 0.00 548.21
#9 41502elba_1 cell_nr 2 0.00 1.35
#10 41502elba_1 biovol 5 0.00 260.64
如果数据集仍然有重复项,尝试
mymelt$indx <- with( mymelt,
ave(seq_along(species),
species,
sample,
FUN=seq_along
)
)
dcast(mymelt, sample+variable+indx~species, value.var='value', fill=0)
mydata <- structure(
list(sample = c("41442bay_1", "41502elba_1", "41502bay_3", "41443bay_2", "41411elba_2"),
species = c("Mytilus sp.", "Mytilus sp.", "Mytilus sp.", "M. edulis", "M. edulis"),
cell_nr = c(6.22, 1.35, 2.74, 599.14, 5107.51),
biovol = c(1243.04, 260.64, 548.21, 114028.15, 1021502.16)
),
.Names = c("sample", "species", "cell_nr", "biovol"),
class = "data.frame",
row.names = c("1", "2", "3", "4", "5")
)
关于没有聚合的 reshape2 dcast - seq 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28565961/
我有一个 Cassandra 集群,里面有 4 个表和数据。 我想使用聚合函数(sum,max ...)发出请求,但我在这里读到这是不可能的: http://www.datastax.com/docu
我有以下两张表 Table: items ID | TITLE 249 | One 250 | Two 251 | Three 我投票给这些: Table: votes VID | IID | u
这个问题在这里已经有了答案: Update MongoDB field using value of another field (12 个答案) 关闭 3 年前。 我想根据另一个“源”集合的文档中
我的收藏包含以下文件。我想使用聚合来计算里面有多少客户,但我遇到了一些问题。我可以获得总行数,但不能获得总(唯一)客户。 [{ _id: "n001", channel: "Kalip
我有下表 Id Letter 1001 A 1001 H 1001 H 1001 H 1001 B 1001 H 1001 H 1001
得到一列的表 ABC。 “创建”的日期列。所以样本值就像; created 2009-06-18 13:56:00 2009-06-18 12:56:00 2009-06-17 14:02:0
我有一个带有数组字段的集合: {[ name:String buyPrice:Int sellPrice:Int ]} 我试图找到最低和最高买入/卖出价格。在某些条目中,买入或卖出价格为零
我有以下问题: 在我的 mongo db 中,我有以下结构: { "instanceId": "12", "eventId": "0-1b", "activityType":
下面给出的是我要在其上触发聚合查询的 Elasticsearch 文档。 { "id": 1, "attributes": [ { "fieldId": 1,
我正在使用 Django 的 aggregate query expression总计一些值。最终值是一个除法表达式,有时可能以零作为分母。如果是这种情况,我需要一种方法来逃避,以便它只返回 0。 我
我正在学习核心数据,特别是聚合。 当前我想要做的事情:计算表中在某些条件上具有逆关系的多对关系的记录数。 目前我正在这样做: NSExpression *ex = [NSExpression expr
我需要有关 Delphi 中的 ClientDatasets 的一些帮助。 我想要实现的是一个显示客户的网格,其中一列显示每个客户的订单数量。我将 ClientDataset 放在表单上并从 Delp
我的集合有 10M 个文档,并且有一个名为 movieId 的字段;该文档具有以下结构: { "_id" : ObjectId("589bed43e3d78e89bfd9b779"), "us
这个问题已经有答案了: What is the difference between association, aggregation and composition? (21 个回答) 已关闭 9
我在 elasticsearch 中有一些类似于这些示例的文档: { "id": ">", "list": [ "a", "b", "c" ] } { "id"
我正在做一些聚合。但是结果完全不是我所期望的,似乎它们没有聚合索引中与我的查询匹配的所有文档,在这种情况下 - 它有什么好处? 例如,首先我做这个查询: {"index":"datalayer","t
假设我在 ES 中有这些数据。 | KEY | value | |:-----------|------------:| | A |
可能在我的文档中,我有一个被分析的文本字段。我只是在ElasticSearch AggregationAPI中迷路了。我需要2种不同情况的支持: 情况A)结果是带有计数标记(条款)的篮子下降。 情况B
我正在为网上商店构建多面过滤功能,如下所示: Filter on Brand: [ ] LG (10) [ ] Apple (5) [ ] HTC (3) Filter on OS: [ ] Andr
我有一个父/子关系并且正在搜索 child 。 是否可以在父属性上创建聚合? 例如parent 是 POST,children 是 COMMENT。如果父项具有“类别”属性,是否可以搜索 COMMEN
我是一名优秀的程序员,十分优秀!