Introduction
This article describes how to unstack a data set in Displayr using R. Unstacking can also be described as going from long to wide format like below:
Requirements
- A Displayr document.
- A stacked SPSS (.sav) data set saved as a URL.
Method
1. Go to Data Sets > Plus (+) > R.
2. Enter a name for the data set under Name.
3. Update and input the below R code where it states "Enter your R code here":
#load the foreign package which reads .sav files into R
library(foreign)
#read in the file and convert it to a data frame in R for easy handling
tech = suppressWarnings(read.spss("https://wiki.q-researchsoftware.com/images/c/cf/Stacked_Technology.sav",
use.value.labels = TRUE,
to.data.frame = TRUE))
#load reshape2 package which has functions to stack/unstack data
library(reshape2)
#make the data wide
reshape(tech, #the data set to reshape
idvar = "original_case", #variable(s) that should remain in their current columns
timevar = "observation", #variable(s) that original data was stacked by unstacking will make a new column for each
v.names = c("Q4A","Q4B","Q4C","Q4D","Q4E","Q4F","Q4G","Q4H","Q4I"), #variables with the data to unstack into columns
direction = "wide") #whether to make the data long or wide
Here, we use the foreign R package to import the stacked SPSS data set. We next use the reshape function to create a non-stacked data frame.
- The idvar argument references the original record id which will be the same for each of the stacked observations for that record.
- The timevar argument references the variable which stores the item that the data set was originally stacked by, i.e. the brand in this example.
- The v.names argument identifies the variables to unstack.
- The direction argument tells the function that we want to unstack the data.
4. OPTIONAL: If you are unsure of the exact code that you need to use to unstack your data set you can prototype the code in a Calculation by using Calculation > Custom Code and typing in your R CODE. The Calculation will allow you to preview the results and modify your code as you go. You can then use that same code to add your R data set.
5. R doesn’t have the same level of metadata as some file types, like SSS and SAV. For example, variables in an R data frame do not have the concept of both Name and Label. Remember to add such information after you have added your data set.
See Also
Comments
0 comments
Article is closed for comments.