Extract contour vertices from a dataframe - r

Hi guys and thanks in advance for your help.
I have a three-column dataframe, two with coordinates for my data (x and y) and a value of brain activity (z). Out of 7505 rows there are many coordinates with null data that I need to exclude for my statistical analysis.
I'm using the package ImageSCC (https://rdrr.io/github/funstatpackages/ImageSCC/man/) so I need to extract the boundaries or contour of my data, meaning that I need a two-column list of the coordinates that separate brain activity data from null data. This is an example provided by the package:
$Brain.V1
V1 V2
[1,] 0.07781920 0.33867403
[2,] 0.07781920 0.56408840
[3,] 0.07781920 0.65469613
[4,] 0.07968313 0.43812155
[5,] 0.10950606 0.50000000
[6,] 0.11323392 0.25690608
[7,] 0.12068966 0.73425414
[8,] 0.16728798 0.18176796
[9,] 0.19897484 0.83812155
[10,] 0.23625349 0.10441989
[11,] 0.26663278 0.63322031
[12,] 0.28808616 0.46804766
[13,] 0.28939153 0.30397021
[14,] 0.30335508 0.91325967
[15,] 0.31081081 0.04033149
[16,] 0.37862235 0.75747676
[17,] 0.42193552 0.19377116
[18,] 0.42823858 0.02044199
[19,] 0.43196645 0.94419890
[20,] 0.44787179 0.57753089
[21,] 0.45363822 0.38319933
[22,] 0.54193849 0.02554144
[23,] 0.54231688 0.73945984
[24,] 0.55125815 0.92756906
[25,] 0.58275116 0.25043703
[26,] 0.59755088 0.47843551
[27,] 0.63513514 0.89662983
[28,] 0.64072693 0.07616022
[29,] 0.66888720 0.64103671
[30,] 0.71390307 0.35218048
[31,] 0.72087605 0.84138122
[32,] 0.75069897 0.15050829
[33,] 0.78631797 0.50829246
[34,] 0.79543336 0.77508287
[35,] 0.84016775 0.26132597
[36,] 0.85507922 0.69552486
[37,] 0.92845294 0.39287293
[38,] 0.92845294 0.60502762
I have tried with the package 'contoureR' but every time I run my code Rstudio crashes and reboots session. This is a sample of my code:
#install.packages("contoureR")
library(contoureR)
x = 1:ncol(df)
y = 1:nrow(df)
z = expand.grid(x=x,y=y)
z$z = apply(z,1,function(xx){df[xx[1],xx[2]]})
z$z[is.nan(z$z)] <- 0
cl = getContourLines(z)
Does anyone have other idea about how could I extract the boundaries of my data?
Thanks in advance.

Related

Optimum cut-off values

I have both univariate and multivariate logistic regression models and I want to find cut-off values with their respective sensitivity and specificity. I want to chose the best cut-off values for both my univariate and multivariate models.
I tried the following code for the univariate models but I am getting the sensitivity and specificity values in decimals. Is there any other way I can get the cut-off values as whole numbers rather than rounding up to the nearest integer? I am also not sure how to use the same code to get the cut-off values of the multivariate model.
Thank you for any help in advance!!
###Cut off values of Var1
library(pROC)
ok <- multiclass.roc(DATA$Outcome, DATA$Var1)
class_1 <- ok$rocs[[1]]
wants <- cbind(sensitivity = class_1$sensitivities, specificity = class_1$specificities, cutt_off = class_1$thresholds)
wants
I am getting the values:
sensitivity specificity cutt_off
[1,] 1.00000 0.000000 Inf
[2,] 1.00000 0.012346 73.500
[3,] 1.00000 0.024691 72.500
[4,] 1.00000 0.049383 71.500
[5,] 1.00000 0.061728 70.500
[6,] 1.00000 0.135802 69.500
[7,] 1.00000 0.172840 68.500
[8,] 0.94118 0.222222 67.500
[9,] 0.88235 0.283951 66.500
[10,] 0.88235 0.320988 65.750
[11,] 0.88235 0.333333 65.250
[12,] 0.88235 0.432099 64.500
[13,] 0.88235 0.506173 63.500
[14,] 0.82353 0.617284 62.500
[15,] 0.82353 0.629630 61.750
[16,] 0.76471 0.629630 61.250
[17,] 0.76471 0.691358 60.500
[18,] 0.70588 0.753086 59.750
[19,] 0.70588 0.777778 59.250
[20,] 0.70588 0.814815 58.500
[21,] 0.64706 0.827160 57.500
[22,] 0.64706 0.876543 56.500
[23,] 0.64706 0.901235 55.250
[24,] 0.58824 0.913580 54.250
[25,] 0.58824 0.938272 53.900
[26,] 0.52941 0.938272 53.400
[27,] 0.41176 0.938272 52.500
[28,] 0.35294 0.950617 51.835
[29,] 0.29412 0.950617 50.835
[30,] 0.29412 0.962963 49.000
[31,] 0.23529 0.975309 47.500
[32,] 0.17647 0.975309 46.000
[33,] 0.11765 0.987654 44.500
[34,] 0.00000 0.987654 42.500
[35,] 0.00000 1.000000 -Inf
To determine the cut-off values for the multivariate model, I tried the following code but I am getting errors. Also, my model comprises of both continuous and categorical values. Var1, Var2, Var3 are continuous variables and Var4 is categorical which was changed to 0, 1, & 2.
library(pROC)
ok <- multiclass.roc(DATA$Outcome, DATA$var1 + DATA$Var2 + DATA$Var3 + DATA$Var4)
class_1 <- ok$rocs[[1]]
wants <- cbind(sensitivity = class_1$sensitivities, specificity = class_1$specificities, cutt_off = class_1$thresholds)
wants

r igraph - how does plot() read the layout matrix?

My question is related to this one here, which unfortunately has not been responded. I'm trying to automatically annotate text next to highlighted communities on a plot. An intermediate step is to understand how nodes are placed on a plot.
G <- make_graph('zachary')
l <- layout_with_fr(G)
l
A layout is a matrix with rows representing nodes and columns representing the x and y plot parameters.
[,1] [,2]
[1,] 2.8510654 -2.2404898
[2,] 2.7183497 -1.1815130
[3,] 3.1429205 0.1117099
[4,] 1.5585372 -1.0743325
[5,] 2.2808632 -4.2035479
[6,] 2.1698198 -5.0526766
[7,] 1.4938068 -4.6975884
[8,] 1.9710816 -1.4672218
[9,] 3.5407035 0.5407852
[10,] 2.2222909 1.9079805
[11,] 3.0784642 -4.5828448
[12,] 4.4115351 -4.1057462
[13,] 0.6002378 -2.2432049
[14,] 2.5010525 -0.1563341
[15,] 4.8914673 4.1417759
[16,] 3.2053338 3.9212694
[17,] 1.1825200 -6.4099021
[18,] 3.7155897 -2.8354432
[19,] 3.8272351 4.2660906
[20,] 3.8636487 -0.5671906
[21,] 2.7302411 3.3998888
[22,] 1.6084374 -2.7407388
[23,] 4.3432855 3.8101278
[24,] 5.9392042 2.2364929
[25,] 6.9980077 0.2389222
[26,] 7.1608499 1.1360134
[27,] 6.0171481 4.0279067
[28,] 5.4996627 1.0367163
[29,] 4.4961257 0.9434659
[30,] 5.5987563 3.2314488
[31,] 2.9958404 1.2022317
[32,] 5.1188900 0.2919268
[33,] 4.1088296 2.5032294
[34,] 4.1686534 2.1339884
But the x, y coordinates of the plot go from -1 to 1, unlike the min-max coordinates in the layout matrix. So how is plot(G, layout = l) reading the layout matrix?
The according to the source, the plot method for objects of class igraph simply rescales the matrix from -1 to 1.
library(igraph)
set.seed(3)
l <- layout_with_fr(G)
[,1] [,2]
[1,] -2.283 0.658
[2,] -1.289 -0.108
[3,] 0.146 1.012
[4,] -1.523 1.601
#... with 30 more rows.
plot(G,layout = l)
maxs <- apply(l, 2, max)
mins <- apply(l, 2, min)
ll <- scale(l, center=(maxs+mins)/2, scale=(maxs-mins)/2)
ll
[,1] [,2]
[1,] -0.2422 -0.1051
[2,] -0.0704 -0.3821
[3,] 0.1775 0.0228
[4,] -0.1108 0.2357
#... with 30 more rows.
plot(G,layout = ll)
Note that the actual rescaling is performed with igraph::norm_coords:
igraph::norm_coords(l)
[,1] [,2]
[1,] -0.2422 -0.1051
[2,] -0.0704 -0.3821
[3,] 0.1775 0.0228
[4,] -0.1108 0.2357
#... with 30 more rows.

How to create a date sequence for 10 years with 16 Day interval with each year starts with 1st January

