impute error: a column has 80% missing value - r

I need to calculate the missing values (specific probeset)in the sample using 6 nearest neighbors and Euclidean distance with the impute.knn() function. I had to replace a value with NA in my dataset and then impute the missing value just to test the function
mat1
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20]
[1,] 8176.8 8308.4 13002.9 8385.3 8962.8 8391.5 8834.2 10978.4 11863.2 10479 51.7 171.9 581.4 161.4 167.9 25.1 213.2 122.6 81.7 111.8
> mat1[1,4] <- NA
> mat1
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20]
[1,] 8176.8 8308.4 13002.9 NA 8962.8 8391.5 8834.2 10978.4 11863.2 10479 51.7 171.9 581.4 161.4 167.9 25.1 213.2 122.6 81.7 111.8
> impute.knn(mat1,2)
Error in impute.knn(mat1, 2) :
a column has more than 80 % missing values!
Here is my error,, any suggestions would be appreciated

Note that since you have a single row, any missing value will be 100% missing values for that column. Consequently adding a NA on the column and calculating inpute.knn(mat1, 2) will result in the error message you are reading.
As has been noted several times, this is clearly stated in the documentation at ?impute.knn:
rowmax - The maximum percent missing data allowed in any row (default
50%). For any rows with more than rowmax% missing are imputed using
the overall mean per sample.
colmax - The maximum percent missing data
allowed in any column (default 80%). If any column has more than
colmax% missing data, the program halts and reports an error.
So the solution is to use a test matrix that is more than one row or column, and then further to adjust these arguments as needed.

Related

Solving a system equation with R

