array format

3 views
Skip to first unread message

james.ryan.white

unread,
Sep 25, 2012, 11:05:54 PM9/25/12
to tropi...@googlegroups.com
Hey all,

This isn't really a spatial question but more of a formatting question. I'm trying to get my data in an array format in order to run a three way parallel factor analysis using the package "PTAk". So far, I can get it converted to an array but it throws my values in the wrong spot when I double check the array. I think it has something to do with the unlist command. Here's my code so far:

#load necessary libraries
library(PTAk)

#import file
minor= read.csv(file.choose(), header=T)

#creates array
reduced= minor[3:10]
dataarray= array(data = unlist(reduced), dim = c(8,3,33))
dimnames(dataarray)= list(c("bite", "DM", "DV", "maxDV", "height", "boldness", "latency", "aggression"), c("F", "L", "S"), c("1", "3", "4", "5", "6", "14", "15", "16", "18", "20", "21", "22", "23", "24", "26", "27", "28", "71", "72", "74", "76", "77", "78", "79", "81", "82", "83", "84", "85", "86", "87", "88", "90"))

My data consists of 8 behavioral traits measured (3 repeated measures) for 33 individual fish (with unique fish ID numbers). The raw data file (.csv) is setup in your typical stacked format:

tank, fishid, bite, dm, dv, maxdv, height, boldness, latency, aggression
F,1,values...
L,1,values...
S,1,values...
F,3,etc.
L,3,etc.
S,3,etc.

Any ideas on how to get this data converted to an array correctly? I feel like it's some dumb little thing I'm missing.

Thanks,
James

Keith, Sally

unread,
Sep 25, 2012, 11:15:11 PM9/25/12
to tropi...@googlegroups.com

Try package reshape (i think now superseded by reshape2?) to create 3D array from long format data, see function “cast” e.g.,

 

cast(reduced,trait~trial~fish)

 

p.s. might have to ‘melt’ data first

--
An R group for questions, tips and tricks relevant to spatial ecology and climate change.
All R questions welcome.
---
You received this message because you are subscribed to the Google Groups "Tropical R" group.
To post to this group, send an email to tropi...@googlegroups.com.
To unsubscribe from this group, send email to tropical-r+...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msg/tropical-r/-/PeEOK9O9IqwJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

james.ryan.white

unread,
Sep 26, 2012, 12:06:06 AM9/26/12
to tropi...@googlegroups.com, sally...@jcu.edu.au
Sally Keith you are a sweet sweet angel. That worked like a charm and was super easy. I've been struggling with this for days.

I owe you a beer.

James
Reply all
Reply to author
Forward
0 new messages