0
votes

I have a data frame in R with the following dimensions [15750,93]. I want to construct an image using this data such that there are 3 row coordinates and 31 column coordinates in the image. Each column in the data frame corresponds to data from one coordinate position in the image. The columns in the data frame have been arranged based on their respective coordinates in the following manner [1,1], [2,1], [3,1], [1,2], [2,2], [3,2] ......... [1,31],[2,31],[3,31]

To generate the image, for each column I would like to have an average of all values, a sum of all values and the highest value in each column. This way there will be exactly one value corresponding to a coordinate. And, with the 3 variations, I should get three types of images - average, sum and highest value.

Can someone help me in generating an overall image using this data or can guide me using data with smaller dimensions?

Some demo data below:

Dimensions of the data frame are [11, 15]

0   0   0   0   0   46  0   0   0   0   0   0   0   78  0
0   734 0   0   0   0   932 0   0   56  0   0   0   0   0
0   0   0   115 0   0   0   0   0   0   64  0   0   0   0
0   67  0   0   0   45  0   0   0   0   0   546 0   12  0
0   0   0   0   65  5   56  0   54  0   0   0   0   0   0
667 0   430 0   0   0   0   456 0   0   787 0   0   467 0
0   0   0   0   54  0   0   0   0   0   0   456 90  0   0
778 45  0   0   0   0   24  913 0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   26  0   0   0
234 0   0   620 0   0   0   0   0   106 0   0   901 0   0
0   0   0   0   0   0   45  0   34  0   0   0   0   0   0

I would like to have an image of with the dimensions [3,5] and the columns in the above data frame have been arranged based on their respective coordinates in the following manner [1,1], [2,1], [3,1], [1,2], [2,2], [3,2]..... and so on

The image coordinate arrangement [1,1] [1,2] [1,3] [1,4] [1,5] [2,1] [2,2] [2,3] [2,4] [2,5] [3,1] [3,2] [3,3] [3,4] [3,5]

1
Can you provide an example of the input data and the expected output? It's very hard to visualize from your question. - jdobres
@jdobres The question is now updated - novicegeek
This still doesn't make any sense to me. How exactly are these numbers supposed to become an image? The indexing isn't clear to me. - MrFlick
@MrFlick I got your point. I missed the crucial thing. For each column I would like to have an average of all values so every, a sum of all values and the highest value in each column. This way there will be exactly on value corresponding to a coordinate. And, with the 3 variations, I should get three types of images - average, sum and highest value. - novicegeek
Then you really need to edit your question to make that more clear. You have some sample input but you should also add the desired output. That way possible solutions can be tested and verified. - MrFlick

1 Answers

1
votes

This function reads in your dataset and finds the mean (or max or sum) of each column (yielding a series of numbers, one per column). It then reshapes that series into your desired output dimensions and displays as an image.

df <- read.table(header=FALSE,text="
0   0   0   0   0   46  0   0   0   0   0   0   0   78  0
0   734 0   0   0   0   932 0   0   56  0   0   0   0   0
0   0   0   115 0   0   0   0   0   0   64  0   0   0   0
0   67  0   0   0   45  0   0   0   0   0   546 0   12  0
0   0   0   0   65  5   56  0   54  0   0   0   0   0   0
667 0   430 0   0   0   0   456 0   0   787 0   0   467 0
0   0   0   0   54  0   0   0   0   0   0   456 90  0   0
778 45  0   0   0   0   24  913 0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   26  0   0   0
234 0   0   620 0   0   0   0   0   106 0   0   901 0   0
0   0   0   0   0   0   45  0   34  0   0   0   0   0   0
")

img <- function(data, op, tall, wide) image(t(matrix(sapply(data, op), nrow = wide, ncol = tall)), 
                                col = gray((0:32) / 32))
img(df, mean, 3, 5)
img(df, max, 3, 5)
img(df, sum, 3, 5)