R 如何在数据帧中融化选定的单个列

R 如何在数据帧中融化选定的单个列,r,reshape,melt,R,Reshape,Melt,使用此代码: dat <- structure(list(Probes = structure(1:6, .Label = c("1415670_at", "1415671_at", "1415672_at", "1415673_at", "1415674_a_at", "1415675_at" ), class = "factor"), Genes = structure(c(2L, 1L, 4L, 5L, 6L, 3L), .Label = c("Atp6v0d1", "Copg1",

使用此代码:

dat <- structure(list(Probes = structure(1:6, .Label = c("1415670_at",
"1415671_at", "1415672_at", "1415673_at", "1415674_a_at", "1415675_at"
), class = "factor"), Genes = structure(c(2L, 1L, 4L, 5L, 6L,
3L), .Label = c("Atp6v0d1", "Copg1", "Dpm2", "Golga7", "Psph",
"Trappc4"), class = "factor"), FOO = c(1.133, 1.068,
1.01, 0.943, 1.048, 1.053)), .Names = c("Probes", "Genes", "FOO"
), row.names = c(NA, 6L), class = "data.frame")
library(reshape2)
melt(dat[,ncol(dat)])
我想做的是选择最后一列并将其融化成如下所示:

FOO 1.133 
FOO 1.068 
FOO 1.010 
FOO 0.943 
FOO 1.048 
FOO 1.053
我该怎么做

我被以下代码困住了:

dat <- structure(list(Probes = structure(1:6, .Label = c("1415670_at",
"1415671_at", "1415672_at", "1415673_at", "1415674_a_at", "1415675_at"
), class = "factor"), Genes = structure(c(2L, 1L, 4L, 5L, 6L,
3L), .Label = c("Atp6v0d1", "Copg1", "Dpm2", "Golga7", "Psph",
"Trappc4"), class = "factor"), FOO = c(1.133, 1.068,
1.01, 0.943, 1.048, 1.053)), .Names = c("Probes", "Genes", "FOO"
), row.names = c(NA, 6L), class = "data.frame")
library(reshape2)
melt(dat[,ncol(dat)])

选择“FOO”列后,我们可以使用
stack

stack(dat['FOO'])

dat[,'FOO']
不是
data.frame
。它是一个
向量
。我们可以在
?Extract
中找到
[
[[
等之间的差异。当只有一列时,
[
的默认选项是
drop=TRUE
。如果我们使用
来指定行索引,我们可以指定
drop=FALSE

stack(dat[, "FOO", drop=FALSE])
如果我们不知道列名,只需使用
ncol

stack(dat[ncol(dat)])

不同之处在于我们使用它的方式和返回的对象。

预期的输出是什么?一个有2列的data.frame?一个有1列+行名的data.frame?还有什么?比如说,我不知道“FOO”,我只想选择最后一列。我如何修改代码以使用索引。@请注意,堆栈可以用于字符和数字或一个组合,但不是你的生活中的因素example@rawr-我可以补充一点,这是非常明智的-不允许将看起来相同但意义完全不同的分类值组合在一起是一个明智的设计选择。