cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
Choose Language Hide Translation Bar
jswislar
Level III

How do I Transpose data from Long to Wide format?

I'm a new JMP user.

My data is currently in long format. In other words, subjects have multiple lines of data each. However, I need the data to be in wide format for survival analysis - 1 line of data per subject. When I try to use Transpose, JMP tells me "Cannot transpose. Data columns have different data types". The variables are a mix of continiuous and nominal. So, how am I supposed to do this? I was able to convert a different data set from wide to long without difficulty, but can't seem to do the reverse.

 

Current data is set up like this (fake data):

 

ID     Var1     Var2    Outcome

001   1          1          0

001   1          1          0

001   1          0          1

002   0          1           0

002   0          1           1

003   1          1            0

 

I want it like this:

ID     Var1_time1  Var2_time1 Var1_time2 Var2_time2.... Outcome_timex

001   1                  1                  1                  1                     1

002   0                  1                  0                  1                      1

003   1                  1                  .                    .                      0       

 

Thanks!                   

15 REPLIES 15
jswislar
Level III

Re: How do I Transpose data from Long to Wide format?

This code gave me 2 tables. The first was called "Source" and only had 10 rows of data and 5 columns. The ID column goes from 1-4 (not my IDs) and ID appears on 3 rows. I don't really know what this was supposed to do. All the data are binary, even my date vars. The second table "untitled" had 4 rows of data and 4 columns. ID goes from 1-4 down the rows and the column are all labeled as missing.

 

I don't think this is doing what I want.

ron_horne
Super User (Alumni)

Re: How do I Transpose data from Long to Wide format?

Last chance,

1) the first command just makes an original data source in long format.

 

2) the second command introduces an index for the number of the event for each ID. the number of events does not need to be equal for each ID. but, if some ID's have less events they will have missing values in the wide table format.

 

3) the third command takes advantage of the index and the ID columns in order to split the table the way you asked for it. one column for each event and variable and each row represents just one ID.

 

on your data you just need to run the last two command.

 

 

names default to here(1);
// source data table
dt = New Table( "Long",
	Add Rows( 7 ),
	New Column( "ID",
		Numeric,
		Continuous,
		Format( "Best", 5 ),
		Set Values( [1, 1, 1, 2, 2, 3, 3] )
	),
	New Column( "Var1",
		Numeric,
		Continuous,
		Format( "Best", 12 ),
		Set Values( [1, 1, 1, 0, 0, 1, 1] )
	),
	New Column( "Var2",
		Numeric,
		Continuous,
		Format( "Best", 12 ),
		Set Values( [1, 1, 0, 1, 1, 1, 0] )
	),
	New Column( "Outcome",
		Numeric,
		Continuous,
		Format( "Best", 12 ),
		Set Values( [0, 0, 1, 1, 1, 0, 0] )
	)
);


// add index column for spliting - no need to sort the table first
dt << new column ("index", formula(Sum( :ID[Index( 1, Row() )] == :ID ) ) );

// now split by index and group by ID
dt << Split(
	Split By( :index ),
	Split( :Var1, :Var2, :Outcome ),
	Group( :ID ),
	Output Table( "Wide" )
);

Jeff_Perkinson
Community Manager Community Manager

Re: How do I Transpose data from Long to Wide format?

@ron_horne has the right solution but perhaps some pictures will help.

 

Tables -> Split will rearrange the data the way you want but it requires a column that identifies the new columns. In your case you want to create columns for each row within ID so you need a column which counts rows within ID. @ron_horne called this column Index.

 

You'll also need to make sure that your data is sorted by ID for this to work.

 

Here's the data that you presented in your first post in the form of a JMP data table.

 

JMPScreenSnapz058.png

 

To this data we need add the Index column. @ron_horne used a different formula for his Index column but I like something a little more straightforward. 

JMPScreenSnapz059.png

The formula above is:

 

If( :ID != Lag( :ID, 1 ), 1, Lag( :Index, 1 ) + 1)

 

In prose this says that if the ID value in the current row isn't the same as the ID in the previous row (i.e. Lag ( :ID, 1)) then put a 1 in the Index column, otherwise take the value of the Index column from the previous row (i.e. Lag (:Index, 1))

 

As you can see, the Index column in the data table now counts the rows within ID.

JMPScreenSnapz060.png

Now we can use Tables->Split to split your Var1, Var2 and Outcome columns by Index and group the rows by ID.

 

JMPScreenSnapz061.png

That will give you this data table, which I think is what you want. It has new columns for each row within ID and one row per ID.

JMPScreenSnapz062.png

It might be easier to see a simpler example where we don't have the create the Split By column. Here's a video that shows that.

 

 

Let me know if I've missed the mark or you're still confused.

 

-Jeff
jswislar
Level III

Re: How do I Transpose data from Long to Wide format?

Thanks, Jeff! That worked!

Re: How do I Transpose data from Long to Wide format?

Hello Jeff,

 

Thank you for this explanation it was helpful but I have a quick question:

 

Current sorting: Each variable is listed with its corresponding event identifier.

Desired sorting: I want all variables for the first event to be grouped together, followed by all variables for the second event, and so on. Within each event group, I want the variables sorted by their names.

Original: var1_1 var1_2 var1_3 var2_1 var2_2 var2_3 var3_1 var3_2 var3_3
Desired: var1_1 var2_1 var3_1 var1_2 var2_2 var3_2 var1_3 var2_3 var3_3

Is there a way to do that ?

 

txnelson
Super User

Re: How do I Transpose data from Long to Wide format?

Other than writing a script to order the columns, I don't believe there is a built in way to interactively sort the columns in the order you want them to have.

Of course, you can manually move the columns.

Jim