[R] Pixel Image Reshaping using R

Ivan Krylov kry|ov@r00t @end|ng |rom gm@||@com
Thu Feb 24 19:50:49 CET 2022


On Thu, 24 Feb 2022 13:31:09 -0500
Paul Bernal <paulbernal07 using gmail.com> wrote:

> Basically, what I am being asked to do is to take the
> train.csv dataset, and store it in a data structure so that the data
> can be reshaped into a matrix of size 28 x 28, then I just need to
> print some indices of that (e.g. index 1, 2, 4, 7,, etc.)

Is this homework? It's considered a good idea to contact your
instructor with homework questions.

> dataframe_train <- array(read.csv(file_path_2, header=TRUE,
> stringsAsFactors = FALSE), 28, 28)

Almost there. Two problems left:

1. You did not remove the first column. R will do what is asked of it
and mix the labels with the pixel values. You will probably need to
subset the data frame that read.csv() returns to remove it before
reshaping the rest of it into a three-way array.

3. The dimensions of the array should be specified in a single
3-element vector: c(N, 28, 28). I don't think there's a convenient way
to do that in a single expression. Once you have your pixels in an N by
784 matrix, use c(nrow(your.matrix), 28, 28) to construct the
dimension vector.

> then printing out several indices by doing (I know this could be done
> with a for loop better but I tried this):

> print(dataframe_train[1])

That chooses the N'th pixel value from the whole array. The syntax to
extract whole slices of an array is different:
https://cran.r-project.org/doc/manuals/r-release/R-intro.html#Array-indexing

-- 
Best regards,
Ivan



More information about the R-help mailing list