This is the Markowitz's efficient portfolio problem, but the "original". I need to maximize the return of a portfolio subject to a level of risk.
I have a vector mu of returns:
mu
[,1] [,2] [,3] [,4] [,5] [,6]
2020-11-11 0.0002720645 0.000436814 0.0001976725 7.367183e-05 0.0001061771 2.123921e-05
[,7] [,8] [,9] [,10] [,11] [,12]
2020-11-11 0.0002674939 -7.217231e-05 7.246612e-05 0.0003106428 0.0002488269 -9.916666e-05
[,13] [,14] [,15] [,16] [,17] [,18]
2020-11-11 0.0001324967 0.000121239 0.0001060435 0.0002293328 0.0001029351 8.083295e-05
A variance covariance matrix:
sigma
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 5.592601e-05 1.867019e-07 -1.928308e-06 6.144346e-07 -2.773157e-07 1.659026e-05
[2,] 1.867019e-07 2.484421e-06 3.387304e-07 -1.116725e-07 4.748165e-08 6.849967e-06
[3,] -1.928308e-06 3.387304e-07 1.203656e-06 -5.667758e-08 1.085685e-07 -3.056249e-07
[4,] 6.144346e-07 -1.116725e-07 -5.667758e-08 2.100015e-08 -6.407951e-09 -5.022572e-08
[5,] -2.773157e-07 4.748165e-08 1.085685e-07 -6.407951e-09 2.156967e-07 3.545964e-09
[6,] 1.659026e-05 6.849967e-06 -3.056249e-07 -5.022572e-08 3.545964e-09 5.226998e-05
[7,] 1.801405e-05 8.490776e-06 -5.241004e-07 1.107834e-07 6.741364e-09 3.427730e-05
[8,] 4.081106e-05 2.616975e-06 -2.783963e-06 6.162108e-07 -2.925430e-07 4.131267e-05
[9,] 5.406343e-08 -9.440572e-09 -5.508222e-09 1.364631e-09 -5.884534e-10 -3.247901e-09
[10,] 1.436211e-05 6.552363e-06 -1.738885e-07 -1.002123e-07 1.232430e-08 3.661951e-05
[11,] 9.153688e-06 4.140858e-07 -4.691999e-07 1.450149e-07 -5.655290e-08 6.904077e-06
[12,] 2.503943e-05 9.261196e-07 -6.080288e-07 3.882300e-07 -8.190216e-08 1.727989e-05
[13,] 2.346539e-06 -2.721675e-07 -1.601445e-08 5.728516e-08 -4.386966e-09 2.463735e-07
[14,] 1.231017e-06 -2.161433e-07 -1.686978e-08 3.623457e-08 3.462555e-08 -1.252470e-07
[15,] 2.803784e-06 -4.366683e-07 1.349062e-07 1.065253e-07 1.319820e-08 -2.084133e-07
[16,] 5.000938e-06 -8.268924e-07 6.099295e-07 1.324316e-07 5.926589e-08 -9.965225e-07
[17,] -1.062064e-07 2.265318e-08 2.875487e-08 -3.366920e-09 1.776274e-09 4.806526e-09
[18,] -4.585421e-07 6.782657e-08 3.215220e-07 -1.258659e-08 2.188159e-08 -1.362058e-07
[,7] [,8] [,9] [,10] [,11] [,12]
[1,] 1.801405e-05 4.081106e-05 5.406343e-08 1.436211e-05 9.153688e-06 2.503943e-05
[2,] 8.490776e-06 2.616975e-06 -9.440572e-09 6.552363e-06 4.140858e-07 9.261196e-07
[3,] -5.241004e-07 -2.783963e-06 -5.508222e-09 -1.738885e-07 -4.691999e-07 -6.080288e-07
[4,] 1.107834e-07 6.162108e-07 1.364631e-09 -1.002123e-07 1.450149e-07 3.882300e-07
[5,] 6.741364e-09 -2.925430e-07 -5.884534e-10 1.232430e-08 -5.655290e-08 -8.190216e-08
[6,] 3.427730e-05 4.131267e-05 -3.247901e-09 3.661951e-05 6.904077e-06 1.727989e-05
[7,] 5.237200e-05 3.680683e-05 5.354780e-09 2.968337e-05 7.161037e-06 1.813164e-05
[8,] 3.680683e-05 1.662421e-04 5.065368e-08 7.083157e-05 1.028446e-05 2.650823e-05
[9,] 5.354780e-09 5.065368e-08 1.520614e-10 -7.552573e-09 1.266719e-08 3.327868e-08
[10,] 2.968337e-05 7.083157e-05 -7.552573e-09 4.900492e-05 4.501299e-06 1.198971e-05
[11,] 7.161037e-06 1.028446e-05 1.266719e-08 4.501299e-06 1.493544e-05 2.091619e-05
[12,] 1.813164e-05 2.650823e-05 3.327868e-08 1.198971e-05 2.091619e-05 6.634911e-05
[13,] 5.699686e-07 1.961050e-06 4.834209e-09 -6.040182e-08 2.463262e-06 4.880533e-06
[14,] 1.583679e-07 1.116495e-06 3.072008e-09 -2.197953e-07 2.903155e-07 8.507265e-07
[15,] 7.753749e-07 2.291707e-06 4.353285e-09 -5.523707e-07 6.641738e-07 2.131070e-06
[16,] 2.247954e-07 3.167986e-06 1.075832e-08 -1.326120e-06 9.988603e-07 3.492155e-06
[17,] -9.016614e-09 -1.176373e-07 -3.044293e-10 1.502323e-08 -2.420553e-08 -4.921572e-08
[18,] -1.588521e-07 -7.068861e-07 -1.222680e-09 -1.076004e-07 -9.341448e-08 -9.692232e-08
[,13] [,14] [,15] [,16] [,17] [,18]
[1,] 2.346539e-06 1.231017e-06 2.803784e-06 5.000938e-06 -1.062064e-07 -4.585421e-07
[2,] -2.721675e-07 -2.161433e-07 -4.366683e-07 -8.268924e-07 2.265318e-08 6.782657e-08
[3,] -1.601445e-08 -1.686978e-08 1.349062e-07 6.099295e-07 2.875487e-08 3.215220e-07
[4,] 5.728516e-08 3.623457e-08 1.065253e-07 1.324316e-07 -3.366920e-09 -1.258659e-08
[5,] -4.386966e-09 3.462555e-08 1.319820e-08 5.926589e-08 1.776274e-09 2.188159e-08
[6,] 2.463735e-07 -1.252470e-07 -2.084133e-07 -9.965225e-07 4.806526e-09 -1.362058e-07
[7,] 5.699686e-07 1.583679e-07 7.753749e-07 2.247954e-07 -9.016614e-09 -1.588521e-07
[8,] 1.961050e-06 1.116495e-06 2.291707e-06 3.167986e-06 -1.176373e-07 -7.068861e-07
[9,] 4.834209e-09 3.072008e-09 4.353285e-09 1.075832e-08 -3.044293e-10 -1.222680e-09
[10,] -6.040182e-08 -2.197953e-07 -5.523707e-07 -1.326120e-06 1.502323e-08 -1.076004e-07
[11,] 2.463262e-06 2.903155e-07 6.641738e-07 9.988603e-07 -2.420553e-08 -9.341448e-08
[12,] 4.880533e-06 8.507265e-07 2.131070e-06 3.492155e-06 -4.921572e-08 -9.692232e-08
[13,] 8.999334e-07 1.321428e-07 3.261184e-07 6.050874e-07 -6.496856e-09 9.309784e-09
[14,] 1.321428e-07 9.120545e-08 1.986952e-07 3.772898e-07 -4.786053e-09 7.777481e-09
[15,] 3.261184e-07 1.986952e-07 8.154712e-07 1.007473e-06 -5.816547e-09 6.094590e-08
[16,] 6.050874e-07 3.772898e-07 1.007473e-06 2.313540e-06 -2.842490e-09 2.360389e-07
[17,] -6.496856e-09 -4.786053e-09 -5.816547e-09 -2.842490e-09 1.682421e-09 8.910643e-09
[18,] 9.309784e-09 7.777481e-09 6.094590e-08 2.360389e-07 8.910643e-09 1.407667e-07
And I want to solve the following maximization problem:
Where mu_p is the portfolio return (weighted average), sigma_p**2 is the variance of the portfolio, SIGMA is the varcov matrix, 1 is a vector of 1 and x are the weights
Plus a constraint that make all values of x greater or equal than cero
I really don't know how to do that in R, I have review some packages like ROI and CVXR, but I don't understand how to solve this.

