Reputation: 64074
With this code:
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")
I created the following data frame:
Probes Genes FOO
1 1415670_at Copg1 1.133
2 1415671_at Atp6v0d1 1.068
3 1415672_at Golga7 1.010
4 1415673_at Psph 0.943
5 1415674_a_at Trappc4 1.048
6 1415675_at Dpm2 1.053
What I want to do is to select last column and melt it into like this:
FOO 1.133
FOO 1.068
FOO 1.010
FOO 0.943
FOO 1.048
FOO 1.053
How can I do that?
I'm stuck with this code:
library(reshape2)
melt(dat[,ncol(dat)])
Upvotes: 3
Views: 1939
Reputation: 887981
We can use stack
after selecting the 'FOO' column.
stack(dat['FOO'])
Or
melt(dat['FOO'])
The dat[,'FOO']
is not a data.frame
. It is a vector
. We can find the difference between [
, [[
etc in the ?Extract
. When there is a single column, the default option with [
is drop=TRUE
. We can specify drop=FALSE
if we are using the ,
to specify the row index.
stack(dat[, "FOO", drop=FALSE])
If we don't know the column name, just use ncol
stack(dat[ncol(dat)])
The difference is in how we are using it and the object returned.
Upvotes: 4