Rearranging a dataset in R

#1
Hello,

So I currently have a dataset that is like this:

structure(list(FID = structure(1:10, .Label = c("A", "B", "C",
"D", "E", "F", "G", "H", "I", "J"), class = "factor"), Treatment = c(1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), Territorial = structure(c(2L,
1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L), .Label = c("NT", "T"), class = "factor"),
Color = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L
), .Label = c("b", "y"), class = "factor"), Response1a = c(4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), Response2a = c(5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), Response3a = c(4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L), Response4a = c(4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L), Response5a = c(4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L), Response1b = c(10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L), Response2b = c(11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L), Response3b = c(12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L), Response4b = c(10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L), Response5b = c(12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L)), .Names = c("FID",
"Treatment", "Territorial", "Color", "Response1a", "Response2a",
"Response3a", "Response4a", "Response5a", "Response1b", "Response2b",
"Response3b", "Response4b", "Response5b"), class = "data.frame", row.names = c(NA,
-10L))


Here there are a string of responses measured in 10 different columns... I would like all responses to be in one column and keep the remaining information while adding the response number and type (weather A or B).

The column headings would be in a structure like this:

structure(list(FID = structure(1L, .Label = "A", class = "factor"),
Treatment = 1L, Territorial = TRUE, Color = structure(1L, .Label = "b", class = "factor"),
Response.Number = 1L, A.or.B = structure(1L, .Label = "a", class = "factor"),
Response.Value = 4L), .Names = c("FID", "Treatment", "Territorial",
"Color", "Response.Number", "A.or.B", "Response.Value"), class = "data.frame", row.names = c(NA,
-1L))

Is that clear? Thanks for any suggestions
 

bryangoodrich

Probably A Mammal
#2
The first 4 columns are factors and the other 10 are measures you want to collapse? In base R I'd look at stack, but it'll be a lot easier to use melt in the Reshape2 package I believe it is in.

But looking at your example output, I have no idea what you just did to collapse the data like that.

Looking at it again, you might want to use the reshape function in base R. It's not easy to use, but it has a few ways to tweak your wide data format into a long data format like you want while cutting up the column names. It is designed for longitudinal data reshaping, which is where the parameter names come from, but I'm sure plyr or Reshape2 packages have easier ways to do this stuff (I don't use them, so I don't know).
 

trinker

ggplot2orBust
#3
When you're posting code, dataframes or computer output it's helpful to wrap this information in code tags by:
  1. either clicking the pound (#) sign icon or
  2. wrap with [NOPARSE]
    Code:
    some code
    [/NOPARSE]

which produces:
Code:
some code
For more see this (LINK)
 
#4
Ok.. Will use code brackets..

I am playing around with reshape now.. Thanks

Basically.. I have 10 columns of responses that I was to put all into one column while keeping the information in the rows consistent... I do not really need to splice the column names as I asked above, I can do without that.
 
#5
Ok

I did this..

Code:
a<-structure(list(FID = structure(1:10, .Label = c("A", "B", "C", 
"D", "E", "F", "G", "H", "I", "J"), class = "factor"), Treatment = c(1L, 
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), Territorial = structure(c(2L, 
1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L), .Label = c("NT", "T"), class = "factor"), 
Color = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L
), .Label = c("b", "y"), class = "factor"), Response1a = c(4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), Response2a = c(5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), Response3a = c(4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L), Response4a = c(4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L), Response5a = c(4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L), Response1b = c(10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L), Response2b = c(11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L), Response3b = c(12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L), Response4b = c(10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L), Response5b = c(12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L)), .Names = c("FID", 
"Treatment", "Territorial", "Color", "Response1a", "Response2a", 
"Response3a", "Response4a", "Response5a", "Response1b", "Response2b", 
"Response3b", "Response4b", "Response5b"), class = "data.frame", row.names = c(NA, 
-10L))

Then,
Code:
newa<-melt(a, id=c('FID', 'Treatment', 'Territorial', 'Color'), measure=c('Response1a', 'Response2a', 'Response3a', 'Response4a', 'Response5a', 'Response1b', 'Response2b', 'Response3b', 'Response4b', 'Response5b'))
and I get what I was looking for..

Thanks for the 'Reshape' suggestion