Converting observational data stored in a 2D data frame to a 3D array in R

Converting observational data stored in a 2D data frame to a 3D array in R



I am trying to produce a 3D array from a 2D data frame in R and could really use some help. So far, I haven't found a solution to this problem from similar questions that have been posted previously.



My input data are available here: https://www.dropbox.com/s/7f8td34mpzgpvgh/example_data.csv?dl=0, and they resemble the following basic structure: I have 14 sites (i.e. Field) with 6 replicates each (i.e. Replicate) in which a subset of 32 subjects (i.e. columns of species codes: AMGO, BASW, etc.) were counted when present in a survey.


Field


Replicate


AMGO


BASW



A subset of the input data looks like so:


example_data[1:5, 1:5]

Field Replicate AMGO BASW BHCO
1 Brinkman 1 2 0 0
72 Brinkman 2 10 0 0
190 Brinkman 3 6 0 0
283 Brinkman 4 0 0 0
342 Brinkman 5 2 1 0



I'd like to reformat these input data to resemble a 3D array (i.e. 14 sites x 6 replicates x 32 subjects), as exemplified below with the AMGO species:


AMGO


, , = AMGO

1 2 3 4 5 6
Brinkman 0 0 0 0 0 0
Clara 0 0 0 0 0 0
Esckelson 0 0 0 0 0 0
GarnerEast 0 0 0 0 0 0
GarnerWest 0 0 0 0 0 0
KHess 0 0 0 0 0 0
Lounsbury 0 0 0 0 0 0
McCallum 0 0 0 0 0 0
Pomeroy 0 0 0 0 0 0
Sattelberg 0 0 0 0 0 0
THess 0 0 0 0 0 0
Turner 0 0 0 0 0 0
VollmarEast 0 0 0 0 0 0
VollmarWest 0 0 0 0 0 0

...



Note that, in the solution, many of the zeroes above would likely be replaced by non-zero counts when AMGO (and other species) was (were) actually encountered during a survey.


AMGO



Please let me know if there's anything I need to clarify, and thanks in advance!




1 Answer
1



Here's a solution using the reshape() function from base R. I'm applying the function to each of the subject columns and creating a list of reshaped dataframes.


reshape()


df <- read.csv("C:\Users\Shrivatav\Downloads\example_data.csv", encoding = "UTF-8")

# Extract subject columns
list.of.cols <- colnames(df)[3:34]

# Function for reshaping
func.for.reshaping <- function(column)

# Subset the data, keep only Field, replicate and the column input in the
# function
to.keep <- c("Field", "Replicate", column)
subset.df <- df[to.keep]
# reshape from long to wide
reshaped.df <- reshape(subset.df, idvar = "Field", timevar = "Replicate", direction = "wide")
return(reshaped.df)


# Apply the function over all subject columns, reulting
# in a list of dataframes
list.of.reshaped.dfs <- lapply(list.of.cols, func.for.reshaping)

# Name the list for easy access
names(list.of.reshaped.dfs) <- list.of.cols



You can access the elements of the list like: list.of.reshaped.dfs$AMGO and so forth.


list.of.reshaped.dfs$AMGO






Thank you, @Vishesh Shrivastav! This solution is very helpful, and I sincerely appreciate your time and expertise.

– AD_R
Sep 16 '18 at 2:22



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)