没有 ID 变量的 dcast [英] dcast without ID variables

查看:30
本文介绍了没有 ID 变量的 dcast的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Reshape2 简介"包中,Sean C. Anderson 提供了以下示例.

In the "An Introduction to reshape2" package Sean C. Anderson presents the following example.

他使用空气质量数据并重命名列名

He uses the airquality data and renames the column names

names(airquality) <- tolower(names(airquality))

数据看起来像

#   ozone solar.r wind temp month day
# 1    41     190  7.4   67     5   1
# 2    36     118  8.0   72     5   2
# 3    12     149 12.6   74     5   3
# 4    18     313 11.5   62     5   4
# 5    NA      NA 14.3   56     5   5
# 6    28      NA 14.9   66     5   6

然后他将它们融化

aql <- melt(airquality, id.vars = c("month", "day"))

得到

#   month day variable value
# 1     5   1    ozone    41
# 2     5   2    ozone    36
# 3     5   3    ozone    12
# 4     5   4    ozone    18
# 5     5   5    ozone    NA
# 6     5   6    ozone    28

最后他得到了原始的(不同的列顺序)

Finally he gets the original one (different column order) by

aqw <- dcast(aql, month + day ~ variable)

我的问题

现在假设我们没有 ID 变量(即月份和日期),并按如下方式融合数据

My Quesiton

Assume now that we do not have ID variables (i.e. month and day) and have melted the data as follows

aql <- melt(airquality)

看起来像

#   variable value
# 1    ozone    41
# 2    ozone    36
# 3    ozone    12
# 4    ozone    18
# 5    ozone    NA
# 6    ozone    28

我的问题是如何获得原版?原始的将对应于

My question is how can I get the original ones? The original ones would correspond to

#   ozone solar.r wind temp 
# 1    41     190  7.4   67 
# 2    36     118  8.0   72 
# 3    12     149 12.6   74
# 4    18     313 11.5   62 
# 5    NA      NA 14.3   56
# 6    28      NA 14.9   66

推荐答案

另一个选项是unstack

out <- unstack(aql,value~variable)
head(out)
#   ozone solar.r wind temp month day
#1    41     190  7.4   67     5   1
#2    36     118  8.0   72     5   2
#3    12     149 12.6   74     5   3
#4    18     313 11.5   62     5   4
#5    NA      NA 14.3   56     5   5
#6    28      NA 14.9   66     5   6

由于问题是关于dcast,我们可以创建一个序列列,然后使用dcast

As the question is about dcast, we can create a sequence column and then use dcast

aql$indx <- with(aql, ave(seq_along(variable), variable, FUN=seq_along))
out1 <- dcast(aql, indx~variable, value.var='value')[,-1]
head(out1)
#   ozone solar.r wind temp month day
#1    41     190  7.4   67     5   1
#2    36     118  8.0   72     5   2
#3    12     149 12.6   74     5   3
#4    18     313 11.5   62     5   4
#5    NA      NA 14.3   56     5   5
#6    28      NA 14.9   66     5   6

如果您使用的是 data.table,则为 data.table 的开发版本,即.v1.9.5 也有dcast 功能.安装开发版的说明是这里

If you are using data.table, the devel version of data.table ie. v1.9.5 also has dcast function. Instructions to install the devel version are here

 library(data.table)#v1.9.5+
 setDT(aql)[, indx:=1:.N, variable]
 dcast(aql, indx~variable, value.var='value')[,-1]

这篇关于没有 ID 变量的 dcast的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