Following code create a date sequence of 10 years with 16 Day interval.
library(chron)
seq.dates("01/01/2008","12/31/2017", 16)
Output
[1] 01/01/08 01/17/08 02/02/08 02/18/08 03/05/08 03/21/08 04/06/08 04/22/08 05/08/08
[10] 05/24/08 06/09/08 06/25/08 07/11/08 07/27/08 08/12/08 08/28/08 09/13/08 09/29/08
[19] 10/15/08 10/31/08 11/16/08 12/02/08 12/18/08 **01/03/09** 01/19/09 02/04/09 02/20/09
[28] 03/08/09 03/24/09 04/09/09 04/25/09 05/11/09 ..........
........................
...........................
[208] 01/25/17 02/10/17 02/26/17 03/14/17 03/30/17 04/15/17 05/01/17 05/17/17 06/02/17
[217] 06/18/17 07/04/17 07/20/17 08/05/17 08/21/17 09/06/17 09/22/17 10/08/17 10/24/17
[226] 11/09/17 11/25/17 12/11/17 12/27/17
I want first entry for every year to be 1st January not the day which comes after 16 days from the last entry of previous year (BOLD entry in the example sequence) and subsequent entries accordingly.
A long way to do this would be creating date sequence for individual years separately then merging them in a single vector. I'm curious that is there any way to do this in a single line code.
How's this work for you. Uses sapply to pass a vector of starting points and then makes seq.dates do more limited sequences. The sapply function will simplify to an array if possible.
dates(sapply( seq.dates("01/01/2008", "01/01/2017", by="years") ,
function(x) seq.dates(x, to=x+365, by=16, length=23)))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 01/01/08 01/01/09 01/01/10 01/01/11 01/01/12 01/01/13 01/01/14 01/01/15
[2,] 01/17/08 01/17/09 01/17/10 01/17/11 01/17/12 01/17/13 01/17/14 01/17/15
[3,] 02/02/08 02/02/09 02/02/10 02/02/11 02/02/12 02/02/13 02/02/14 02/02/15
[4,] 02/18/08 02/18/09 02/18/10 02/18/11 02/18/12 02/18/13 02/18/14 02/18/15
[5,] 03/05/08 03/06/09 03/06/10 03/06/11 03/05/12 03/06/13 03/06/14 03/06/15
[6,] 03/21/08 03/22/09 03/22/10 03/22/11 03/21/12 03/22/13 03/22/14 03/22/15
[7,] 04/06/08 04/07/09 04/07/10 04/07/11 04/06/12 04/07/13 04/07/14 04/07/15
[8,] 04/22/08 04/23/09 04/23/10 04/23/11 04/22/12 04/23/13 04/23/14 04/23/15
[9,] 05/08/08 05/09/09 05/09/10 05/09/11 05/08/12 05/09/13 05/09/14 05/09/15
[10,] 05/24/08 05/25/09 05/25/10 05/25/11 05/24/12 05/25/13 05/25/14 05/25/15
[11,] 06/09/08 06/10/09 06/10/10 06/10/11 06/09/12 06/10/13 06/10/14 06/10/15
[12,] 06/25/08 06/26/09 06/26/10 06/26/11 06/25/12 06/26/13 06/26/14 06/26/15
[13,] 07/11/08 07/12/09 07/12/10 07/12/11 07/11/12 07/12/13 07/12/14 07/12/15
[14,] 07/27/08 07/28/09 07/28/10 07/28/11 07/27/12 07/28/13 07/28/14 07/28/15
[15,] 08/12/08 08/13/09 08/13/10 08/13/11 08/12/12 08/13/13 08/13/14 08/13/15
[16,] 08/28/08 08/29/09 08/29/10 08/29/11 08/28/12 08/29/13 08/29/14 08/29/15
[17,] 09/13/08 09/14/09 09/14/10 09/14/11 09/13/12 09/14/13 09/14/14 09/14/15
[18,] 09/29/08 09/30/09 09/30/10 09/30/11 09/29/12 09/30/13 09/30/14 09/30/15
[19,] 10/15/08 10/16/09 10/16/10 10/16/11 10/15/12 10/16/13 10/16/14 10/16/15
[20,] 10/31/08 11/01/09 11/01/10 11/01/11 10/31/12 11/01/13 11/01/14 11/01/15
[21,] 11/16/08 11/17/09 11/17/10 11/17/11 11/16/12 11/17/13 11/17/14 11/17/15
[22,] 12/02/08 12/03/09 12/03/10 12/03/11 12/02/12 12/03/13 12/03/14 12/03/15
[23,] 12/18/08 12/19/09 12/19/10 12/19/11 12/18/12 12/19/13 12/19/14 12/19/15
[,9] [,10]
[1,] 01/01/16 01/01/17
[2,] 01/17/16 01/17/17
[3,] 02/02/16 02/02/17
[4,] 02/18/16 02/18/17
[5,] 03/05/16 03/06/17
[6,] 03/21/16 03/22/17
[7,] 04/06/16 04/07/17
[8,] 04/22/16 04/23/17
[9,] 05/08/16 05/09/17
[10,] 05/24/16 05/25/17
[11,] 06/09/16 06/10/17
[12,] 06/25/16 06/26/17
[13,] 07/11/16 07/12/17
[14,] 07/27/16 07/28/17
[15,] 08/12/16 08/13/17
[16,] 08/28/16 08/29/17
[17,] 09/13/16 09/14/17
[18,] 09/29/16 09/30/17
[19,] 10/15/16 10/16/17
[20,] 10/31/16 11/01/17
[21,] 11/16/16 11/17/17
[22,] 12/02/16 12/03/17
[23,] 12/18/16 12/19/17
I was a bit surprised at this result since I thought the value would be a character matrix, but str shows it's a matrix of chron date elements. Can remove the apparent "matrix" (actually "dates" with a dimension attribute) structure with a call to c:
str(c(dates(sapply( seq.dates("01/01/2008", "01/01/2017", by="years") , function(x) seq.dates(x, to=x+365, by=16, length=23))) ))
'dates' num [1:230] 01/01/08 01/17/08 02/02/08 02/18/08 03/05/08 ...
- attr(*, "format")= chr "m/d/y"
- attr(*, "origin")= num [1:3] 1 1 1970