Convert data to quarterly returns

I need to convert all my monthly return data into quarterly returns. But it has to be done in a specific way. I set up the problem as follows:
mat1 <- matrix(nrow = 2,
ncol = (37 + 37),
data = rnorm(74, 0, 1))
seperate.mat <- c()
seq1 <- seq (37, 74, 37)
for (i in seq1) {
seperate.mat[[which(i == seq1)]] = mat1[, (i - 36):i]
}
seperate.mat[[1]]
seperate.mat[[2]]
For each row in seperate.mat[[i]] i need to convert monthly return data into quarterly data.
> mat1
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
[1,] 0.4342922 -1.4269698 0.488264775 -0.5366129 0.04941425 -0.2497828 -1.439336 -3.3251713 1.10019063 0.18061550 0.5497727 0.1029185 -1.1238067 0.7291232
[2,] -1.0269178 -0.2531924 0.001663337 -0.6085732 -1.59923701 -0.2183555 1.663534 -0.1202581 -0.03978411 -0.09819961 0.4751221 1.0787960 0.2576227 0.4813277
> seperate.mat[[1]]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
[1,] 0.4342922 -1.4269698 0.488264775 -0.5366129 0.04941425 -0.2497828 -1.439336 -3.3251713 1.10019063 0.18061550 0.5497727 0.1029185 -1.1238067 0.7291232
[2,] -1.0269178 -0.2531924 0.001663337 -0.6085732 -1.59923701 -0.2183555 1.663534 -0.1202581 -0.03978411 -0.09819961 0.4751221 1.0787960 0.2576227 0.4813277
then the first entry in row1 of new output.matrix would be (1+0.43)(1-1.43)(1+0.49)-1. the second entry would be (1-1.43)(1_0.49)(1-0.54) -1.. and so on and so forth.
I want to apply this process to all matrices in the list seperate.mat
Your data is not reproducible because you are missing set.seed. But you can use rollapply from zoo to go over the rows combined with an apply statement. If I put all of this in a function you can then call this function with lapply and to run over all the matrices in one go.
set.seed(1234) # for reproducibility
mat1 <- matrix(nrow = 2, ncol = (37 + 37), data = rnorm(74, 0, 1))
seperate.mat <- c()
seq1 <- seq (37, 74, 37)
for (i in seq1) {
seperate.mat[[which(i == seq1)]] = mat1[, (i-36):i]
}
# create period 3 function.
my_period_function <- function(x) {
t(apply(x, 1, function(x) {
zoo::rollapply(x, width = 3, function(x) {
(1+x[1]) * (1+x[2]) * (1+x[3]) - 1
},
align = "right",
fill = NA)
}
)
)
}
lapply(seperate.mat, my_period_function)
[[1]]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
[1,] NA NA -1.616834 0.2668183 -0.7352959 -0.9031650 -0.9490513 -0.7707860 -0.7856123 -0.8439821 -0.9097022 -0.8966906 -0.8056753
[2,] NA NA -3.588961 -1.9188394 -0.9249181 -0.9999196 -0.9998111 -0.9984719 -0.9158963 -0.7301124 -0.8455037 1.5392960 -1.3331908
[,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25]
[1,] -0.7301674 -0.5249856 2.260488 -0.3984036 -1.384278 -0.7843013 -0.4761154 -3.0398455 -0.7499798 -0.9979948 -1.000088 -1.000289
[2,] -0.9845245 -0.9993209 -1.000795 -0.9832478 -1.043840 -0.9714934 -0.9694723 -0.9874984 -1.0263740 -1.0015548 -1.005707 -1.003992
[,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37]
[1,] -0.9588103 -0.9582071 -0.9264772 -1.241523 4.780042 10.430416 3.166862 0.5868448 -1.132772 -1.320440 -1.3251214 0.4858955
[2,] -1.0529872 -1.0031462 -1.0097738 -1.005300 -1.055905 -1.126936 -1.185025 2.2560412 1.172376 7.788185 0.7231891 0.1995426
[[2]]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
[1,] NA NA -1.616834 0.2668183 -0.7352959 -0.9031650 -0.9490513 -0.7707860 -0.7856123 -0.8439821 -0.9097022 -0.8966906 -0.8056753
[2,] NA NA -3.588961 -1.9188394 -0.9249181 -0.9999196 -0.9998111 -0.9984719 -0.9158963 -0.7301124 -0.8455037 1.5392960 -1.3331908
[,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25]
[1,] -0.7301674 -0.5249856 2.260488 -0.3984036 -1.384278 -0.7843013 -0.4761154 -3.0398455 -0.7499798 -0.9979948 -1.000088 -1.000289
[2,] -0.9845245 -0.9993209 -1.000795 -0.9832478 -1.043840 -0.9714934 -0.9694723 -0.9874984 -1.0263740 -1.0015548 -1.005707 -1.003992
[,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37]
[1,] -0.9588103 -0.9582071 -0.9264772 -1.241523 4.780042 10.430416 3.166862 0.5868448 -1.132772 -1.320440 -1.3251214 0.4858955
[2,] -1.0529872 -1.0031462 -1.0097738 -1.005300 -1.055905 -1.126936 -1.185025 2.2560412 1.172376 7.788185 0.7231891 0.1995426

