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

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

问题描述

在reshape2简介包Sean C. Anderson提供以下示例。



他使用空间数据并重命名列名

 名称(空气质量)< -  tolower(名称(空气质量))

数据类似

 #ozone solar.r风温月
#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 ))

获取

 #月日变量值
#1 5 1臭氧41
#2 5 2臭氧36
#3 5 3臭氧12
#4 5 4臭氧18
#5 5 5臭氧NA
#6 5 6臭氧28

最后,他通过

获得原始的(不同的列顺序)

  aqw <变量)



我的quesiton



我们没有ID变量(即月和日),并按如下方式融化数据

  aql<  -  melt(airquality)



看起来像

 变量值
#1臭氧41
#2臭氧36
#3臭氧12
#4臭氧18
#5臭氧NA
#6臭氧28

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

 #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 b#ozone solar.r风温月
#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

  aql $ indx < -  with(aql,ave(seq_along(variable),variable,FUN = seq_along ))
out1 < - dcast(aql,indx〜variable,value.var ='value')[, - 1]
head(out1)
#月日
#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 的devel版本。 v1.9.5 也有 dcast 函数。安装devel版本的说明为 此处

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


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))

The data look like

#   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

Then he melts them by

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

to get

#   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)

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)

which look like

#   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

解决方案

Another option is 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

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

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]

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

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