Neural network-development&simulation result&explanation

I've been up to developing a backward propagation(BP) neural network to make a time-series data prediction(past two year data to predict the 3rd one).AMORE package and Neural Network package in R have both been applied and I've got my output in AMORE.However,the big problem is that all my testing samples given the input values finally shared exactly the same outputs,which could really be disastrous.I would paste my codes here,hoping someone would do me a favor.I am really anxious with the explanation and really appreciate your help. Mercy!
library(AMORE)
tbincidence<-c(22.37,52.22,73.99,83.51,74.25,79.10,81.19,81.98,80.76,69.29,78.94,80.04,65.80,58.12,116.71,127.01,115.74,116.08,102.58,101.71,94.25,80.27,89.88,89.37,68.37,79.33,113.04,110.72,101.01,102.88,94.41,97.17,88.01,82.37,84.77,86.93,87.80,69.35,107.51,113.10,103.19,102.27,102.16,98.86,90.24,84.33,88.21,90.38,84.49,76.90,118.43,116.34,107.71,99.42,102.91,92.72,93.49,90.30,83.54,91.20,67.86,95.68,105.28,105.19,96.50,106.80,97.26,95.24,93.15,82.00,78.55,90.18,79.31,66.46,103.72,100.13,96.17,95.35,91.61,87.56,83.84,75.73,82.44,78.33,74.26,73.14,101.19,96.31,93.13,88.79,83.77,85.59,79.45,74.57,82.17,77.72,65.21,91.96,102.80,95.35,97.22,87.91,86.85,85.09,78.52,73.98,77.77,72.60,76.95,66.10,92.61,89.73,90.04,78.49,83.61,78.80,77.42,73.74,74.69,73.99,70.57,65.02,82.18,87.19,78.14,74.37,77.50,71.07,70.20,65.12,62.96,67.99,69.54,55.18,82.69,78.82,74.47,74.67,75.07,69.77,69.25,62.63,64.13,65.34,62.70,60.77,83.40,75.24,73.14,68.46,67.54,69.82,65.03,60.49,64.04,65.24,58.91,67.02)
tbincidence
lagtwo<-c(22.37,52.22,73.99,83.51,74.25,79.10,81.19,81.98,80.76,69.29,78.94,80.04,65.80,58.12,116.71,127.01,115.74,116.08,102.58,101.71,94.25,80.27,89.88,89.37,68.37,79.33,113.04,110.72,101.01,102.88,94.41,97.17,88.01,82.37,84.77,86.93,87.80,69.35,107.51,113.10,103.19,102.27,102.16,98.86,90.24,84.33,88.21,90.38,84.49,76.90,118.43,116.34,107.71,99.42,102.91,92.72,93.49,90.30,83.54,91.20,67.86,95.68,105.28,105.19,96.50,106.80,97.26,95.24,93.15,82.00,78.55,90.18,79.31,66.46,103.72,100.13,96.17,95.35,91.61,87.56,83.84,75.73,82.44,78.33,74.26,73.14,101.19,96.31,93.13,88.79,83.77,85.59,79.45,74.57,82.17,77.72,65.21,91.96,102.80,95.35,97.22,87.91,86.85,85.09,78.52,73.98,77.77,72.60,76.95,66.10,92.61,89.73,90.04,78.49,83.61,78.80,77.42,73.74,74.69,73.99,70.57,65.02,82.18,87.19,78.14,74.37,77.50,71.07,70.20,65.12,62.96,67.99,69.54,55.18,82.69,78.82,74.47,74.67,75.07,69.77,69.25,62.63,64.13,65.34,62.70,60.77,83.40,75.24,73.14,68.46,67.54,69.82,65.03,60.49,64.04,65.24)
lagone<-c(52.22,73.99,83.51,74.25,79.10,81.19,81.98,80.76,69.29,78.94,80.04,65.80,58.12,116.71,127.01,115.74,116.08,102.58,101.71,94.25,80.27,89.88,89.37,68.37,79.33,113.04,110.72,101.01,102.88,94.41,97.17,88.01,82.37,84.77,86.93,87.80,69.35,107.51,113.10,103.19,102.27,102.16,98.86,90.24,84.33,88.21,90.38,84.49,76.90,118.43,116.34,107.71,99.42,102.91,92.72,93.49,90.30,83.54,91.20,67.86,95.68,105.28,105.19,96.50,106.80,97.26,95.24,93.15,82.00,78.55,90.18,79.31,66.46,103.72,100.13,96.17,95.35,91.61,87.56,83.84,75.73,82.44,78.33,74.26,73.14,101.19,96.31,93.13,88.79,83.77,85.59,79.45,74.57,82.17,77.72,65.21,91.96,102.80,95.35,97.22,87.91,86.85,85.09,78.52,73.98,77.77,72.60,76.95,66.10,92.61,89.73,90.04,78.49,83.61,78.80,77.42,73.74,74.69,73.99,70.57,65.02,82.18,87.19,78.14,74.37,77.50,71.07,70.20,65.12,62.96,67.99,69.54,55.18,82.69,78.82,74.47,74.67,75.07,69.77,69.25,62.63,64.13,65.34,62.70,60.77,83.40,75.24,73.14,68.46,67.54,69.82,65.03,60.49,64.04,65.24,58.91)
predict<-cbind(lagtwo,lagone)
predict<-matrix(predict,ncol=2,byrow=F)
predict
output<-c(73.99,83.51,74.25,79.10,81.19,81.98,80.76,69.29,78.94,80.04,65.80,58.12,116.71,127.01,115.74,116.08,102.58,101.71,94.25,80.27,89.88,89.37,68.37,79.33,113.04,110.72,101.01,102.88,94.41,97.17,88.01,82.37,84.77,86.93,87.80,69.35,107.51,113.10,103.19,102.27,102.16,98.86,90.24,84.33,88.21,90.38,84.49,76.90,118.43,116.34,107.71,99.42,102.91,92.72,93.49,90.30,83.54,91.20,67.86,95.68,105.28,105.19,96.50,106.80,97.26,95.24,93.15,82.00,78.55,90.18,79.31,66.46,103.72,100.13,96.17,95.35,91.61,87.56,83.84,75.73,82.44,78.33,74.26,73.14,101.19,96.31,93.13,88.79,83.77,85.59,79.45,74.57,82.17,77.72,65.21,91.96,102.80,95.35,97.22,87.91,86.85,85.09,78.52,73.98,77.77,72.60,76.95,66.10,92.61,89.73,90.04,78.49,83.61,78.80,77.42,73.74,74.69,73.99,70.57,65.02,82.18,87.19,78.14,74.37,77.50,71.07,70.20,65.12,62.96,67.99,69.54,55.18,82.69,78.82,74.47,74.67,75.07,69.77,69.25,62.63,64.13,65.34,62.70,60.77,83.40,75.24,73.14,68.46,67.54,69.82,65.03,60.49,64.04,65.24,58.91,67.02)
output<-matrix(output,ncol=1)
output
trainPmatrix<-predict[1:130,]
trainTmatrix<-output[1:130,]
testPmatrix<-predict[131:156,]
testTmatrix<-output[131:156,]
net<-newff(n.neurons=c(2,5,1),learning.rate.global=1e-3,momentum.global=0.5,error.criterium="LMS",Stao=NA,hidden.layer="tansig",output="purelin",method="ADAPTgdwm")
result<- train(net,trainPmatrix,trainTmatrix,error.criterium="LMS",report=TRUE,show.step=100,n.shows=10)
y<-sim(result$net,testPmatrix)
y
The output would be something like this.
y
[,1]
[1,] 84.81958
[2,] 84.81958
[3,] 84.81957
[4,] 84.81958
[5,] 84.81958
[6,] 84.81958
[7,] 84.81958
[8,] 84.81958
[9,] 84.81958
[10,] 84.81958
[11,] 84.81958
[12,] 84.81958
[13,] 84.81958
[14,] 84.81958
[15,] 84.81958
[16,] 84.81958
[17,] 84.81958
[18,] 84.81958
[19,] 84.81958
[20,] 84.81958
[21,] 84.81958
[22,] 84.81958
[23,] 84.81958
[24,] 84.81958
[25,] 84.81958
[26,] 84.81958
How could that be possible!!
Thanks a lot for your sharp ideas!
It's been a while since I used R, and I'm not familiar with AMORE. But, are you normalizing your input in any way? It doesn't look like it. You're using the tansig function for the hidden layer, which returns values between -1 and 1. It might work better if your input were in that range. For time series, you might use the percentage change (expressed as between -1 and 1) from one value to the next instead of the raw values, then scaled if necessary. I'm sure there's a function in an R library which can do that for you. Then, you transform the output in reverse fashion to get the predictions.
You'll probably have to keep tuning the network (learning rate, momentum, etc.), but this might be a place to start.