convert pictures to pixels dataframe in R (EBImage lib)

I have folder with images
C:/Users/admin/Downloads/mypicture
Here several images, I need read it.
I use library("EBImage")
x <- readImage("C:/Users/admin/Downloads/mypicture")
then get the error
Error in readImage("C:/Users/admin/Downloads/mypicture") :
Unable to determine type of C:/Users/admin/Downloads/mypicture: Filename extension missing.
How to read all pictures at once,
and then extract pixel array for each picture
# width and height of the original image
dim(x)[1:2]
# scale to a specific width and height
y <- resize(x, w = 200, h = 100)
# scale by 50%; the height is determined automatically so that
# the aspect ratio is preserved
y <- resize(x, dim(x)[1]/2)
# show the scaled image
display(y)
# extract the pixel array
z <- imageData(y)
How to do it?
here two images from my folder
I need to create pixel array
z <- array(y)
like this,
where marked name of picture kBFrf.jpg
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 0.4676471 0.5000000 0.5382353 0.5803922 0.6107843 0.6441176 0.6715686 0.6843137 0.6901961 0.7117647 0.7303922 0.7362745
[2,] 0.4735294 0.5078431 0.5441176 0.5745098 0.6186275 0.6392157 0.6774510 0.6882353 0.7127451 0.7245098 0.7500000 0.7764706
[3,] 0.4735294 0.5039216 0.5470588 0.5892157 0.6186275 0.6539216 0.6764706 0.7049020 0.7225490 0.7343137 0.7637255 0.7862745
[,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24]
[1,] 0.7686275 0.7843137 0.8000000 0.8156863 0.8450980 0.8460784 0.8519608 0.8539216 0.8529412 0.8607843 0.8647059 0.8568627
[2,] 0.8049020 0.8274510 0.8392157 0.8745098 0.8843137 0.8892157 0.8960784 0.8882353 0.8911765 0.9009804 0.8950980 0.8872549
[3,] 0.8058824 0.8303922 0.8558824 0.8735294 0.8784314 0.8901961 0.8911765 0.8882353 0.8794118 0.8823529 0.8803922 0.8852941
69onL.jpg
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 0.4676471 0.5000000 0.5382353 0.5803922 0.6107843 0.6441176 0.6715686 0.6843137 0.6901961 0.7117647 0.7303922 0.7362745
[2,] 0.4735294 0.5078431 0.5441176 0.5745098 0.6186275 0.6392157 0.6774510 0.6882353 0.7127451 0.7245098 0.7500000 0.7764706
[3,] 0.4735294 0.5039216 0.5470588 0.5892157 0.6186275 0.6539216 0.6764706 0.7049020 0.7225490 0.7343137 0.7637255 0.7862745
[,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24]
[1,] 0.7686275 0.7843137 0.8000000 0.8156863 0.8450980 0.8460784 0.8519608 0.8539216 0.8529412 0.8607843 0.8647059 0.8568627
[2,] 0.8049020 0.8274510 0.8392157 0.8745098 0.8843137 0.8892157 0.8960784 0.8882353 0.8911765 0.9009804 0.8950980 0.8872549
[3,] 0.8058824 0.8303922 0.8558824 0.8735294 0.8784314 0.8901961 0.8911765 0.8882353 0.8794118 0.8823529 0.8803922 0.8852941
How to create such dataframe
result for picture with digits
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19]
[1,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1.0000000 1.0000000
[2,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1.0000000 1.0000000
[,20] [,21] [,22] [,23] [,24] [,25] [,26] [,27] [,28] [,29]
[1,] 1.00000000 1.00000000 1.000000000 1.000000000 1.000000000 1.000000000 1.000000000 1.000000000 1.000000000 1.000000000
readImage function takes as input a vector of file names, so in order to read all images from a directory you need to list its contents
library(EBImage)
# create a vector of file names
fls <- dir("./img", full.names = TRUE)
# read images
imgs <- readImage(fls, 'png')
# display images
display(imgs, method = 'raster', all = TRUE)
Image data is stored in an Image class which internally holds an array
# internal representation of images data
y <- class(imageData(imgs))
# dimensions of image stack
dim(imgs)
## [1] 825 825 4 10
The first two dimensions correspond to the XY dimensions, the third one holds color channels (RGBA in this case), and the last one are the individual images. In order to extract a single image, for example the second one, use
img <- getFrame(imgs, 2, "render")
If you would like to refer to images by their original names you could use an approach similar to the following.
# mapping of file names to frame indices
idx <- setNames(1:length(fls), basename(fls))
getFrame(imgs, idx['sample.png'], "render")

binding dates in data frame

I am trying to understand, why what I am doing is not working, and why I am receiving the results that I am. Both the reason why when I have a single column of data frame and try to add a row, I am expanding it somehow, and also why are the formats of the dates changing? If anybody could please help out, in addition to knowing the proper way, I am really interested in knowing the reason why it doesn't work.
thanks
class(wklyAvg.hedge.cost$Date[[NROW(wklyAvg.hedge.cost)]]+7)
[1] "Date"
class(wklyAvg.hedge.cost$Date[2:NROW(wklyAvg.hedge.cost$Date)])
[1] "Date"
head(wklyAvg.hedge.cost$Date[2:NROW(wklyAvg.hedge.cost$Date)])
[1] "1997-01-10" "1997-01-17" "1997-01-24" "1997-01-31" "1997-02-07" "1997-02-14"
wklyAvg.hedge.cost$Date[[NROW(wklyAvg.hedge.cost)]]+7
[1] "2014-01-10"
hedge.apply.dates <- rbind(wklyAvg.hedge.cost$Date[2:NROW(wklyAvg.hedge.cost$Date)],wklyAvg.hedge.cost$Date[[NROW(wklyAvg.hedge.cost)]]+7)
head(hedge.apply.dates)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37]
[1,] 9871 9878 9885 9892 9899 9906 9913 9920 9927 9934 9941 9948 9955 9962 9969 9976 9983 9990 9997 10004 10011 10018 10025 10032 10039 10046 10053 10060 10067 10074 10081 10088 10095 10102 10109 10116 10123
[2,] 16080 16080 16080 16080 1608
Just do this instead
hedge.apply.dates <- c(wklyAvg.hedge.cost$Date[2:NROW(wklyAvg.hedge.cost$Date)],wklyAvg.hedge.cost$Date[[NROW(wklyAvg.hedge.cost)]]+7)
rbind binds rows. Since your two vectors are not of the same length, the shorter is recycled. Also, as help("rbind") points out, "any classes the inputs might have are discarded". Thus you end up with the internal integer representation of the Dates in a two-row matrix.
a <- as.Date(c("1997-01-10", "1997-01-17", "1997-01-24", "1997-01-31", "1997-02-07", "1997-02-14"))
b <- as.Date("2014-01-10")
rbind(a, b)
# [,1] [,2] [,3] [,4] [,5] [,6]
#a 9871 9878 9885 9892 9899 9906
#b 16080 16080 16080 16080 16080 16080
Possibly you'd like to use c (which creates a vector) instead?
c(a, b)
#[1] "1997-01-10" "1997-01-17" "1997-01-24" "1997-01-31" "1997-02-07" "1997-02-14" "2014-01-10"

if distance from one point to others is less than certain amount, how do I tell it to change the mark of the points?

I am using the package spatstat. I have a data frame of coordinates that I have divided into two sets, healthy(Mark=no) and diseased(Mark=yes). I am able to find the distance between the diseased point and all of the other points:
>D<-crossdist(diseased,healthy)
But now I need to remark my points based on the distances they are from the diseased point, sort of like:
>i<-length(D)
>for n=1:i,
> if D[n]<1,
> mark(n)<-yes,
> else,
> mark(n)<-no,
> end
>end
I am new to using loops in R, and I was never really good at it in Matlab, I am still going through all of the help guides, but any tips on how to do this would be very helpful.
D looks like:
> D
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 0.4796548 5.906068 6.061941 5.837476 5.62358 5.196297 7.687075 1.740198
[,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16]
[1,] 2.760947 8.921383 10.64188 8.385258 6.788703 6.542282 8.631057 11.19689
[,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24]
[1,] 11.38957 11.16738 10.88189 10.48931 9.116005 7.757465 7.907191 8.453458
[,25] [,26] [,27] [,28] [,29] [,30] [,31] [,32]
[1,] 7.072456 6.89453 8.900875 8.613843 8.569073 11.82978 11.68813 9.27332
[,33] [,34] [,35] [,36] [,37] [,38] [,39]
[1,] 9.318501 9.64491 9.264779 9.357388 9.488624 10.57165 9.173374
Two approaches to do this problem if you truly have a matrix (it appears you do):
set.seed(10)
D <- matrix(rnorm(20) + 2, 1)
D2 <- ifelse(D > 1, "yes", "no") #method 1
D2
D2 <- matrix(rep("no", ncol(D)), 1) #method 2
D2[D > 1] <- "yes"
D2
Edit:
D2 <- ifelse(c(D) > 1, "yes", "no") #method 1
D2 <- matrix(rep("no", ncol(D)), 1) #method 2
D2[D > 1] <- "yes"
c(D2)

Resources