MomX / Momocs

:dove: Morphometrics using R
http://momx.github.io/Momocs/
51 stars 18 forks source link

Delete elements from a CHC file and efourier problem #176

Closed raz1 closed 7 years ago

raz1 commented 7 years ago

I wish to delete some elements from a CHC file like in the script below but I get this message "Error: the number of rows in $fac must equal the number of shapes"

Sample_chc.zip

How can I remove elements and still do the efourier? myOut<-chc2Out("C:/Users/Dzzer/Desktop/shape/Sample_chc.chc", skip=5) panel(myOut) ef <- efourier(myOut, 12) myOut1<-myOut myOut1[2]<-NULL myOut1[5]<-NULL panel(myOut1) ef <- efourier(myOut1, 12)

Furthermore, how can I add or change the fac?

vbonhomme commented 7 years ago

Hi there. This is quite exotic to me but here is a quick and dirty fix. The "problem" was that chc2pic (on which chc2Out relies) expect only chc numeric (no covariables before, and no -1 in the end)

x <- readLines("~/Desktop/Sample_chc.chc")

# turns one of your chc line into a list with covariables and coordinates
line2chc <- function(x){
  x <- strsplit(x, " ")[[1]]
  coo <- x[-c(1:5, length(x))] %>% as.numeric() %>% chc2pix()
  fac <- x[1:5]
  return(list(coo=coo, fac=fac))
}

# we apply this function to all lines
y <- lapply(x, line2chc)

# we extract only coordinates
coo <- lapply(y, function(.) .$coo)
# and now covariables with some dirty and ungeneric approach
fac <- lapply(y, function(.) .$fac) %>% do.call("rbind", .) %>% data.frame(stringsAsFactors=F)
fac[, 1] %<>% as.factor()
fac[, 2] %<>% as.numeric()
fac[, 3] %<>% as.numeric()
fac[, 4] %<>% as.numeric()
fac[, 5] %<>% as.numeric()
names(fac) <- c("ind", "var1", "var2", "var3", "var4")

# here is your working fac
Out(coo, fac)
# eg
Out(coo, fac) %>% efourier %>% PCA %>% plot("var4", cex=1)

good luck