-3

The data structure I'm looking at is:

head(data)
  ID Gender      Location   Generation                           Question Response
1  2   Male South America Generation X Q0. Vote in the upcoming election?    0: No
2  2   Male South America Generation X                     Q1. Pulse Rate    0: No
3  2   Male South America Generation X                     Q2. Metabolism    0: No
4  2   Male South America Generation X                  Q3.Blood Pressure   1: Yes
5  2   Male South America Generation X                    Q4. Temperature    0: No
6  2   Male South America Generation X         Q5. Galvanic Skin Response   1: Yes

The column headers in this data frame are as follows:

> colnames(data)
[1] "ID"         "Gender"     "Location"   "Generation" "Question"   "Response" 

The Question as the header contains the questions asked and so does the Responses. How I'd want to look at it is:

> colnames(final_data)
 [1] "ID"                                 "Gender"                            
 [3] "Location"                           "Generation"                        
 [5] "Q0. Vote in the upcoming election?" "Q1. Pulse Rate"                    
 [7] "Q134. Good Job Skills"              "Q135. Sense of Humor"              
 [9] "Q136. Intelligence"                 "Q137.Can Play Jazz"                
[11] "Q138.Likes the Beatles"             "Q139. Snobbiness"                  
[13] "Q140.Ability to lift heavy objects" "Q141.Grace under pressure"         
[15] "Q142.Grace on the dance floor"      "Q143.Likes animals"                
[17] "Q144.Makes good coffee"             "Q145.Eats all his/her vegetables"  
[19] "Q2. Metabolism"                     "Q3.Blood Pressure"                 
[21] "Q4. Temperature"                    "Q5. Galvanic Skin Response"        
[23] "Q6. Breathing"                      "Q7. Perspiration"                  
[25] "Q8.Pupil Dilation"                  "Q9. Adrenaline Production" 

Currently I have the data which records the attributes for each ID in a single row. Essentially it means that each row has attributes for only one unique ID.

I saw another question here but failed to understand it. Can anyone please help?

Community
  • 1
  • 1
Ayan
  • 145
  • 2
  • 16
  • 1
    I fail to see the question. What are you trying to achieve? What is the desired result? Do you have a reproducible example? – Roman Luštrik Mar 11 '15 at 13:01

1 Answers1

0

I didnt get how do you actually want the data to look finally. My guess is that you want the data to have ID,Gender, Location,Generation as first 4 columns and then transform the questions into column names with their answers as values under those respective columns. To do that you can simply use melt and dcast functions in reshape2 package in R

x=melt(data,id=c("ID","Gender","Location","Generation"))
#this will melt the data frame telling R that these 4 variables are your primary keys
final_data=dcast(x, ID + Gender + Location + Generation ~ Question, value.var="Response")

I think this will do the trick

Ayan
  • 145
  • 2
  • 16
Aayush Agrawal
  • 184
  • 1
  • 6
  • Thanks a ton, i wanted to up-vote (clicked on the downwards arrow unintentionally). Now i dont have enough points to change. Thanks a ton Aayush, I will remember this trick. Really helped – Ayan Mar 12 '15 at 06:43
  • Just curious...from this stage, how can I get the data to look like how it was? – Ayan Mar 12 '15 at 06:47
  • You will again have to melt this new data and give id=c("ID","Gender","Location","Generation") it will revert the data to how it was – Aayush Agrawal Mar 12 '15 at 11:08