How to subset a geomorph data frame by a factor?

928 views
Skip to first unread message

Alexandre Palaoro

unread,
Aug 4, 2016, 2:52:18 PM8/4/16
to geomorph R package
Hello all,

After performing the GPA analysis, I made a geomorph data frame with the GPA output, a species factor (three levels) and a sex factor (two levels).
However, I want to subset that geomorph data frame by a factor and then perform further analysis only on that subset. For instance, I want to test the allometric component of shape and CS only on the males of the three species.

I have been trying to use the lapply function, but all further analysis do not work in the subsetted list. Apparently, the coordinates and Csize lists lose information in the process. 
I have tried to use arrayspecs, but I also got an error.

How can I proceed?

Thanks in advance for the help!

Code and errors below:

> gdf <- geomorph.data.frame(Y.gpa, species = mytable$species, sex = mytable$sex)

> males
<- lapply(gdf, "[", gdf$sex == "male")

> plotTangentSpace(males$coords,groups=males$species)
Error in plotTangentSpace(males$coords, groups = males$species) : 
  Data matrix not a 3D array (see 'arrayspecs').

> arrayspecs(males,17,262)
Error in arrayspecs(males, 17, 262) : 
  Matrix dimensions do not match input

> dim(Y.gpa$coords)
[1]  17   2 262
> dim(males$coords)
NULL


> str(Y.gpa$coords)
 num [1:17, 1:2, 1:262] -0.3102 -0.2515 -0.1485 -0.048 0.0746 ...
 - attr(*, "dimnames")=List of 3
  ..$ : NULL
  ..$ : NULL
  ..$ : chr [1:262] "0 " "1 " "2 " "3 " ...
> str(Y.gpa$Csize)
 Named num [1:262] 404 546 573 485 392 ...
 - attr(*, "names")= chr [1:262] "0 " "1 " "2 " "3 " ...

> str(males$coords)
 num [1:4556] -0.3102 -0.2515 -0.1485 -0.048 0.0746 ...
> str(males$Csize)
 Named num [1:134] 404 546 573 485 392 ...
 - attr(*, "names")= chr [1:134] "0 " "1 " "2 " "3 " ...




Michael Collyer

unread,
Aug 4, 2016, 3:10:03 PM8/4/16
to Alexandre Palaoro, geomorph R package
Alexandre,

There is not a good way to subset a geomorph data frame because, unlike general data frames, the geomorph data frame cannot be coerced into a matrix where rows can be removed.  The geomorph data frame (like any data frame) is a list that is constrained to have the same number of specimens, though the dimensions of any part of that list are not constrained to be n x 1 vectors, like in a regular data frame.

I think the only solution is to subset the separate parts and reconstitute a new data frame

E.g., 

coords.M <- gdf$coords[ , , gdf$sex == “male"]
Csize.M <- gdf$coords[gdf$sex == “male"]
species.M <- gdf$species[gdf$sex == “male"]

gdf.M <- geomorph.data.frame(coords = coords.M, Csize = Csize.M, species = species.M)

Of course this can all be done with one line of code by substituting the first three lines within the parentheses of the last line.  You can see why there is not a unique subset solution.

Alternatively, you can use a general data frame, and coerce coordinates into a matrix (for some but not all analyses)

E.g.,

df <- data.frame(Y = two.d.array(Y.gpa$coords), Csize = Y.gpa$Csize, species = mytable$species, sex = mytable$sex)
df2 <- df[df$sex == “male”, ]

This approach does not take advantage of the built-in decomposing of Y.gpa into Csize and coords parts like geomorph.data.frame.  Thus, either way, you are doing some maneuvering.

Cheers!
Mike


--
You received this message because you are subscribed to the Google Groups "geomorph R package" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geomorph-r-pack...@googlegroups.com.
To post to this group, send email to geomorph-...@googlegroups.com.
Visit this group at https://groups.google.com/group/geomorph-r-package.
To view this discussion on the web, visit https://groups.google.com/d/msgid/geomorph-r-package/02068c34-dd0a-42b5-9443-e98f4f8f8ac5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alexandre Palaoro

unread,
Aug 4, 2016, 8:11:04 PM8/4/16
to Michael Collyer, geomorph R package
Hello Michael,

The first example worked like a charm!
Thank you once again for the quick and helpful answer!

Cheers!
Alexandre
Alexandre Varaschin Palaoro
Núcleo de Estudos em Biodiversidade Aquática - Universidade Federal de Santa Maria
Reply all
Reply to author
Forward
0 new messages