Export TPS file format from R

My data (object called cc) is a 3D array. Each block begins with an identifier which is followed by a square chunk of data with 39 rows and 2 columns. They look like below:
, , 001-10-f
[,1] [,2]
[1,] 188.16863 379.5638
[2,] 188.16863 360.3510
[3,] 131.26338 349.7913
[4,] 164.26256 348.1780
[5,] 145.04971 358.0044
[6,] 147.68964 345.8314
[7,] 149.74292 353.1645
[8,] 212.51469 350.5246
[9,] 245.95385 352.2845
[10,] 229.08761 360.7910
[11,] 230.26091 348.6180
[12,] 227.91430 355.6578
[13,] 164.99587 310.3389
[14,] 211.04806 310.4856
[15,] 171.15572 320.8986
[16,] 204.88821 320.4587
[17,] 177.46223 300.2192
[18,] 199.75501 300.2192
[19,] 188.16863 303.4457
[20,] 188.16863 282.1796
[21,] 180.24883 284.8195
[22,] 199.46168 282.4729
[23,] 160.44932 265.7534
[24,] 215.44795 267.3666
[25,] 177.46223 282.9129
[26,] 205.18154 277.4864
[27,] 187.87530 257.9802
[28,] 187.87530 270.4466
[29,] 95.77094 327.0585
[30,] 282.17961 331.0184
[31,] 107.94397 254.3136
[32,] 272.93984 259.1535
[33,] 189.34193 209.2881
[34,] 113.37050 365.0442
[35,] 129.79675 389.5369
[36,] 172.62235 382.6438
[37,] 201.95495 381.4705
[38,] 244.63388 391.7369
[39,] 265.46003 369.0041
, , 002-10-f
[,1] [,2]
[1,] 194.3285 361.2310
[2,] 191.9819 344.3647
[3,] 141.0898 340.4048
[4,] 172.0357 336.7382
[5,] 153.5562 345.6847
[6,] 155.9028 335.5649
[7,] 158.6894 342.4581
[8,] 214.1280 333.6583
[9,] 245.9539 333.9517
[10,] 229.9676 340.8448
[11,] 230.5542 330.1384
[12,] 228.3543 337.6182
[13,] 170.4224 293.9127
[14,] 210.7547 292.7393
[15,] 173.9423 303.1524
[16,] 208.8481 301.6858
[17,] 180.1022 286.2862
[18,] 200.3417 285.4062
[19,] 190.2219 286.4328
[20,] 188.4620 271.4732
[21,] 182.7421 274.1131
[22,] 195.3551 273.5265
[23,] 167.3425 259.1535
[24,] 213.5413 256.6602
[25,] 180.2488 272.7932
[26,] 199.6083 270.7399
[27,] 188.6086 249.6204
[28,] 189.0486 261.3535
[29,] 111.0239 321.6320
[30,] 274.9931 309.8989
[31,] 126.1302 252.1137
[32,] 259.0069 246.5405
[33,] 188.9019 202.2483
[34,] 126.4235 355.6578
[35,] 139.6232 374.4306
[36,] 182.8888 362.2576
[37,] 204.8882 359.4710
[38,] 248.8871 367.2442
[39,] 264.7267 348.4713
, , 003-10-f
[,1] [,2]
[1,] 195.25311 371.5863
[2,] 194.64767 356.9045
[3,] 139.85572 351.1529
[4,] 172.09518 346.4608
[5,] 151.20764 356.9045
[6,] 155.14298 343.8876
[7,] 156.35385 352.5151
[8,] 215.98929 344.6444
[9,] 248.98555 346.6121
[10,] 235.21189 353.5746
[11,] 236.72548 341.0118
[12,] 232.63878 348.5798
[13,] 167.85713 303.3234
[14,] 214.02163 303.0207
[15,] 172.54926 314.2213
[16,] 210.23765 312.4050
[17,] 179.20906 293.7878
[18,] 204.94009 293.6365
[19,] 190.86370 293.4851
[20,] 190.10690 270.0245
[21,] 182.99303 272.9003
[22,] 199.49116 273.2030
[23,] 160.13782 261.3970
[24,] 217.80560 259.4293
[25,] 179.20906 271.8408
[26,] 204.94009 271.2353
[27,] 189.50147 248.0774
[28,] 189.65283 262.7592
[29,] 98.23199 333.7466
[30,] 283.79812 325.8759
[31,] 118.81681 250.6505
[32,] 260.64020 246.4125
[33,] 187.83652 206.3023
[34,] 119.42225 370.3755
[35,] 138.34213 386.1168
[36,] 177.99818 372.4945
[37,] 212.35668 372.3431
[38,] 247.92604 382.7869
[39,] 265.63504 367.0456
, , 004-10-f
[,1] [,2]
[1,] 195.7082 358.5218
[2,] 194.1993 340.5655
[3,] 141.2358 329.0976
[4,] 171.8671 329.0976
[5,] 156.0234 336.0387
[6,] 155.1180 324.4199
[7,] 157.5323 331.8137
[8,] 216.0788 326.5325
[9,] 247.6154 326.3816
[10,] 232.3752 333.9262
[11,] 236.1475 322.6092
[12,] 232.6770 329.2485
[13,] 168.3966 284.7351
[14,] 214.2681 283.2262
[15,] 174.7341 295.2976
[16,] 209.5904 293.4869
[17,] 178.5064 275.9833
[18,] 205.0636 274.6253
[19,] 191.1814 274.3235
[20,] 191.0305 261.9502
[21,] 183.3350 264.0627
[22,] 197.8207 263.4592
[23,] 161.3046 246.2574
[24,] 218.9457 245.5029
[25,] 178.3555 261.3467
[26,] 204.6109 259.2342
[27,] 190.2761 231.6208
[28,] 190.7288 244.1449
[29,] 112.1135 310.8396
[30,] 278.0958 307.3690
[31,] 125.2412 236.9020
[32,] 262.2520 235.9967
[33,] 189.8234 191.4832
[34,] 124.0340 341.7726
[35,] 144.4046 367.4245
[36,] 180.6189 354.4477
[37,] 209.5904 355.0512
[38,] 247.7663 364.8593
[39,] 265.2699 342.2253
, , 005-10-f
[,1] [,2]
[1,] 227.2118 375.5037
[2,] 226.1675 359.3915
[3,] 171.4159 356.2586
[4,] 202.5959 349.5451
[5,] 185.1411 361.1817
[6,] 185.2903 349.3960
[7,] 185.1411 357.1537
[8,] 242.7272 344.4728
[9,] 273.4598 346.2630
[10,] 254.5130 353.2748
[11,] 264.5086 341.4890
[12,] 258.3919 349.6943
[13,] 196.6285 304.3415
[14,] 244.8159 299.8659
[15,] 203.7894 316.1273
[16,] 240.3403 310.6073
[17,] 207.3699 295.8378
[18,] 235.5663 292.5557
[19,] 221.0951 292.5557
[20,] 219.4541 274.8025
[21,] 212.2931 278.5321
[22,] 226.7642 276.4435
[23,] 191.5561 266.8955
[24,] 242.2797 262.4199
[25,] 204.5354 276.2943
[26,] 232.8809 272.5646
[27,] 216.6195 247.6504
[28,] 218.1114 264.5086
[29,] 134.1191 341.0415
[30,] 298.6724 322.2439
[31,] 145.4573 264.9561
[32,] 274.2057 250.9325
[33,] 213.7850 214.2325
[34,] 155.3037 373.5642
[35,] 175.4439 387.2895
[36,] 211.9947 375.5037
[37,] 237.8041 372.6691
[38,] 267.0447 378.7858
[39,] 287.6325 364.6130
I want to export this array to .xlsx or .csv or .txt format with the array with the following changes:
1. remove the two commas before each block's identifier (e.g., the two commas before "001-10-f"), add "IMAGE=" before and ".JPG" after the identifier, and move each identifier from the beginning to the end of each block;
2. add "LM=39" to the beginning of each block;
3. remove the row ([1,] to [39,]) and column ([,1] and [,2]) labels.
The above changes format the data into TPS format ready for geometric morphometric analysis.
The exported data should look like below:
LM=39
188.16863 379.5638
188.16863 360.351
131.26338 349.7913
164.26256 348.178
145.04971 358.0044
147.68964 345.8314
149.74292 353.1645
212.51469 350.5246
245.95385 352.2845
229.08761 360.791
230.26091 348.618
227.9143 355.6578
164.99587 310.3389
211.04806 310.4856
171.15572 320.8986
204.88821 320.4587
177.46223 300.2192
199.75501 300.2192
188.16863 303.4457
188.16863 282.1796
180.24883 284.8195
199.46168 282.4729
160.44932 265.7534
215.44795 267.3666
177.46223 282.9129
205.18154 277.4864
187.8753 257.9802
187.8753 270.4466
95.77094 327.0585
282.17961 331.0184
107.94397 254.3136
272.93984 259.1535
189.34193 209.2881
113.3705 365.0442
129.79675 389.5369
172.62235 382.6438
201.95495 381.4705
244.63388 391.7369
265.46003 369.0041
IMAGE=001-10-f.JPG
LM=39
194.3285 361.231
191.9819 344.3647
141.0898 340.4048
172.0357 336.7382
153.5562 345.6847
155.9028 335.5649
158.6894 342.4581
214.128 333.6583
245.9539 333.9517
229.9676 340.8448
230.5542 330.1384
228.3543 337.6182
170.4224 293.9127
210.7547 292.7393
173.9423 303.1524
208.8481 301.6858
180.1022 286.2862
200.3417 285.4062
190.2219 286.4328
188.462 271.4732
182.7421 274.1131
195.3551 273.5265
167.3425 259.1535
213.5413 256.6602
180.2488 272.7932
199.6083 270.7399
188.6086 249.6204
189.0486 261.3535
111.0239 321.632
274.9931 309.8989
126.1302 252.1137
259.0069 246.5405
188.9019 202.2483
126.4235 355.6578
139.6232 374.4306
182.8888 362.2576
204.8882 359.471
248.8871 367.2442
264.7267 348.4713
IMAGE=002-10-f.JPG
LM=39
195.25311 371.5863
194.64767 356.9045
139.85572 351.1529
172.09518 346.4608
151.20764 356.9045
155.14298 343.8876
156.35385 352.5151
215.98929 344.6444
248.98555 346.6121
235.21189 353.5746
236.72548 341.0118
232.63878 348.5798
167.85713 303.3234
214.02163 303.0207
172.54926 314.2213
210.23765 312.405
179.20906 293.7878
204.94009 293.6365
190.8637 293.4851
190.1069 270.0245
182.99303 272.9003
199.49116 273.203
160.13782 261.397
217.8056 259.4293
179.20906 271.8408
204.94009 271.2353
189.50147 248.0774
189.65283 262.7592
98.23199 333.7466
283.79812 325.8759
118.81681 250.6505
260.6402 246.4125
187.83652 206.3023
119.42225 370.3755
138.34213 386.1168
177.99818 372.4945
212.35668 372.3431
247.92604 382.7869
265.63504 367.0456
IMAGE=003-10-f.JPG
LM=39
195.7082 358.5218
194.1993 340.5655
141.2358 329.0976
171.8671 329.0976
156.0234 336.0387
155.118 324.4199
157.5323 331.8137
216.0788 326.5325
247.6154 326.3816
232.3752 333.9262
236.1475 322.6092
232.677 329.2485
168.3966 284.7351
214.2681 283.2262
174.7341 295.2976
209.5904 293.4869
178.5064 275.9833
205.0636 274.6253
191.1814 274.3235
191.0305 261.9502
183.335 264.0627
197.8207 263.4592
161.3046 246.2574
218.9457 245.5029
178.3555 261.3467
204.6109 259.2342
190.2761 231.6208
190.7288 244.1449
112.1135 310.8396
278.0958 307.369
125.2412 236.902
262.252 235.9967
189.8234 191.4832
124.034 341.7726
144.4046 367.4245
180.6189 354.4477
209.5904 355.0512
247.7663 364.8593
265.2699 342.2253
IMAGE=004-10-f.JPG
LM=39
227.2118 375.5037
226.1675 359.3915
171.4159 356.2586
202.5959 349.5451
185.1411 361.1817
185.2903 349.396
185.1411 357.1537
242.7272 344.4728
273.4598 346.263
254.513 353.2748
264.5086 341.489
258.3919 349.6943
196.6285 304.3415
244.8159 299.8659
203.7894 316.1273
240.3403 310.6073
207.3699 295.8378
235.5663 292.5557
221.0951 292.5557
219.4541 274.8025
212.2931 278.5321
226.7642 276.4435
191.5561 266.8955
242.2797 262.4199
204.5354 276.2943
232.8809 272.5646
216.6195 247.6504
218.1114 264.5086
134.1191 341.0415
298.6724 322.2439
145.4573 264.9561
274.2057 250.9325
213.785 214.2325
155.3037 373.5642
175.4439 387.2895
211.9947 375.5037
237.8041 372.6691
267.0447 378.7858
287.6325 364.613
IMAGE=005-10-f.JPG
May I know how to perform the conversion in R and export the converted object?
Thank you!
You have to iterate over cc and use string formatting as necessary. You can use nested for loops and R's version of sprintf.
sink ('output-file.txt')
for (i in 1:dim(cc)[3]) {
cat (sprintf ("LM=39\n"))
for (j in 1:dim(cc)[1]) {
cat (sprintf ("%.4f %.4f\n", cc[j,1,i], cc[j,2,i]))
}
cat (sprintf ("IMAGE=%s.JPG\n", dimnames(cc)[[3]][i]))
}

Resources