简体   繁体   English

使用多个解释变量从长到长重塑数据

[英]Reshape data from long to wide with multiple explained variables

I'm trying to convert my data frame format using R. I want unique company name as it has multiple observations for each company. 我正在尝试使用R转换我的数据框格式。我想要唯一的公司名称,因为它对每个公司都有多个观察结果。 My data looks like 我的数据看起来像

company name    Values  Year
    A              1    2010
    A              2    2011
    B              4    2010
    B              6    2012
    C              8    2011

I want below format 我想要下面的格式

 company name   first_value First_year  second_values second_year
     A              1          2010          2           2011
     B              4          2010          6           2012  
     C              8          2011          NA           NA

I have tried this code but it is not giving result what I am expecting 我已经尝试过这段代码,但它没有给出我期待的结果

library(plyr)
extract.hashtags <- function(x) {
x <- subset(x,select=c(-Company.Name))
mat <- as.matrix(x)
dim(mat) <- c(1,length(mat))
as.data.frame(mat)
}

df1 = ddply(data, .(Company.Name), extract.hashtags )

You can use reshape in base R after you add a "time" variable, which can be done with getanID from my "splitstackshape" package: 您可以使用reshape的基础R你添加一个“时间”变量,这是可以做到的后getanID从我的“splitstackshape”套餐:

reshape(getanID(mydf, "companyname"), idvar = "companyname", 
          timevar = ".id", direction = "wide")
#    companyname Values.1 Year.1 Values.2 Year.2
# 1:           A        1   2010        2   2011
# 2:           B        4   2010        6   2012
# 3:           C        8   2011       NA     NA

A similar solution using the devel version of data.table (v 1.9.5+) 使用devel版本的data.table (v 1.9.5+)的类似解决方案

library(data.table) ## v 1.9.5+
dcast(setDT(df)[, indx := 1:.N, by = company_name], 
      company_name ~ indx, value.var = c("Values", "Year"))

# c   ompany_name Values_1 Values_2 Year_1 Year_2
# 1:            A        1        2   2010   2011
# 2:            B        4        6   2010   2012
# 3:            C        8       NA   2011     NA

The idea is to add a counter per group and then reshape from long to wide according to that counter while specifying two variables as the explained vars simultaneously (currently available only in the devel version). 这个想法是为每个组添加一个计数器,然后根据该计数器从长到大重新整形,同时将两个变量同时指定为解释的变量(目前仅在devel版本中可用)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM