0

我正在尝试使用融化功能重塑下表

structure(list(a = c(0.153705582462314, 0.0486852891805727, 0.0443466683559926, 
0.049402643366726, 0.10496252040361, 0.0605314701268217, 0.123066826124396, 
0.10436283816338, 0.123452539416624, 0.0921469079168177, 0.131665907599587, 
0.0927555742098017, 0.228148961056112, 0.130543777324655, 0.0843670900309334, 
0.131948120538527), b = c(0.158938848699283, 0.0487570327200071, 
0.0454810210056237, 0.0510635787328623, 0.103120937885508, 0.0639093015144946, 
0.111187181680815, 0.0978797635763352, 0.0792834772158317, 0.0582100024292881, 
0.0860846940492594, 0.0620370376768188, 0.11814043806398, 0.0825043302248793, 
0.0615953756935117, 0.0711048468042418), c = c(0.148449985263957, 
0.0514334902734327, 0.0448107469650824, 0.0553431826494535, 0.11011576290828, 
0.0596050964023732, 0.109924244560051, 0.100309207109092, 0.0772350806188979, 
0.0567484442878015, 0.0943868769266179, 0.0642831549581465, 0.117850661875511, 
0.0868600001807722, 0.0619469756159616, 0.0745909201443937), 
    d = c(0.153576142965318, 0.0440065816952808, 0.0383730598042165, 
    0.0466911489805908, 0.0883448739785253, 0.052233465825278, 
    0.0782617872165657, 0.0740854821951614, 0.0704324151657985, 
    0.051813360749928, 0.0865163379367009, 0.0581975106052581, 
    0.118039038983586, 0.082545661321027, 0.0567767394969306, 
    0.0641904998624335), e = c(0.161975563218496, 0.0457647898614343, 
    0.0394148591712433, 0.0454720734366032, 0.0822881130339494, 
    0.0520786880977144, 0.0772094145035842, 0.0685930881198674, 
    0.0634496037760497, 0.0475349902051384, 0.0730862457567602, 
    0.0539538999707352, 0.0918201356593523, 0.0711086911717703, 
    0.0541541288301524, 0.0575437259907984)), .Names = c("a", 
"b", "c", "d", "e"), class = "data.frame", row.names = c("Naphthalene", 
"Acenaphtylene", "Acenaphthene", "Fluorene", "Phenenthrene", 
"Anthracene", "Fluoranthene", "Pyrene", "Benzo(a)anthracene", 
"Chysene", "Benzo(b)fluoranthene", "Benzo(k)fluoranthene", "Benzo(a)pyrene", 
"Indeno(1.2.3-cd)pyrene", "Dibenz(a.h)anthracene", "Benzo(g.h.i)perylene"
))

我想用 colnames (a,b,c,d,e,f) 创建组

我使用了融化功能

dfm <- melt(test,id=c("a","b","c","d","e"))

但是,生成的表与原始表相同。

有人可以指出我正确的方式吗?它应该很简单,但我已经尝试了几种组合但没有结果。

4

2 回答 2

2

你实际上并没有说出你想要什么(如果这不是你想要的,那么改变你的问题)。但我猜您正在尝试跟踪存储为行名的化学名称:

test$chemical = rownames(test)

##Pointless melting as nothing really happens
##dfm is equal to test now
dfm = melt(test, id=c("a","b","c","d","e"))
head(dfm)
        a       b       c       d       e variable         value
1 0.15371 0.15894 0.14845 0.15358 0.16198 chemical   Naphthalene
2 0.04869 0.04876 0.05143 0.04401 0.04576 chemical Acenaphtylene
3 0.04435 0.04548 0.04481 0.03837 0.03941 chemical  Acenaphthene
4 0.04940 0.05106 0.05534 0.04669 0.04547 chemical      Fluorene
5 0.10496 0.10312 0.11012 0.08834 0.08229 chemical  Phenenthrene
6 0.06053 0.06391 0.05961 0.05223 0.05208 chemical    Anthracene

或者,您可能会追求:

dfm = melt(test, "chemical")
head(dfm)
      chemical variable   value
1   Naphthalene        a 0.15371
2 Acenaphtylene        a 0.04869
3  Acenaphthene        a 0.04435
4      Fluorene        a 0.04940
5  Phenenthrene        a 0.10496
6    Anthracene        a 0.06053
于 2013-02-06T21:09:36.493 回答
0

如果您所追求的是@csgillespie 发布的第二个选项,您也可以在base R 中以非常简单的方式执行此操作stack

mydf2 <- data.frame(chemical = rownames(mydf), stack(mydf))
head(mydf2)
#        chemical     values ind
# 1   Naphthalene 0.15370558   a
# 2 Acenaphtylene 0.04868529   a
# 3  Acenaphthene 0.04434667   a
# 4      Fluorene 0.04940264   a
# 5  Phenenthrene 0.10496252   a
# 6    Anthracene 0.06053147   a
于 2013-02-07T07:03:11.650 回答