novicegeek
novicegeek

Reputation: 773

Generate an image with specific dimensions from a data frame in R

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]

Upvotes: 0

Views: 308

Answers (1)

ddunn801
ddunn801

Reputation: 1890

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)

Upvotes: 1

Related Questions