简体   繁体   English

枢转R Statfile的多列

[英]Pivot Multiple Columns of R Statfile

I have an R dataframe which has 120,000 rows and 34 columns. 我有一个具有120,000行和34列的R数据框。 I wish to pivot 3 of these columns, but maintain all of the other columns in that dataframe. 我希望透视其中的3列,但要保留该数据框中的所有其他列。

Take the example of the below record (fictional), unfortunately I can't paste an image or excel workbook. 以下面的记录为例(虚构),很遗憾,我无法粘贴图像或excel工作簿。

Initial  Initial Code  Renewal Renewal Code Other Other Code  Date    Consultant 
400      52070/1       200     52080/2      250   52090/1     1-1-18  Bill

Is there a way where I can pivot the 3 code columns ie Initial Code, Renewal code, Other Code, but still include all the remaining columns. 有没有一种方法可以透视3个代码列,即“初始代码”,“续订代码”,“其他代码”,但仍然包括所有其余列。 Basically so that it would look like the following: 基本上使它看起来像以下内容:

Initial Code      Renewal Other Date    Consultant
400     52070/1   200     250   1-1-18  Bill
400     52080/1   200     250   1-1-18  Bill
400     52090/1   200     250   1-1-18  Bill

Or better yet: 或者更好:

Amount Code      Date     Consultant  Type
400    52070/1   1-1-18   Bill        Initial
200    52080/1   1-1-18   Bill        Renewal
250    52090/1   1-1-18   Bill        Other

I appreciate the bottom one is essentially two layers of transformation, and the first suggestion isn't perfect, but it would be a workable layout for me. 我很欣赏最底层的转换本质上是两层转换,第一个建议并不完美,但是对我来说这是一个可行的布局。

Unfortunately I can't use Excel as a workaround here. 不幸的是,我不能在这里使用Excel作为解决方法。

Many thanks for any help that may come forward, Eoghan 非常感谢您的帮助,Eoghan

Can you try something like this? 你可以尝试这样的事情吗?

df %>%
  select(-grep("Code", names(df))) %>%
  gather(Type, Amount, -Date, -Consultant) %>%
  inner_join(df %>%
              select(-Initial, -Renewal, -Other) %>%
              gather(Type, Code, -Date, -Consultant) %>%
              mutate(Type=gsub(".Code","",Type)))

Output is: 输出为:

    Date Consultant    Type Amount    Code
1 1-1-18       Bill Initial    400 52070/1
2 1-1-18       Bill Renewal    200 52080/2
3 1-1-18       Bill   Other    250 52090/1


#sample data
df <- structure(list(Initial = 400L, Initial.Code = "52070/1", Renewal = 200L, 
    Renewal.Code = "52080/2", Other = 250L, Other.Code = "52090/1", 
    Date = "1-1-18", Consultant = "Bill"), .Names = c("Initial", 
"Initial.Code", "Renewal", "Renewal.Code", "Other", "Other.Code", 
"Date", "Consultant"), class = "data.frame", row.names = c(NA, 
-1L))

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

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