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

is.numeric(mydata)

[1] 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:

as.matrix(sapply(mydata[], as.numeric))

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.

is.numeric(shape)

[1] TRUE # now it’s numeric. Ready to go!

Simple!

Emma

*Related*

To

**leave a comment** for the author, please follow the link and comment on their blog:

** geomorph**.

R-bloggers.com offers

**daily e-mail updates** about

R news and

tutorials on topics such as:

Data science,

Big Data, R jobs, visualization (

ggplot2,

Boxplots,

maps,

animation), programming (

RStudio,

Sweave,

LaTeX,

SQL,

Eclipse,

git,

hadoop,

Web Scraping) statistics (

regression,

PCA,

time series,

trading) and more...

If you got this far, why not

__subscribe for updates__ from the site? Choose your flavor:

e-mail,

twitter,

RSS, or

facebook...