dcast将所有变量重命名为以数字开头 [英] dcast renaming all variables to start with a number

查看:195
本文介绍了dcast将所有变量重命名为以数字开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我得到的数据如下:

So I've got data that looks like this:

           id year principal interest
 1: 011000600 2013      0.00     0.00
 2: 011000600 2014    544.03     0.00
 3: 011000700 2013      0.00     0.00
 4: 011000700 2014      0.01     0.00
 5: 011000800 2013    363.44    12.79
 6: 011000800 2014   2005.98     0.00
 7: 011000900 2013      0.00     0.00
 8: 011000900 2014      0.00     0.00
 9: 011001000 2013      0.00     0.00
10: 011001000 2014      0.00     0.00
11: 011001100 2013      0.00     0.00
12: 011001100 2014   1723.24     0.00
13: 011001560 2013      0.00     0.00
14: 011001560 2014      0.00     0.00
15: 011001650 2013      0.00     0.00
16: 011001650 2014      0.00     0.00

(基本上是一堆变量的纵向示例)

(basically a longitudinal sample of a bunch of variables)

数据在很大的一面,所以我使用 data.table 。我重塑它以获得每个 id 独特的行:

The data is on the large side so I'm using data.table for everything. I reshape it to get each id unique by row:

datam<-melt(data,id=c("id","year"))
data1<-dcast.data.table(datam,id~...)

这会产生:

          id 2013_principal 2013_interest 2014_principal 2014_interest
1: 011000600           0.00          0.00         544.03             0
2: 011000700           0.00          0.00           0.01             0
3: 011000800         363.44         12.79        2005.98             0
4: 011000900           0.00          0.00           0.00             0
5: 011001000           0.00          0.00           0.00             0
6: 011001100           0.00          0.00        1723.24             0

我想要的数据的任何建议如何处理这个?我更愿意:

Any suggestions for how to deal with this? I'd much rather have:

          id principal_2013 interest_2013 principal_2014 interest_2014
1: 011000600           0.00          0.00         544.03             0
2: 011000700           0.00          0.00           0.01             0
3: 011000800         363.44         12.79        2005.98             0
4: 011000900           0.00          0.00           0.00             0
5: 011001000           0.00          0.00           0.00             0
6: 011001100           0.00          0.00        1723.24             0

(将年更改为后缀)
我试图更多显示投放时,例如

(switching the year to be a suffix) I've tried being more explicit when casting, e.g.

data2<-dcast.data.table(datam,id~year+...)
data3<-dcast.data.table(datam,id~...+year)

无效:

data2
          id 2013_principal 2013_interest 2014_principal 2014_interest
1: 011000600           0.00          0.00         544.03             0
2: 011000700           0.00          0.00           0.01             0
3: 011000800         363.44         12.79        2005.98             0
4: 011000900           0.00          0.00           0.00             0
5: 011001000           0.00          0.00           0.00             0
6: 011001100           0.00          0.00        1723.24             0

data3
          id 2013_principal 2013_interest 2014_principal 2014_interest
1: 011000600           0.00          0.00         544.03             0
2: 011000700           0.00          0.00           0.01             0
3: 011000800         363.44         12.79        2005.98             0
4: 011000900           0.00          0.00           0.00             0
5: 011001000           0.00          0.00           0.00             0
6: 011001100           0.00          0.00        1723.24             0

看来dcast的命名约定默认为这种风格,假设我想象这种类型的重塑是无所不在的。

Seems pretty silly for the naming convention of dcast to default to this style, given that I imagine this type of reshaping is ubiquitous.

我也尝试过补丁的事后给一些其他的帖子,我发现(例如此处),但它运行速度非常慢(有〜 400个变量以在完整数据集中重命名)

I've also tried patching things up ex-post given some other posts I've found (e.g. here), but it runs unfathomably slow (there are ~400 variables to rename in the full data set)

names(data)<-ifelse(substr(names(data),1,2) %in% c("19","20"),    
                    paste(substr(names(data),6,nchar(data)),
                          substr(names(data),1,4),sep="_")   ,
                    names(copy))

(我试图找到以年 - 19xx或20xx开始的所有变量 - 并尝试交换开始和结束)

(I'm trying to find all the variables starting with years--19xx or 20xx--and trying to swap the beginning and end)

推荐答案

更好的是,在 dcast wiki / Installationrel =nofollow> v1.9.5 + ,我们可以同时投放多个列。

Even better, with the new developments to dcast in data.table from v1.9.5+, we can cast multiple columns simultaneously..

require(data.table) # v1.9.5+
dcast(dt, id ~ year, value.var = c("principal", "interest"))
#          id principal_2013 principal_2014 interest_2013 interest_2014
# 1: 11000600           0.00         544.03          0.00             0
# 2: 11000700           0.00           0.01          0.00             0
# 3: 11000800         363.44        2005.98         12.79             0
# 4: 11000900           0.00           0.00          0.00             0
# 5: 11001000           0.00           0.00          0.00             0
# 6: 11001100           0.00        1723.24          0.00             0
# 7: 11001560           0.00           0.00          0.00             0
# 8: 11001650           0.00           0.00          0.00             0

不必再有融化 > cast ,因此效率很高。

No more having to melt unnecessarily before to cast, therefore quite efficient.

这篇关于dcast将所有变量重命名为以数字开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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