简体   繁体   English

如何从reshape2包中“恢复”熔化功能?

[英]How to “back” melt function from reshape2 package?

That's my data: 那是我的数据:

> head(data)

    id C1 C2 C3 B1 B2 B3 Name
    12 3  12 8  1  3  12 Agar
    14 4  11 9  5  12 14 LB
    18 7  17 6  7  14 16 YEF
    20 9  15 4  3  11 17 KAN

so I used a melt function from reshape2 package to reorganize my data. 所以我使用了来自reshape2包的melt函数来重组我的数据。 Now it looks like that: 现在看起来像这样:

dt <- melt(data, measure.vars=2:7)

> head(dt)

   n    v variable value   rt
1 id Name        p    C1   1
2 12 Agar        p     3   2
3 14   LB        p     4   3
4 18  YEF        p     7   6
5 20  KAN        p     9   3
6 id Name        u    C2   1

I did some calculations on my data and now there is an extra column. 我对数据做了一些计算,现在有一个额外的列。 Let's call it "rt". 我们称之为“ rt”。 I'd like to transform my data now to the previous "state" with this an extra column. 现在,我想通过增加一列将数据转换为先前的“状态”。 Do you know any function which would be useful ? 您知道有用的任何功能吗?

dput(dt)
structure(list(n = structure(c(5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 
4L, 5L, 1L, 2L, 3L, 4L), class = "factor", .Label = c("12", "14", 
"18", "20", "id")), v = structure(c(4L, 1L, 3L, 5L, 2L, 4L, 1L, 
3L, 5L, 2L, 4L, 1L, 3L, 5L, 2L, 4L, 1L, 3L, 5L, 2L, 4L, 1L, 3L, 
5L, 2L, 4L, 1L, 3L, 5L, 2L), class = "factor", .Label = c("Agar", 
"KAN", "LB", "Name", "YEF")), variable = structure(c(1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 
4L, 4L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L), .Label = c("p", 
"u", "k", "l", "t", "h"), class = "factor"), value = c("C1", 
"3", "4", "7", "9", "C2", "12", "11", "17", "15", "C3", "8", 
"9", "6", "4", "B1", "1", "5", "7", "3", "B2", "3", "12", "14", 
"11", "B3", "12", "14", "16", "17")), .Names = c("n", "v", "variable", 
"value"), row.names = c(NA, -30L), class = "data.frame")

In the "reshape2" universe, melt and *cast go hand-in-hand. 在“ reshape2”宇宙中, melt*cast齐头并进。

Here's an example of melt ing a data.frame and dcast ing it back to its original form. 这是melt data.frame并将其重新dcast回其原始形式的示例。 You would need to take a similar approach with your data. 您将需要对数据采取类似的方法。

mydf <- data.frame(A = LETTERS[1:3], B = 1:3, C = 4:6)
mydf
#   A B C
# 1 A 1 4
# 2 B 2 5
# 3 C 3 6

library(reshape2)

mDF <- melt(mydf, id.vars="A")
mDF
dcast(mDF, A ~ variable, value.var="value")
#   A B C
# 1 A 1 4
# 2 B 2 5
# 3 C 3 6

In the dcast step, think of the items before the ~ as being the "id" variables, and those coming after as being the resulting column names. dcast步骤中,将~之前的项目视为“ id”变量,并将其之后的项目视为结果列名称。 value.var should be the column from which the values will fill in the resulting "grid" created by the id variables and column names. value.var应该是列,值将从中填充ID变量和列名创建的结果“网格”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 来自 reshape2 包的熔化函数 - melt function from reshape2 package R Shiny-如何使用“融化”功能(reshape2程序包)创建堆叠的条形图 - R Shiny - How to use the “melt” function (reshape2 package) to create a stacked barplot 如何更改reshape2包中的melt.data.frame函数将“变量”列返回到“字符”类? - How to change the melt.data.frame function in reshape2 package returned “variable” column to “character” class? 我如何使用reshape2包中的熔铸法获得以下答案 - how would I get the below answer using melt/cast from reshape2 package 时间戳上的 reshape2 熔化函数给出了 NA - reshape2 melt function on timestamps gives NAs 如何用 tidyr 替换数组的 reshape2::melt? - How to replace reshape2::melt for an array with tidyr? 如何使用reshape2包中的melt()来堆叠数据的分类标签以产生多个并排的boxpot - How to use melt() from the reshape2 package to stack categorical labels of data to produce multiple side-by-side boxpots 使用reshape2 :: melt和reshape2 :: dcast在用户定义的函数中调用变量 - Calling variable in user-defined function with reshape2::melt and reshape2::dcast 在MatLab中有效的R中的reshape2中是否存在类似的“融化”功能? - Is there a similar function to 'melt' in reshape2 in R that works in MatLab? reshape2融化警告信息 - reshape2 melt warning message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM