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
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
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