I have a large .csv file containing the results of recent large-scale forest surveys, in which each row contains a given individual tree's location, species identity, and measured cross-sectional area. I read this .csv into RStudio using fread() to produce a data.table. I want to collapse this large data.table into a matrix such that each row corresponds with a location, each column corresponds with a single species, and each cell contains the sum of all cross-sectional areas of that species at that location.
Below is a dummy data.table in the format of my data, as copied from the console. Values in cells are summed values from column x-sect area in raw.input.
> raw.input <- fread("raw_input.csv")
> raw.input
site sp x-sect area
1: hilltop sp2 10
2: hilltop sp1 3
3: hilltop sp1 5
4: hilltop sp1 4
5: hilltop sp1 3
6: stream sp3 45
7: stream sp3 50
8: stream sp1 4
Below is a matrix in my desired format, generated as a .csv is MS Excel, read in using fread(), and converted to a matrix in RStudio.
> mtrx.tmp <- fread("mtrx_final.csv")
> mtrx <- as.matrix(mtrx.tmp[,2:4]) #remove character strings so matrix is numeric
> row.names(mtrx) <- mtrx.tmp$site #mtrx.tmp$site is equivalent to mtrx.tmp[,1] in content
> mtrx
sp1 sp2 sp3
hilltop 15 10 0
stream 4 0 95
If a data.table is an inappropriate/inefficient format in which to read in this data set please do include that in your answer.