Today’s exercise is another nice and simple one, and allows you to get used to manipulating datasets in R.
Exercise 4 – How to read a file of coordinate data into R and make sure it is numeric.
Reading your data files into R for analysis with geomorph or other packages can be challenging. Geomorph has several functions for common data file types of coordinate data (e.g. readland.tps(), readland.nts()).
But what if your data are not in this format?
One of the main issues people face is that using read.table() or read.delim(), numerical data can be coerced into a factor automatically. Below is a very simple solution to make sure your data are numerical:
mydata <- read.table(“data.txt”,header=TRUE,row.names=1,
stringsAsFactors = FALSE)
# here we are reading in a tab-delimited text file from MorphoJ, but this can be an issue with data from any outside program. The stringsAsFactors = FALSE is VERY important here
 FALSE # here R tells is the data are not numeric, even though we can see they are if we use View(mydata).
The solution is:
For example, say we know the shape coordinates are present in the file after two columns of non-shape coordinates (these could be centroid size, or a classifier), then:
shape <- as.matrix(sapply(mydata[,-(1:2)], as.numeric))
# here we say, use all columns except the first two.
 TRUE # now it’s numeric. Ready to go!