79605

Using tidyr to combine multiple columns [duplicate]

Question:

This question already has an answer here:

<ul><li> <a href="/questions/12466493/reshaping-multiple-sets-of-measurement-columns-wide-format-into-single-columns" dir="ltr" rel="nofollow">Reshaping multiple sets of measurement columns (wide format) into single columns (long format)</a> <span class="question-originals-answer-count"> 6 answers </span> </li> </ul>

The data I am working at looks like this:

Cust no. Chg1 Chg2 Chg3 Chg4 Ctot1 Ctot2 Ctot3 Ctot4 Size Name 61 A B C D 2 4 5 6 2 XA

The desired output is:

Cust no. Chg Ctot Size Name 61 A 2 2 XA 61 B 3 2 XA 61 C 4 2 XA 61 D 5 2 XA

Using gather(key,value,Ctot1:Ctot4), the output is

Cust no. Chg Key Value Size Name 61 A Ctot1 2 2 XA 61 A Ctot2 3 2 XA 61 A Ctot3 4 2 XA 61 A Ctot4 5 2 XA

Some advise on this will be really helpful.

Answer1:

We can use melt from data.table for this purpose as it can take multiple measure patterns

library(data.table) melt(setDT(df1), measure = patterns("^Chg", "^Ctot"), value.name = c("Chg", "Ctot"))[, variable := NULL][] # Custno. Size Name Chg Ctot #1: 61 2 XA A 2 #2: 61 2 XA B 4 #3: 61 2 XA C 5 #4: 61 2 XA D 6

Recommend

  • After searching in a database how to display the result field values in an editor widget using progr
  • get the column name during runtime with entityframework
  • Generate c# object code and assign values to its properties from an xml document
  • How to keep date from client saved in MongoDB as date?
  • Load 24 bit TGA
  • Distribute Range of Numbers between each threads
  • How do I create an image and save it for later to draw as texture in XNA?
  • Imageloader not loading image on real device
  • Encrypting credit card details using AngularJS in Braintree
  • What is RSL (Runtime shared library ) used for in flash?
  • Unicorn and Rails eat up 2x MySQL connections
  • After getting Cell Info, EXC_BAD_ACCESS is thrown
  • How to create a custom Powershell operator?
  • How to save dynamically created textboxes and their values
  • Formatting a text in a table cell with PHPWord e.g. bold, font, size e.t.c
  • Accessing another variable in the same class with a click event
  • Galaxy S4 Android Custom Camera Issue
  • F#: In which memory area is the continuation stored: stack or heap?
  • Deleting empty Series out of Graph (with VBA)
  • Does CSS support inheritance [duplicate]
  • Xaml, wpf image position and crop issue
  • Inversing an interpolation of rotation
  • C# - Most efficient way to iterate through multiple arrays/list
  • HttpClient: disabling chunked encoding
  • Display validation errors inside
  • Floated image with variable width and heading with background image
  • Extract zip entries to another Zip file
  • Using a canvas object in a thread to do simple animations - Java
  • GridView breaks while scrolling
  • How to change the font size of a single index for UISegmentedControl?
  • Time complexity of a program which involves multiple variables
  • Atlas images wrong size on iPad iOS 9
  • Change multiple background-images with jQuery
  • Android screen density dpi vs ppi
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Change an a tag attribute in JavaScript based on screen width
  • Matrix multiplication with MKL
  • Hits per day in Google Big Query
  • File not found error Google Drive API
  • Converting MP3 duration time